Skip to content

Commit

Permalink
core: more sensible default values and constructors for package
Browse files Browse the repository at this point in the history
  • Loading branch information
barsoosayque committed Dec 1, 2024
1 parent 8e6679e commit 52ebfa3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
2 changes: 2 additions & 0 deletions crates/opensi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ quick-xml = { version = "0.36.2", features = ["serialize"] }
zip = { version = "0.5.4", default-features = false, features = ["deflate"] }
percent-encoding = "2.1.0"
derive_more = { version = "0.99.0", default-features = false, features = [ "from", "as_ref", "deref" ] }
uuid = { version = "1.11.0", features = [ "v4", "fast-rng" ] }
chrono = { version = "0.4.38", features = [ "now", "alloc", "wasmbind" ] }
41 changes: 38 additions & 3 deletions crates/opensi-core/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Authors {
pub authors: Vec<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Round {
#[serde(rename = "@name")]
Expand All @@ -35,7 +35,13 @@ pub struct Round {
pub themes: Vec<Theme>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
impl Default for Round {
fn default() -> Self {
Self { name: "Новый раунд".to_string(), kind: None, info: None, themes: vec![] }
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Theme {
#[serde(rename = "@name")]
pub name: String,
Expand Down Expand Up @@ -64,13 +70,29 @@ impl Theme {
}
}

impl Default for Theme {
fn default() -> Self {
Self {
name: "Новая тема".to_string(),
questions: vec![
Question { price: 100, ..Question::default() },
Question { price: 200, ..Question::default() },
Question { price: 300, ..Question::default() },
Question { price: 400, ..Question::default() },
Question { price: 500, ..Question::default() },
],
info: None,
}
}
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Questions {
#[serde(rename = "question")]
pub questions: Vec<Question>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Question {
#[serde(rename = "@price")]
Expand All @@ -87,6 +109,19 @@ pub struct Question {
pub info: Option<Info>,
}

impl Default for Question {
fn default() -> Self {
Self {
price: 100,
question_type: None,
scenario: vec![],
right: vec![],
wrong: vec![],
info: None,
}
}
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct QuestionType {
#[serde(rename = "@name")]
Expand Down
54 changes: 39 additions & 15 deletions crates/opensi-core/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::Datelike;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use quick_xml::de::from_str;
use quick_xml::se::to_string;
Expand All @@ -15,8 +16,8 @@ use crate::serde_impl;

/// Complete package structure with meta information about
/// the package and its tree of [`Question`].
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default, rename = "package")]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename = "package")]
pub struct Package {
// attributes
#[serde(rename = "@name")]
Expand All @@ -25,33 +26,58 @@ pub struct Package {
pub version: f32,
#[serde(rename = "@id")]
pub id: String,
#[serde(rename = "@date")]
#[serde(default, rename = "@date")]
pub date: String,
#[serde(rename = "@publisher")]
#[serde(default, rename = "@publisher")]
pub publisher: String,
#[serde(rename = "@difficulty")]
#[serde(default, rename = "@difficulty")]
pub difficulty: u8,
#[serde(rename = "@language", skip_serializing_if = "String::is_empty")]
#[serde(default, rename = "@language", skip_serializing_if = "String::is_empty")]
pub language: String,
#[serde(rename = "@logo", skip_serializing_if = "Option::is_none")]
#[serde(default, rename = "@logo", skip_serializing_if = "Option::is_none")]
pub logo: Option<String>,
#[serde(rename = "@restriction", skip_serializing_if = "String::is_empty")]
#[serde(default, rename = "@restriction", skip_serializing_if = "String::is_empty")]
pub restriction: String,
#[serde(rename = "@xmlns")]
#[serde(default, rename = "@xmlns")]
pub namespace: String,

// elements
#[serde(default)]
pub info: Info,
#[serde(with = "serde_impl::rounds")]
#[serde(default, with = "serde_impl::rounds")]
pub rounds: Vec<Round>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,

// resources
#[serde(skip)]
pub resource: HashMap<Resource, Vec<u8>>,
}

/// # Creation of package.
impl Package {
pub fn new() -> Self {
let utc = chrono::Utc::now();

Self {
name: "Новый пакет вопросов".to_string(),
version: 5.0,
id: uuid::Uuid::new_v4().to_string(),
date: format!("{}-{:0>2}-{:0>2}", utc.year(), utc.month(), utc.day()),
publisher: String::new(),
difficulty: 5,
language: String::new(),
logo: None,
restriction: String::new(),
namespace: String::new(),
info: Info::default(),
rounds: vec![],
tags: vec![],
resource: HashMap::new(),
}
}
}

/// # [`PackageNode`]-based methods
impl Package {
/// Clone a node and push it afterwards.
Expand Down Expand Up @@ -151,8 +177,7 @@ impl Package {
/// Create a new default [`Round`], push it and return
/// a reference to it.
pub fn allocate_round(&mut self) -> &mut Round {
let round = Round { name: "Новый раунд".to_string(), ..Default::default() };
self.push_round(round)
self.push_round(Round::default())
}

/// Check if [`Round`] by index exist.
Expand Down Expand Up @@ -229,8 +254,7 @@ impl Package {
/// and return a reference to it.
pub fn allocate_theme(&mut self, idx: impl Into<RoundIdx>) -> Option<&mut Theme> {
let idx = idx.into();
let theme = Theme { name: "Новая тема".to_string(), ..Default::default() };
self.push_theme(idx, theme)
self.push_theme(idx, Theme::default())
}

/// Return amount of [`Theme`]s in a [`Round`].
Expand Down
4 changes: 2 additions & 2 deletions crates/opensi-editor/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl eframe::App for EditorApp {
};
if new_pack_modal.suggested_button(ui, "Перезаписать").clicked() {
self.package_state =
PackageState::Active { package: Package::default(), selected: None };
PackageState::Active { package: Package::new(), selected: None };
};
});
});
Expand Down Expand Up @@ -127,7 +127,7 @@ impl eframe::App for EditorApp {
},
_ => {
self.package_state = PackageState::Active {
package: Package::default(),
package: Package::new(),
selected: None,
};
},
Expand Down

0 comments on commit 52ebfa3

Please sign in to comment.