Skip to main content

Config

Struct Config 

Source
pub struct Config { /* private fields */ }
Expand description

A key/value configuration file backed by <game_dir>/yog-config/<mod_id>.cfg.

Implementations§

Source§

impl Config

Source

pub fn load(game_dir: &str, mod_id: &str) -> Config

Load (or create) a config file for mod_id under game_dir.

The file lives at <game_dir>/yog-config/<mod_id>.cfg. : and / in mod_id are replaced with _ in the filename.

Source

pub fn get(&self, key: &str) -> Option<&str>

Get a value as a string slice.

Source

pub fn get_or<'a>(&'a self, key: &str, default: &'a str) -> &'a str

Get a string value, or default if the key is absent.

Source

pub fn get_int(&self, key: &str) -> Option<i64>

Get a value parsed as i64.

Source

pub fn get_int_or(&self, key: &str, default: i64) -> i64

Get an integer value, or default if absent or unparseable.

Source

pub fn get_float(&self, key: &str) -> Option<f64>

Get a value parsed as f64.

Source

pub fn get_float_or(&self, key: &str, default: f64) -> f64

Get a float value, or default if absent or unparseable.

Source

pub fn get_bool(&self, key: &str) -> Option<bool>

Get a value parsed as bool.

Truthy: true, yes, 1, on. Falsy: false, no, 0, off.

Source

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

Get a bool value, or default if absent or unrecognised.

Source

pub fn set(&mut self, key: impl Into<String>, value: impl ToString)

Set (or overwrite) a key. Call [save] to persist.

Source

pub fn remove(&mut self, key: &str) -> Option<String>

Remove a key. Returns the old value if present.

Source

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

Returns true if the key exists in the config.

Source

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

Persist the current state to disk. Creates yog-config/ if needed.

Comments from the original file are not preserved; keys are written in alphabetical order for determinism.

Source

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

Save only if the config file does not yet exist (write defaults on first run).

Source

pub fn len(&self) -> usize

Number of stored entries.

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &str)>

Auto Trait Implementations§

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.