Struct jupiter::config::Config

source ·
pub struct Config { /* private fields */ }
Expand description

Provides access to the system configuration.

Most probably a config instance is installed by the Builder and can be obtained via platform.require::<Config>(). Note that it is highly recommended to register a change listener by calling Config::notifier() as we expect all components to pick up config changes without restarting the application.

Implementations§

source§

impl Config

source

pub fn new(file: &str) -> Self

Creates a new config reading the given file.

Note that this will not install a change listener. This is only done by the install function.

source

pub fn notifier(&self) -> ChangeNotifier

Obtains a change notifier which receives a message once the config changed.

source

pub fn current(&self) -> Handle

Obtains a handle to the currently loaded configuration.

Note that this is a fairly efficient operation but still provides some overhead. Therefore this shouldn’t be placed in an inner loop.

source

pub async fn load(&self) -> Result<()>

Forces the config to read the underlying file.

Note that this is normally called by the framework and should not be invoked manually.

source

pub async fn store(&self, config: &str) -> Result<()>

Loads a configuration from the given string instead of a file.

This is intended to be used in test environments where we cannot / do not want to load a config file from disk.

§Example
let config = Config::new("test_config.yml");

// Remove any left over file...
std::fs::remove_file("test_config.yml");

// Write a config file...
assert_eq!(config.store("
server:
    port: 12345
").await.is_ok(), true);

// Load it back and verify its contents (in a fully running Apollo, this would
// happen automatically via the config watcher...)
assert_eq!(config.load().await.is_ok(), true);
assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345);

// Writing an invalid config file is prevented...
assert_eq!(config.store("server: \"test").await.is_err(), true);

// Therefore the original config is still present...
assert_eq!(config.load().await.is_ok(), true);
assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345);
source

pub fn load_from_string( &self, data: &str, last_modified: Option<SystemTime> ) -> Result<()>

Loads a configuration from the given string instead of a file.

This is intended to be used in test environments where we cannot / do not want to load a config file from disk.

§Example
use std::time::Instant;
let config = Config::new("somefile.yml");
config.load_from_string("
server:
    port: 12345
", None);

assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345);

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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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