This repository has been archived on 2026-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
rust20_d10/src/loader.rs

124 lines
3.6 KiB
Rust

//! # Loader Module
//!
//! The `loader` module provides functionality for loading a specific model file and handling IO operations and errors.
use std::{fs, io, path::Path};
use crate::data_structure::{GlobalFormat, SheetFormat};
/// Represents an error during Loading
#[derive(Debug)]
pub enum LoadError {
/// Indicate an IOError occured
Io(io::Error),
/// Indicate the file format is invalid
InvalidFormat,
/// Indicate a line couldn't be parsed
LineParseError,
/// Indicate an element was improperly hierarchized
WrongHierarchy,
/// Empty line, skip it
EmptyLine,
}
/// Represents a model once loaded
#[derive(Debug)]
pub struct SheetModel {
/// The Config associated with a Model
sheet_config: Option<GlobalFormat>,
/// The sheet formatter associated with the Model
skill_config: Option<SheetFormat>,
}
impl SheetModel {
/// Create a new SheetModel
pub fn new() -> SheetModel {
SheetModel {
sheet_config: None,
skill_config: None,
}
}
/// Loads a model file from the specified path.
pub fn load_global_model<P>(&mut self, path: P) -> Result<(), LoadError>
where
P: AsRef<Path>,
{
// Attempt to read the file
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
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<P>(&mut self, path_skills: P) -> Result<(), LoadError>
where
P: AsRef<Path>,
{
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 model_from_files<P>(path_global: P, path_skills: P) -> Result<Self, LoadError>
where
P: AsRef<Path>,
{
let mut modl = Self::new();
modl.load_global_model(path_global)?;
modl.load_skill_model(path_skills)?;
Ok(modl)
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use crate::loader::SheetModel;
#[test]
fn test_creation() {
let modl = SheetModel::new();
assert!(modl.sheet_config.is_none())
}
#[test]
fn test_load() {
let mut modl = SheetModel::new();
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_global_model(Path::new("wrong_file")).is_err())
}
#[test]
fn test_from_file() {
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().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) => (),
crate::loader::LoadError::InvalidFormat => (),
crate::loader::LoadError::LineParseError => (),
crate::loader::LoadError::WrongHierarchy => (),
crate::loader::LoadError::EmptyLine => (),
}
}
}