lang-evolve-core/src/lib.rs

113 lines
3.0 KiB
Rust

#![crate_name = "lang_evolve_core"]
#![deny(clippy::all)]
//! # LangEvolveCore
//!
//! `lang_evolve_core` is the core crate used by two other crates:
//! `lang_evolve_cli` and `lang_evolve_gui`.
//!
//! # What it does
//!
//! LangEvolveCore is the crate that hosts all the core logic behind LangEvolve,
//! a conlanging software developped after the
//! [original software](https://github.com/ceronyon/LangEvolve) which applies
//! user-defined sound changes to words and texts based on regex expressions.
use std::fs::File;
use log::{info, warn};
use simplelog::*;
pub mod settings;
pub mod utils;
/// Initializes the crate
///
/// # What it does
///
/// Initializing the crate allows to initialize its logging system. All its logs
/// will be written to the file `core.log` while the errors will also be shown
/// in the terminal.
///
/// # Return type
///
/// This function returns a null object if it succeeds. However, if it does not,
/// it will send a `log::SetLoggerError` back.
///
/// # Example usage
///
/// Its usage is extremely simple, just call it before you do anything with it:
/// ```
/// lang_evolve_core::init();
/// ```
pub fn init() -> std::result::Result<(), log::SetLoggerError> {
// #[cfg(debug_assertions)]
match CombinedLogger::init(if cfg!(debug_assertions) {
vec![
WriteLogger::new(
LevelFilter::Warn,
Config::default(),
File::create("core.log").unwrap(),
),
WriteLogger::new(
LevelFilter::Debug,
Config::default(),
File::create("core.log").unwrap(),
),
WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create("core.log").unwrap(),
),
]
} else {
vec![
WriteLogger::new(
LevelFilter::Warn,
Config::default(),
File::create("core.log").unwrap(),
),
WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create("core.log").unwrap(),
),
]
}) {
Err(why) => {
warn!("Could not initialize logger: {}", why.to_string());
Err(why)
}
Ok(_) => {
info!("Logger initialized");
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_settings() {
let s = settings::Settings::new();
let path = std::path::Path::new("settings.yaml");
let settings = r#"---
version: "1"
categories: {}
rules: []"#;
utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap();
assert_eq!(settings, utils::read_file(&path).unwrap());
}
#[test]
fn read_settings() {
let s1 = settings::Settings::new();
let path = std::path::Path::new("settings.yml");
s1.export(&path).unwrap();
let s2 = settings::Settings::import(&path).unwrap();
assert_eq!(s1, s2);
}
}