,
}
impl SheetModel {
/// Create a new SheetModel
pub fn new() -> SheetModel {
- SheetModel { config: None }
+ SheetModel {
+ sheet_config: None,
+ skill_config: None,
+ }
}
/// Loads a model file from the specified path.
- ///
- /// # Arguments
- ///
- /// * `path` - A reference to the path of the model file to load.
- ///
- /// # Returns
- ///
- /// A Result asserting if the loading was sucessful or a LoadError if an error occurs.
- ///
- /// # Examples
- ///
- /// ```
- /// use rust20_d10::loader::ModelLoader;
- ///
- /// let loader = ModelLoader::new();
- /// match loader.load_model("path/to/model_file.txt") {
- /// Ok(model_data) => println!("Model loaded successfully: {:?}", model_data),
- /// Err(error) => eprintln!("Error loading model: {:?}", error),
- /// }
- /// ```
- pub fn load_model(&mut self, path: P) -> Result<(), LoadError>
+ pub fn load_global_model
(&mut self, path: P) -> Result<(), LoadError>
where
P: AsRef,
{
// Attempt to read the file
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
-
- let model_data: GlobalConfig = toml::from_str(&file_content).unwrap(); //. .ok_or(LoadError::InvalidFormat);
-
- self.config = Some(model_data);
+ let model_data: GlobalFormat = toml::from_str(&file_content).unwrap(); //. .ok_or(LoadError::InvalidFormat);
+ self.sheet_config = Some(model_data);
Ok(())
}
+ /// Load the skill formater
+ fn load_skill_model(&mut self, path_skills: P) -> Result<(), LoadError>
+ where
+ P: AsRef,
+ {
+ let file_content = fs::read_to_string(&path_skills).map_err(LoadError::Io)?;
+ let skill_data: SheetFormat = toml::from_str(&file_content).unwrap();
+ self.skill_config = Some(skill_data);
+ Ok(())
+ }
+
/// Create a Model from a path (shortcut for creating and loading)
- pub fn from_file(path: P) -> Result
+ pub fn model_from_files(path_global: P, path_skills: P) -> Result
where
P: AsRef,
{
let mut modl = Self::new();
- modl.load_model(path)?;
+ modl.load_global_model(path_global)?;
+ modl.load_skill_model(path_skills)?;
Ok(modl)
}
}
@@ -83,28 +78,33 @@ mod tests {
#[test]
fn test_creation() {
let modl = SheetModel::new();
- assert!(modl.config.is_none())
+ assert!(modl.sheet_config.is_none())
}
#[test]
fn test_load() {
let mut modl = SheetModel::new();
- assert!(modl.load_model(Path::new("data/cyberpunk.toml")).is_ok());
- assert!(modl.config.is_some())
+ assert!(modl
+ .load_global_model(Path::new("data/cyberpunk.toml"))
+ .is_ok());
+ assert!(modl.sheet_config.is_some())
}
#[test]
fn test_bad_load() {
let mut modl = SheetModel::new();
- assert!(modl.load_model(Path::new("wrong_file")).is_err())
+ assert!(modl.load_global_model(Path::new("wrong_file")).is_err())
}
#[test]
fn test_from_file() {
- let modl = SheetModel::from_file(Path::new("data/cyberpunk.toml"));
+ let path_global = Path::new("data/cyberpunk.toml");
+ let path_skills = Path::new("data/cyberpunk_skill_format.toml");
+ let bad_path = Path::new("wrong_file");
+ let modl = SheetModel::model_from_files(path_global, path_skills);
assert!(modl.is_ok());
- assert!(modl.unwrap().config.is_some());
- let modl = SheetModel::from_file(Path::new("wrong_file"));
+ assert!(modl.unwrap().sheet_config.is_some());
+ let modl = SheetModel::model_from_files(bad_path, path_skills);
assert!(modl.is_err());
match modl.err().unwrap() {
crate::loader::LoadError::Io(_e) => (),
diff --git a/src/main.rs b/src/main.rs
index 8d9d38c..e366e81 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@
#![allow(dead_code)]
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
+
use loader::SheetModel;
mod data_structure;
@@ -12,7 +13,9 @@ mod generator;
mod loader;
mod skills_structures;
+#[cfg(not(tarpaulin_include))]
fn main() {
- let modl = SheetModel::from_file("data/cyberpunk.toml");
+ let modl =
+ SheetModel::model_from_files("data/cyberpunk.toml", "data/cyberpunk_skill_format.toml");
println!("{:?}", modl);
}
diff --git a/src/skills_structures.rs b/src/skills_structures.rs
index d0e0767..dee2ff3 100644
--- a/src/skills_structures.rs
+++ b/src/skills_structures.rs
@@ -3,8 +3,10 @@
//! The `skills` module provides data structures and methods for managing skills.
//!
//! Includes all Section/Subsection/Skill/Sub-skill and a skill-list parser
+// #![warn(dead_code)]
use std::{collections::HashMap, str::FromStr};
-use strfmt::strfmt;
+
+use crate::generator::to_skill_name;
/// Type sugarcoat of a replacement hashmap
type HashReplace = HashMap;
@@ -84,7 +86,8 @@ impl DataCell {
/// Create a datacell from the given raw string
pub fn new(raw_name: &str, id: usize, global_id: usize) -> Self {
let name = String::from_str(raw_name).unwrap();
- let stripped_name = name.replace(|c: char| !c.is_ascii_alphabetic(), "");
+ let stripped_name = to_skill_name(&name);
+ // let stripped_name = name.replace(|c: char| !c.is_ascii_alphabetic(), "");
DataCell {
name,
stripped_name,
@@ -101,28 +104,28 @@ impl DataCell {
h.insert("id".to_string(), self.id.to_string());
h.insert("global_id".to_string(), self.global_id.to_string());
if self.id == 1 {
- h.insert("selected".to_string(), "true".to_owned());
+ h.insert("selected".to_string(), "checked=\"checked\"".to_owned());
} else {
- h.insert("selected".to_string(), "false".to_owned());
+ h.insert("selected".to_string(), "".to_owned());
}
h
}
}
-/// A CharacterSheet ready to be rendered
+/// A A skill that contains all information to be rendered by the external renderer
trait Renderable {
/// Create a new renderable
fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self;
/// Get the vars hashmap
fn get_vars(&self) -> HashReplace;
- /// Render the element and all sub elements
- fn render(&self, model: &str) -> Result;
- /// Render the element header
- fn header(&self, model: &str) -> Result;
- /// Render the element content
- fn content(&self, model: &str) -> Result;
- /// Render the element footer
- fn footer(&self, model: &str) -> Result;
+ // /// Render the element and all sub elements
+ // fn render(&self, model: &str) -> Result;
+ // /// Render the element header
+ // fn header(&self, model: &str) -> Result;
+ // /// Render the element content
+ // fn content(&self, model: &str) -> Result;
+ // /// Render the element footer
+ // fn footer(&self, model: &str) -> Result;
/// Get the underlying name of the skill
fn get_name(&self) -> &str;
/// Get the underlying short name
@@ -150,36 +153,36 @@ impl Renderable for Section {
self.data.to_hashmap()
}
- /// A section cannot be rendered as a whole
- fn render(&self, _model: &str) -> Result {
- Err(ErrorSkills::NonCallableMethod)
- }
+ // /// A section cannot be rendered as a whole
+ // fn render(&self, _model: &str) -> Result {
+ // Err(ErrorSkills::NonCallableMethod)
+ // }
- fn header(&self, model: &str) -> Result {
- let mut vars = HashMap::new();
- vars.insert("number".to_string(), "0");
- vars.insert("name".to_string(), self.get_name());
+ // fn header(&self, model: &str) -> Result {
+ // let mut vars = HashMap::new();
+ // vars.insert("number".to_string(), "0");
+ // vars.insert("name".to_string(), self.get_name());
- match self.get_id() {
- 0 => vars.insert("checked".to_string(), "checked=\"checked\""),
- _ => vars.insert("checked".to_string(), ""),
- };
- match strfmt(model, &vars) {
- Ok(res) => Ok(res),
- Err(e) => Err(ErrorSkills::FormatError(e)),
- }
- }
+ // match self.get_id() {
+ // 0 => vars.insert("checked".to_string(), "checked=\"checked\""),
+ // _ => vars.insert("checked".to_string(), ""),
+ // };
+ // match strfmt(model, &vars) {
+ // Ok(res) => Ok(res),
+ // Err(e) => Err(ErrorSkills::FormatError(e)),
+ // }
+ // }
- fn content(&self, model: &str) -> Result {
- match strfmt(model, &self.get_vars()) {
- Ok(r) => Ok(r),
- Err(e) => Err(ErrorSkills::FormatError(e)),
- }
- }
+ // fn content(&self, model: &str) -> Result {
+ // match strfmt(model, &self.get_vars()) {
+ // Ok(r) => Ok(r),
+ // Err(e) => Err(ErrorSkills::FormatError(e)),
+ // }
+ // }
- fn footer(&self, _model: &str) -> Result {
- Err(ErrorSkills::NonCallableMethod)
- }
+ // fn footer(&self, _model: &str) -> Result {
+ // Err(ErrorSkills::NonCallableMethod)
+ // }
fn new(name: &str, id: usize, global_id: usize, size_hint: usize) -> Self {
Self {
@@ -190,6 +193,7 @@ impl Renderable for Section {
}
/// All errors regarding the skill handling
+#[derive(Debug)]
pub enum ErrorSkills {
/// The formatting failed
FormatError(strfmt::FmtError),
@@ -199,23 +203,48 @@ pub enum ErrorSkills {
#[cfg(test)]
mod tests {
+ use std::collections::HashMap;
+
use super::{DataCell, Renderable, Section};
static S1: &str =
"This is a test sentence with some non-ASCII characters like café and résumé.";
- static S2: &str = "ThisisatestsentencewithsomenonASCIIcharacterslikecafandrsum";
+ static S2: &str = "thisisatestsentencewithsomenonasciicharacterslikecafandrsum";
static T1: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris in velit purus. Cras commodo, libero vel tempus facilisis, erat metus eleifend nulla, at dignissim nibh erat vel magna. Maecenas in augue eget tortor rhoncus venenatis. Fusce non odio vitae velit dictum ultricies eget a mauris. Nullam sodales nisi in enim pulvinar, ut eleifend ex feugiat. Integer tincidunt, metus et consectetur ultricies, magna dolor fringilla lorem, non ultricies ligula est quis justo. Quisque non diam at magna efficitur fermentum.";
- static T2: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris in velit purus. Cras commodo, libero vel tempus facilisis, erat metus eleifend nulla, at dignissim nibh erat vel magna. Maecenas in augue eget tortor rhoncus venenatis. Fusce non odio vitae velit dictum ultricies eget a mauris. Nullam sodales nisi in enim pulvinar, ut eleifend ex feugiat. Integer tincidunt, metus et consectetur ultricies, magna dolor fringilla lorem, non ultricies ligula est quis justo. Quisque non diam at magna efficitur fermentum.";
+ static T2: &str = "lorem ipsum dolor sit amet, consectetur adipiscing elit. mauris in velit purus. cras commodo, libero vel tempus facilisis, erat metus eleifend nulla, at dignissim nibh erat vel magna. maecenas in augue eget tortor rhoncus venenatis. fusce non odio vitae velit dictum ultricies eget a mauris. nullam sodales nisi in enim pulvinar, ut eleifend ex feugiat. integer tincidunt, metus et consectetur ultricies, magna dolor fringilla lorem, non ultricies ligula est quis justo. quisque non diam at magna efficitur fermentum.";
static U: &str = "Thïs ïs å råñdôm têšt wïth 1234 sÿmbøls ãñd chårãctêrs lïkê ¡@#$%^&*()_+={}[]:;'\"<>,.?/èéêëēęëēæœöôóøōõúüùûūų";
static V: &str = "Testing special characters like ©, ®, ™, and emojis like 😊, 💻.";
#[test]
fn test_datacell() {
- let ds = DataCell::new(S1, 1, 1);
+ let ds = DataCell::new(S1, 1, 2);
+ let ds2 = DataCell::new(S1, 2, 2);
assert_eq!(ds.name, S1);
assert_eq!(ds.stripped_name, S2);
+
+ let mut map = HashMap::new();
+ map.insert("name".to_string(), S1.to_string());
+ map.insert("pname".to_string(), S2.to_string());
+ map.insert("id".to_string(), "1".to_string());
+ map.insert("global_id".to_string(), "2".to_string());
+ map.insert("selected".to_string(), "checked=\"checked\"".to_string());
+ assert_eq!(ds.to_hashmap(), map);
+ map.insert("selected".to_string(), "".to_string());
+ map.insert("id".to_string(), "2".to_string());
+ assert_eq!(ds2.to_hashmap(), map);
}
#[test]
fn test_section() {
- let _s1 = Section::new(T1, 0, 1, 30);
+ let s1 = Section::new(S1, 1, 200, 30);
+ assert_eq!(s1.get_name(), S1);
+ assert_eq!(s1.get_stripped_name(), S2);
+ assert_eq!(s1.get_id(), 1);
+ assert_eq!(s1.get_global_id(), 200);
+ let mut map = HashMap::new();
+ map.insert("name".to_string(), S1.to_string());
+ map.insert("pname".to_string(), S2.to_string());
+ map.insert("id".to_string(), "1".to_string());
+ map.insert("global_id".to_string(), "200".to_string());
+ map.insert("selected".to_string(), "checked=\"checked\"".to_string());
+ assert_eq!(s1.get_vars(), map);
}
}
diff --git a/tarpaulin-report.html b/tarpaulin-report.html
index 71582eb..794f2da 100644
--- a/tarpaulin-report.html
+++ b/tarpaulin-report.html
@@ -118,8 +118,8 @@