#![doc(test(attr(deny(warnings))))] pub mod account;
pub mod stg;
pub mod prelude {
#[doc(inline)]
pub use crate::account::{Account, DeepError, Valid};
#[doc(inline)]
pub use crate::stg::{Setting, Stg, StgError, StgTrait};
}
#[doc(inline)]
pub use self::{account::Account, stg::Stg};
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::{
account::Account,
prelude::Valid,
stg::{Setting, Stg, StgError, StgTrait},
};
#[test]
fn stg_example() -> Result<(), StgError> {
let mut account = Account::<(), &str, Stg>::default();
account.insert("Number of trees", 5.stg());
account.insert("Grass color", "green".to_string().stg());
account.insert("Today is good", true.stg());
let today_bool: bool = account.get(&"Today is good").unstg()?;
let grass_color: String = account.get(&"Grass color").unstg_panic();
let trees: i32 = account.get(&"Number of trees").unstg()?;
print!(
"It's {today_bool} that today is a wonderful day,
the grass is {grass_color} and I can see {trees} trees in the distance"
);
Ok(())
}
#[test]
fn doc_example() -> Result<(), StgError> {
let mut account = Account::<String, &str, Stg>::default();
account.push(
Account::new(
"Default".to_string(), true, HashMap::from([
("lines", 3.stg()),
("word_repetition", 10.stg()),
("word", "default".to_string().stg()),
]), vec![], ),
Valid::new_true(),
);
account.push(
Account::new(
"Global Settings".to_string(),
true,
HashMap::from([
("word_repetition", 2.stg()),
("word", "global".to_string().stg()),
]), vec![],
),
Valid::new_true(),
);
account.push(
Account::new(
"Local Settings".to_string(),
true,
HashMap::from([("word", "local".to_string().stg())]),
vec![],
),
Valid::new_true(),
);
account.push(
Account::new(
"Inactive Account".to_string(),
false, HashMap::from([("word", "inactive".to_string().stg())]),
vec![],
),
Valid::new_true(),
);
let word: String = account.get(&"word").unstg()?;
let word_repetition = account.get(&"word_repetition").unstg()?;
let lines = account.get(&"lines").unstg()?;
let mut sentence = String::new();
for _ in 0..word_repetition {
sentence.push_str(&word);
sentence.push(' ');
}
sentence.pop();
for _ in 0..lines {
println!("{sentence}");
}
let ref_child_account: &Account<_, _, _> =
account.deep(&mut vec![&"Default".to_string()]).unwrap();
let inactive_word: String = ref_child_account.get(&"word").unstg()?;
println!("{inactive_word}");
let ref_child_account: &Account<_, _, _> = account
.deep(&mut vec![&"Inactive Account".to_string()])
.unwrap();
let inactive_word: String = ref_child_account.get(&"word").unstg()?;
println!("{inactive_word}");
Ok(())
}
#[test]
fn account_test() {
let bool_setting = true;
let i32_setting = 42;
let mut account = Account::<(), String, Stg>::default();
account.insert("bool_setting".to_string(), bool_setting.stg());
account.insert("i32_setting".to_string(), i32_setting.stg());
let i32s: i32 = account
.get(&"i32_setting".to_string())
.unwrap()
.clone()
.unstg_panic();
assert_eq!(i32s, 42);
let stg: Stg = account.get(&"bool_setting".to_string()).unwrap().clone();
assert!(stg.unstg_panic::<bool>());
}
#[test]
fn partialeq_test() {
assert!(true.stg() == true.stg());
}
#[test]
const fn setting_example() {
use crate::stg::Setting;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[allow(dead_code)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, Debug, PartialEq)]
pub struct MyType {}
#[cfg_attr(feature = "serde", typetag::serde)]
impl Setting for MyType {}
}
#[test]
fn account_new() {
let mut account1 = Account::new(
"name".to_string(),
Default::default(),
HashMap::default(),
Vec::default(),
);
account1.insert("answer to everything", 42.stg());
account1.insert("true is true", true.stg());
let account2 = Account::new(
"name".to_string(),
Default::default(),
[
("answer to everything", 42.stg()),
("true is true", true.stg()),
]
.into(),
Vec::default(),
);
assert!(account1 == account2);
}
}