From 8de1e3213084da3fc14f22cedb9335c11d856954 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 5 Oct 2017 15:46:40 -0700 Subject: [PATCH] Add 'Config::get_string' to retrieve 'String' from extras. --- lib/src/config/config.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/src/config/config.rs b/lib/src/config/config.rs index fa06363a..d00ccc75 100644 --- a/lib/src/config/config.rs +++ b/lib/src/config/config.rs @@ -597,7 +597,7 @@ impl Config { self.secret_key.inner() } - /// Attempts to retrieve the extra named `name` as a string. + /// Attempts to retrieve the extra named `name` as a borrowed string. /// /// # Errors /// @@ -621,6 +621,29 @@ impl Config { val.as_str().ok_or_else(|| self.bad_type(name, val.type_str(), "a string")) } + /// Attempts to retrieve the extra named `name` as an owned string. + /// + /// # Errors + /// + /// If an extra with `name` doesn't exist, returns an `Err` of `NotFound`. + /// If an extra with `name` _does_ exist but is not a string, returns a + /// `BadType` error. + /// + /// # Example + /// + /// ```rust + /// use rocket::config::{Config, Environment}; + /// + /// let config = Config::build(Environment::Staging) + /// .extra("my_extra", "extra_value") + /// .unwrap(); + /// + /// assert_eq!(config.get_string("my_extra"), Ok("extra_value".to_string())); + /// ``` + pub fn get_string(&self, name: &str) -> Result { + self.get_str(name).map(|s| s.to_string()) + } + /// Attempts to retrieve the extra named `name` as an integer. /// /// # Errors