[−][src]Struct figment::value::magic::RelativePathBuf
magic
only.A PathBuf
that knows the path of the file it was configured in, if any.
Paths in configuration files are often desired to be relative to the
configuration file itself. For example, a path of a/b.html
configured in a
file /var/config.toml
might be desired to resolve as /var/a/b.html
. This
type makes this possible by simply delcaring the configuration value's type
as RelativePathBuf
.
Example
use std::path::Path; use serde::Deserialize; use figment::{Figment, value::magic::RelativePathBuf, Jail}; use figment::providers::{Env, Format, Toml}; #[derive(Debug, PartialEq, Deserialize)] struct Config { path: RelativePathBuf, } Jail::expect_with(|jail| { // When a path is declared in a file and deserialized as // `RelativePathBuf`, `relative()` will be relative to the file. jail.create_file("Config.toml", r#"path = "a/b/c.html""#)?; let c: Config = Figment::from(Toml::file("Config.toml")).extract()?; assert_eq!(c.path.original(), Path::new("a/b/c.html")); assert_eq!(c.path.relative(), jail.directory().join("a/b/c.html")); // If a path is declared elsewhere, the "relative" path is the original. jail.set_env("PATH", "a/b/c.html"); let c: Config = Figment::from(Toml::file("Config.toml")) .merge(Env::raw().only(&["PATH"])) .extract()?; assert_eq!(c.path.original(), Path::new("a/b/c.html")); assert_eq!(c.path.relative(), Path::new("a/b/c.html")); // Absolute paths remain unchanged. jail.create_file("Config.toml", r#"path = "/var/c.html""#); let c: Config = Figment::from(Toml::file("Config.toml")).extract()?; assert_eq!(c.path.original(), Path::new("/var/c.html")); assert_eq!(c.path.relative(), Path::new("/var/c.html")); Ok(()) });
Implementations
impl RelativePathBuf
[src]
pub fn original(&self) -> &Path
[src]
magic
only.Returns the path as it was declared, without modification.
Example
use std::path::Path; use figment::{Figment, value::magic::RelativePathBuf, Jail}; use figment::providers::{Format, Toml}; #[derive(Debug, PartialEq, serde::Deserialize)] struct Config { path: RelativePathBuf, } Jail::expect_with(|jail| { jail.create_file("Config.toml", r#"path = "hello.html""#)?; let c: Config = Figment::from(Toml::file("Config.toml")).extract()?; assert_eq!(c.path.original(), Path::new("hello.html")); Ok(()) });
pub fn relative(&self) -> PathBuf
[src]
magic
only.Returns this path relative to the file it was delcared in, if any. Returns the original if this path was not declared in a file or if the path has a root.
Example
use std::path::Path; use figment::{Figment, value::magic::RelativePathBuf, Jail}; use figment::providers::{Env, Format, Toml}; #[derive(Debug, PartialEq, serde::Deserialize)] struct Config { path: RelativePathBuf, } Jail::expect_with(|jail| { jail.create_file("Config.toml", r#"path = "hello.html""#)?; let c: Config = Figment::from(Toml::file("Config.toml")).extract()?; assert_eq!(c.path.relative(), jail.directory().join("hello.html")); jail.set_env("PATH", r#"hello.html"#); let c: Config = Figment::from(Env::raw().only(&["PATH"])).extract()?; assert_eq!(c.path.relative(), Path::new("hello.html")); Ok(()) });
pub fn metadata_path(&self) -> Option<&Path>
[src]
magic
only.Returns the path to the file this path was declared in, if any.
Example
use std::path::Path; use figment::{Figment, value::magic::RelativePathBuf, Jail}; use figment::providers::{Env, Format, Toml}; #[derive(Debug, PartialEq, serde::Deserialize)] struct Config { path: RelativePathBuf, } Jail::expect_with(|jail| { jail.create_file("Config.toml", r#"path = "hello.html""#)?; let c: Config = Figment::from(Toml::file("Config.toml")).extract()?; assert_eq!(c.path.metadata_path().unwrap(), jail.directory().join("Config.toml")); jail.set_env("PATH", r#"hello.html"#); let c: Config = Figment::from(Env::raw().only(&["PATH"])).extract()?; assert_eq!(c.path.metadata_path(), None); Ok(()) });
Trait Implementations
impl Clone for RelativePathBuf
[src]
fn clone(&self) -> RelativePathBuf
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for RelativePathBuf
[src]
impl<'de> Deserialize<'de> for RelativePathBuf
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl<P: AsRef<Path>> From<P> for RelativePathBuf
[src]
fn from(path: P) -> RelativePathBuf
[src]
impl Magic for RelativePathBuf
[src]
const NAME: &'static str
[src]
const FIELDS: &'static [&'static str]
[src]
fn deserialize_from<'de: 'c, 'c, V: Visitor<'de>>(
de: ConfiguredValueDe<'c>,
visitor: V
) -> Result<V::Value, Error>
[src]
de: ConfiguredValueDe<'c>,
visitor: V
) -> Result<V::Value, Error>
impl PartialEq<RelativePathBuf> for RelativePathBuf
[src]
impl Serialize for RelativePathBuf
[src]
Auto Trait Implementations
impl RefUnwindSafe for RelativePathBuf
impl Send for RelativePathBuf
impl Sync for RelativePathBuf
impl Unpin for RelativePathBuf
impl UnwindSafe for RelativePathBuf
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,