pub trait XCfg {
// Provided methods
fn with_format<P: AsRef<Path>>(
path: P,
fmt: Format,
) -> Result<File<Self, P>, Error>
where Self: DeserializeOwned { ... }
fn load<P: AsRef<Path>>(path: P) -> Result<File<Self, PathBuf>, Error>
where Self: DeserializeOwned { ... }
fn load_or_default<P: AsRef<Path>>(path: P) -> Result<File<Self, P>, Error>
where Self: Default + DeserializeOwned { ... }
fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>
where Self: Serialize { ... }
fn fmt_to_string(&self, fmt: Format) -> Result<String, Error>
where Self: Serialize { ... }
}Provided Methods§
fn with_format<P: AsRef<Path>>(
path: P,
fmt: Format,
) -> Result<File<Self, P>, Error>where
Self: DeserializeOwned,
Sourcefn load<P: AsRef<Path>>(path: P) -> Result<File<Self, PathBuf>, Error>where
Self: DeserializeOwned,
fn load<P: AsRef<Path>>(path: P) -> Result<File<Self, PathBuf>, Error>where
Self: DeserializeOwned,
§Example
use serde::{Deserialize, Serialize};
use xcfg::XCfg;
#[derive(XCfg, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Test {
a: i32,
b: Vec<i32>,
sub: SubTest,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct SubTest {
c: Vec<String>,
}
let test = Test {
a: 1,
b: vec![0, 1, 2],
sub: SubTest {
c: vec!["ab".to_string(), "cd".to_string()],
},
};
let path = "./test.toml";
test.save(path).unwrap();
assert_eq!(Test::load(path).unwrap().into_inner(), test);
std::fs::remove_file(path).unwrap();Sourcefn load_or_default<P: AsRef<Path>>(path: P) -> Result<File<Self, P>, Error>where
Self: Default + DeserializeOwned,
fn load_or_default<P: AsRef<Path>>(path: P) -> Result<File<Self, P>, Error>where
Self: Default + DeserializeOwned,
§Example
use serde::{Deserialize, Serialize};
use xcfg::XCfg;
#[derive(XCfg, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Test {
a: i32,
b: Vec<i32>,
sub: SubTest,
}
impl Default for Test {
fn default() -> Self {
Self {
a: 0,
b: vec![],
sub: SubTest::default(),
}
}
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct SubTest {
c: Vec<String>,
}
impl Default for SubTest {
fn default() -> Self {
Self { c: vec![] }
}
}
let test = Test {
a: 1,
b: vec![0, 1, 2],
sub: SubTest {
c: vec!["ab".to_string(), "cd".to_string()],
},
};
let path = "./test.toml";
let mut f = Test::load_or_default(path).unwrap();
assert_eq!(f.inner, Test::default());
f.inner = test.clone();
f.save().unwrap();
assert_eq!(Test::load(path).unwrap().into_inner(), test);
std::fs::remove_file(path).unwrap();Sourcefn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>where
Self: Serialize,
fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>where
Self: Serialize,
§Example
use serde::{Deserialize, Serialize};
use xcfg::XCfg;
#[derive(XCfg, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Test {
a: i32,
b: Vec<i32>,
sub: SubTest,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct SubTest {
c: Vec<String>,
}
let test = Test {
a: 1,
b: vec![0, 1, 2],
sub: SubTest {
c: vec!["ab".to_string(), "cd".to_string()],
},
};
let path = "./test.toml";
test.save(path).unwrap();
std::fs::remove_file(path).unwrap();Sourcefn fmt_to_string(&self, fmt: Format) -> Result<String, Error>where
Self: Serialize,
fn fmt_to_string(&self, fmt: Format) -> Result<String, Error>where
Self: Serialize,
§Example
use serde::{Deserialize, Serialize};
use xcfg::{XCfg, Format};
#[derive(XCfg, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Test {
a: i32,
b: Vec<i32>,
sub: SubTest,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct SubTest {
c: Vec<String>,
}
let test = Test {
a: 1,
b: vec![0, 1, 2],
sub: SubTest {
c: vec!["ab".to_string(), "cd".to_string()],
},
};
let right = r#"a = 1
b = [0, 1, 2]
[sub]
c = ["ab", "cd"]
"#;
assert_eq!(test.fmt_to_string(Format::Toml).unwrap(), right);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.