OrangeGuidanceTomestone/server/src/pack.rs

74 lines
2.4 KiB
Rust
Raw Normal View History

2022-09-08 03:07:00 +00:00
use serde::{Deserialize, Serialize};
2022-09-03 10:35:15 +00:00
use uuid::Uuid;
2022-09-08 03:07:00 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2022-09-03 10:35:15 +00:00
pub struct Pack {
pub name: String,
pub id: Uuid,
2022-09-08 03:07:00 +00:00
#[serde(skip_serializing)]
pub visible: bool,
#[serde(default, skip_serializing)]
pub order: u8,
2022-09-03 10:35:15 +00:00
pub templates: Vec<String>,
pub conjunctions: Vec<String>,
pub words: Vec<WordList>,
}
2022-09-08 03:07:00 +00:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2022-09-03 10:35:15 +00:00
pub struct WordList {
pub name: String,
pub words: Vec<String>,
}
impl Pack {
pub fn format(&self, template_1_idx: usize, word_1_idx: Option<(usize, usize)>, conjunction: Option<usize>, template_2_idx: Option<usize>, word_2_idx: Option<(usize, usize)>) -> Option<String> {
let template_1 = self.templates.get(template_1_idx)?;
if template_1.contains("{0}") && word_1_idx.is_none() {
return None;
}
2022-09-10 07:06:49 +00:00
let mut formatted = if_chain::if_chain! {
if template_1.contains("{0}");
if let Some((w1_list, w1_word)) = word_1_idx;
then {
let word_1 = self.words.get(w1_list)?.words.get(w1_word)?;
template_1.replace("{0}", word_1)
} else {
template_1.clone()
}
2022-09-03 10:35:15 +00:00
};
if let Some(conj_idx) = conjunction {
if let Some(template_2_idx) = template_2_idx {
let conj = self.conjunctions.get(conj_idx)?;
2022-09-04 04:32:56 +00:00
let is_punc = conj.len() == 1 && conj.chars().next().map(|x| x.is_ascii_punctuation()).unwrap_or(false);
2022-09-04 05:03:59 +00:00
if is_punc {
2022-09-04 04:32:56 +00:00
formatted.push_str(conj);
2022-09-03 10:35:15 +00:00
formatted.push('\n');
2022-09-04 04:32:56 +00:00
} else {
formatted.push('\n');
formatted.push_str(conj);
formatted.push(' ');
2022-09-03 10:35:15 +00:00
}
let template_2 = self.templates.get(template_2_idx)?;
2022-09-10 07:06:49 +00:00
let append = if_chain::if_chain! {
if template_2.contains("{0}");
if let Some((w2_list, w2_word)) = word_2_idx;
then {
let word_2 = self.words.get(w2_list)?.words.get(w2_word)?;
template_2.replace("{0}", word_2)
} else {
template_2.clone()
}
2022-09-03 10:35:15 +00:00
};
formatted.push_str(&append);
}
}
Some(formatted)
}
}