pub struct Config { /* private fields */ }Expand description
Represents a configuration object.
Start by creating a new Config object:
let config = Config::new("org.example.Demo", 1, None)?;Provide an application name, a version and optionally a prefix, then, a new directory will be added to your filesystem, this is where all the created files will be stored in.
§Write a file.
let config = Config::new("org.example.Demo", 1, None)?;
config.set_json("colors", json!({ "accent": "#7a7af9" }))?;This wil store the file here:
$HOME/.config/org.example.Demo/v1/colors.json
§Get a file.
#[derive(Debug, Serialize, Deserialize)]
struct Colors { accent: String }
let settings: Colors = config.get_json("colors")?;Implementations§
Source§impl Config
impl Config
Sourcepub fn new(name: &str, version: u64, scope: Option<&str>) -> Result<Self, Error>
pub fn new(name: &str, version: u64, scope: Option<&str>) -> Result<Self, Error>
Creates a new Config object.
§Arguments
name- The name of the application.version- The version of the configuration.scope- An optional scope for the application.
§Returns
A Result containing the new Config object or an Error if an error occurred.
Examples found in repository?
More examples
Sourcepub fn get_json<T: DeserializeOwned>(&self, key: &str) -> Result<T, Error>
pub fn get_json<T: DeserializeOwned>(&self, key: &str) -> Result<T, Error>
Gets the content of a json file with the given key and deserializes it into a type.
§Arguments
key- The key used to store the file.
§Returns
A Result containing the deserialized value or an Error if an error occurred.
Examples found in repository?
More examples
Sourcepub fn set_json<T: Serialize>(&self, key: &str, value: T) -> Result<(), Error>
pub fn set_json<T: Serialize>(&self, key: &str, value: T) -> Result<(), Error>
Sets the content of a json file with the given key and serializes the value.
§Arguments
key- The key used to store the file.value- The value to be serialized and stored.
§Returns
A Result indicating success or an Error if an error occurred.
Examples found in repository?
More examples
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more