Struct rocket::config::Config
[−]
[src]
pub struct Config { pub address: String, pub port: usize, pub log_level: LoggingLevel, pub env: Environment, // some fields omitted }
The core configuration structure.
Fields
address: String
The address to serve on.
port: usize
The port to serve on.
log_level: LoggingLevel
How much information to log.
env: Environment
The environment that this configuration corresponds to.
Methods
impl Config
[src]
fn default_for(env: Environment, filepath: &str) -> Result<Config>
Returns the default configuration for the environment env
given that
the configuration was stored at filepath
. If filepath
is not an
absolute path, an Err
of ConfigError::BadFilePath
is returned.
fn set(&mut self, name: &str, val: &Value) -> Result<()>
Sets the configuration val
for the name
entry. If the name
is one
of "address", "port", "session_key", or "log" (the "default" values),
the appropriate value in the self
Config structure is set. Otherwise,
the value is stored as an extra
.
For each of the default values, the following Value
variant is
expected. If a different variant is supplied, a BadType
Err
is
returned:
- address: String
- port: Integer
- session_key: String (192-bit base64)
- log: String
fn take_session_key(&self) -> Option<String>
Moves the session key string out of the self
Config, if there is one.
Because the value is moved out, subsequent calls will result in a return
value of None
.
Example
use rocket::config::{Config, Environment, Value}; // Create a new config with a session key. let key = "adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz".to_string(); let config = Config::default_for(Environment::Staging, "/custom").unwrap() .session_key(key.clone()); // Get the key for the first time. let session_key = config.take_session_key(); assert_eq!(session_key, Some(key.clone())); // Try to get the key again. let session_key_again = config.take_session_key(); assert_eq!(session_key_again, None);
fn extras<'a>(&'a self) -> impl Iterator<Item=(&'a str, &'a Value)>
Returns an iterator over the names and values of all of the extras in
the self
Config.
fn get_str<'a>(&'a self, name: &str) -> Result<&'a str>
Attempts to retrieve the extra named name
as a string. If an extra
with that name doesn't exist, returns an Err
of NotFound
. If an
extra with that name does exist but is not a string, returns a BadType
error.
fn get_int(&self, name: &str) -> Result<i64>
Attempts to retrieve the extra named name
as an integer. If an extra
with that name doesn't exist, returns an Err
of NotFound
. If an
extra with that name does exist but is not an integer, returns a
BadType
error.
fn get_bool(&self, name: &str) -> Result<bool>
Attempts to retrieve the extra named name
as a boolean. If an extra
with that name doesn't exist, returns an Err
of NotFound
. If an
extra with that name does exist but is not a boolean, returns a
BadType
error.
fn get_float(&self, name: &str) -> Result<f64>
Attempts to retrieve the extra named name
as a float. If an extra
with that name doesn't exist, returns an Err
of NotFound
. If an
extra with that name does exist but is not a float, returns a
BadType
error.
fn root(&self) -> &Path
Returns the path at which the configuration file for self
is stored.
For instance, if the configuration file is at /tmp/Rocket.toml
, the
path /tmp
is returned.
fn address(self, var: String) -> Self
Sets the address
in self
to var
and returns the structure.
fn port(self, var: usize) -> Self
Sets the port
in self
to var
and returns the structure.
fn log_level(self, var: LoggingLevel) -> Self
Sets the log_level
in self
to var
and returns the structure.
fn session_key(self, var: String) -> Self
Sets the session_key
in self
to var
and returns the structure.
fn env(self, var: Environment) -> Self
Sets the env
in self
to var
and returns the structure.
fn extra(self, name: &str, value: &Value) -> Self
Adds an extra configuration parameter with name
and value
to self
and returns the structure.