[][src]Struct figment::value::magic::RelativePathBuf

pub struct RelativePathBuf { /* fields omitted */ }
This is supported on crate feature 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]

This is supported on crate feature 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]

This is supported on crate feature 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]

This is supported on crate feature 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]

impl Debug for RelativePathBuf[src]

impl<'de> Deserialize<'de> for RelativePathBuf[src]

impl<P: AsRef<Path>> From<P> for RelativePathBuf[src]

impl Magic for RelativePathBuf[src]

impl PartialEq<RelativePathBuf> for RelativePathBuf[src]

impl Serialize for RelativePathBuf[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,