Config

Struct Config 

Source
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

Source

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?
examples/clean.rs (line 4)
3fn main() -> Result<(), Error> {
4    let config = Config::new("org.example.Demo", 1, None)?;
5    config.clean()?;
6    Ok(())
7}
More examples
Hide additional examples
examples/set.rs (line 5)
4fn main() -> Result<(), Error> {
5    let config = Config::new("org.example.Demo", 1, None)?;
6    config.set_json("colors", json!({ "accent": "#7a7af9" }))?;
7    Ok(())
8}
examples/get.rs (line 10)
9fn main() -> Result<(), Error> {
10    let config = Config::new("org.example.Demo", 1, None)?;
11    let colors: Colors = config.get_json("colors")?;
12    println!("{colors:?}");
13    Ok(())
14}
examples/scope.rs (line 11)
10fn main() -> Result<(), Error> {
11    let config = Config::new("org.example.Demo", 1, Some("appearance"))?;
12    config.set_json("colors", json!({ "accent": "#7a7af9" }))?;
13    let colors: Colors = config.get_json("colors")?;
14    println!("{colors:?}");
15    Ok(())
16}
Source

pub fn has_plain(&self, key: &str) -> bool

Determines if a plain file with the given key is present in the filesystem.

§Arguments
  • key - The key used to store the file.
§Returns

true if the plain file exists, false otherwise.

Source

pub fn has_json(&self, key: &str) -> bool

Determines if a json file with the given key is present in the filesystem.

§Arguments
  • key - The key used to store the file.
§Returns

true if the json file exists, false otherwise.

Source

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?
examples/get.rs (line 11)
9fn main() -> Result<(), Error> {
10    let config = Config::new("org.example.Demo", 1, None)?;
11    let colors: Colors = config.get_json("colors")?;
12    println!("{colors:?}");
13    Ok(())
14}
More examples
Hide additional examples
examples/scope.rs (line 13)
10fn main() -> Result<(), Error> {
11    let config = Config::new("org.example.Demo", 1, Some("appearance"))?;
12    config.set_json("colors", json!({ "accent": "#7a7af9" }))?;
13    let colors: Colors = config.get_json("colors")?;
14    println!("{colors:?}");
15    Ok(())
16}
Source

pub fn get_plain(&self, key: &str) -> Result<String, Error>

Gets the content of a plain file with the given key.

§Arguments
  • key - The key used to store the file.
§Returns

A Result containing the value or an Error if an error occurred.

Source

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?
examples/set.rs (line 6)
4fn main() -> Result<(), Error> {
5    let config = Config::new("org.example.Demo", 1, None)?;
6    config.set_json("colors", json!({ "accent": "#7a7af9" }))?;
7    Ok(())
8}
More examples
Hide additional examples
examples/scope.rs (line 12)
10fn main() -> Result<(), Error> {
11    let config = Config::new("org.example.Demo", 1, Some("appearance"))?;
12    config.set_json("colors", json!({ "accent": "#7a7af9" }))?;
13    let colors: Colors = config.get_json("colors")?;
14    println!("{colors:?}");
15    Ok(())
16}
Source

pub fn set_plain(&self, key: &str, value: impl ToString) -> Result<(), Error>

Sets the content of a plain file with the given key.

§Arguments
  • key - The key used to store the file.
  • value - String to write.
§Returns

A Result indicating success or an Error if an error occurred.

Source

pub fn path(&self, key: &str, file_type: FileType) -> Result<PathBuf, Error>

Given a key, returns the file path in the filesystem.

§Arguments
  • key - The key used to store the file.
  • file_type - The file extension.
§Returns

A Result containing the file path or an Error if an error occurred.

Source

pub fn clean(&self) -> Result<(), Error>

Removes all files in the configuration path.

§Returns

A Result containing the file path or an Error if an error occurred.

Examples found in repository?
examples/clean.rs (line 5)
3fn main() -> Result<(), Error> {
4    let config = Config::new("org.example.Demo", 1, None)?;
5    config.clean()?;
6    Ok(())
7}

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more