Cfg

Struct Cfg 

Source
pub struct Cfg;
Expand description

Config file helper

A simple helper for managing config files.

Implementations§

Source§

impl Cfg

Source

pub fn path(appname: &str, file: &str) -> PathBuf

Config file path

Returns the formatted path to a config file of an app.

Source

pub fn read(appname: &str, file: &str) -> Result<String>

Read config file

Reads an app’s config file into a String. This can then be used to parse it via Serde.

Examples found in repository?
examples/config.rs (line 14)
9fn main() -> io::Result<()> {
10	paintln!(Colors::Blue, "Saving test file...");
11	Cfg::save(APP, FILE, PAYLOAD)?;
12
13	paintln!(Colors::Cyan, "Reading test file...");
14	let text = Cfg::read(APP, FILE)?;
15
16	println!("Test file: {text}");
17	assert_eq!(text, PAYLOAD.to_owned());
18	Ok(())
19}
More examples
Hide additional examples
examples/wallpaper-dl.rs (line 10)
8fn main() {
9	paint!(Colors::Green, "Your wallpaper-dl config: ");
10	match Cfg::read(APP_NAME, CONFIG_FILE) {
11		Err(_) => paintln!(Colors::YellowBold, "Not found"),
12		Ok(x) => println!("\n{x}")
13	}
14
15	println!("\n{}\n", "=".repeat(36));
16
17	paint!(Colors::Blue, "Your wallpaper database: ");
18	match Appdata::read_str(APP_NAME, DB_FILE) {
19		Err(_) => paintln!(Colors::RedBold, "Not found"),
20		Ok(x) => println!("\n{x}")
21	}
22}
examples/alacritty.rs (line 7)
5fn main() {
6	paintln!(Colors::Rgb(42, 164, 69), "Attempting to read alacritty config file...");
7	match Cfg::read("alacritty", "alacritty.toml") {
8		Ok(data) => println!("Your alacritty config:\n{}", data),
9		Err(_) => paintln!(Colors::Red, "You don't seem to have an alacritty config!"),
10	}
11
12	paintln!(Colors::Rgb(129, 69, 231), "But does your terminal support TrueColor?");
13	paintln!(Colors::RgbBold(129, 69, 231), "And does it support Bold Colors???");
14}
Source

pub fn save(appname: &str, file: &str, data: impl AsRef<[u8]>) -> Result<()>

Save config file

Saves to an app’s config file. Note that this will override the file!

Examples found in repository?
examples/config.rs (line 11)
9fn main() -> io::Result<()> {
10	paintln!(Colors::Blue, "Saving test file...");
11	Cfg::save(APP, FILE, PAYLOAD)?;
12
13	paintln!(Colors::Cyan, "Reading test file...");
14	let text = Cfg::read(APP, FILE)?;
15
16	println!("Test file: {text}");
17	assert_eq!(text, PAYLOAD.to_owned());
18	Ok(())
19}
Source

pub fn global_path(appname: &str, file: &str) -> PathBuf

Global config file path

Returns the formatted path to an app’s global config file.

Source

pub fn global_read(appname: &str, file: &str) -> Result<String>

Read global config

Reads the global config file of an app into a String. Will most likely be parsed, e.g. via Serde.

Source

pub fn global_save( appname: &str, file: &str, data: impl AsRef<[u8]>, ) -> Result<()>

Save global

Saves to an app’s global config file. Note that this will override the file!

Auto Trait Implementations§

§

impl Freeze for Cfg

§

impl RefUnwindSafe for Cfg

§

impl Send for Cfg

§

impl Sync for Cfg

§

impl Unpin for Cfg

§

impl UnwindSafe for Cfg

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, 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.