Struct Describer

Source
pub struct Describer { /* private fields */ }
Expand description

Describer holds descriptions of files and directories.

§Types of Descriptions

  • Specific description: A string mapped to a path describing a file or directory. When describe is called this will be retrieved as is.
  • Pattern description: A string mapped to a directory’s path describing a child of the directory. When description of a child is wanted, the pattern is retrieved. In patterns, a wildcard is interpreted as a place holder for child’s name, and are replaced by the name when retreived.

If a string can be described using both a pattern and a specific description, the specific description will be favoured.

§Examples

// Create a mutable describer.
let mut describer = def::Describer::new();

// Map a description to a given path.
describer.add_description("path/to/directory", "This is an empty directory.");

// Map a pattern to a given path. The pattern applies to the path's
// children. "*" works as a placeholder and will be replaced by the
// child's name.
describer.add_pattern("parent/directory", "* is a child of parent/directory.");

// The description is retrieved as is.
assert_eq!(
    describer.describe("path/to/directory"),
    Some("This is an empty directory.".to_string())
);

// "*" is replaced with "test".
assert_eq!(
    describer.describe("parent/directory/test"),
    Some("test is a child of parent/directory.".to_string())
);

// Despite having a pattern mapped to it, the pattern only applies to
// its children.
assert_eq!(describer.describe("parent/directory"), None);

Implementations§

Source§

impl Describer

Source

pub fn new() -> Describer

Create and return a new empty describer.

Source

pub fn new_with( d: HashMap<String, String>, p: HashMap<String, String>, ) -> Describer

Create and return a new describer using given HashMaps.

§Arguments
  • d - A map of descriptions.
  • p - A map of patterns.
Source

pub fn new_from_json(json: &str) -> Result<Describer, Error>

Create and return a new describer using the given JSON value.

§Arguments
  • json - A string representing a JSON value that can be deserialized into a Describer. An error is returned if the JSON string can’t be deserialized.
Source

pub fn describe(&self, path: &str) -> Option<String>

Return a description of the given path or None if no description exists. The descriptions map is checked for a description first, if none is found, then the patterns map is checked.

Source

pub fn add_description(&mut self, path: &str, desc: &str)

Add a description to the descriptions map.

Source

pub fn add_pattern(&mut self, path: &str, desc: &str)

Add a pattern to the patterns map.

Source

pub fn to_json(&self, pretty: bool) -> Result<String, Error>

Return a string JSON representation of this Describer. This is subsequently written to a file to be re-loaded on next run.

§Arguments
  • pretty - If true, return a “pretty” JSON string.

Trait Implementations§

Source§

impl Debug for Describer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Describer

Source§

fn default() -> Describer

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Describer

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Describer

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,