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
impl Describer
Sourcepub fn new_with(
d: HashMap<String, String>,
p: HashMap<String, String>,
) -> Describer
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.
Sourcepub fn new_from_json(json: &str) -> Result<Describer, Error>
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.
Sourcepub fn describe(&self, path: &str) -> Option<String>
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.
Sourcepub fn add_description(&mut self, path: &str, desc: &str)
pub fn add_description(&mut self, path: &str, desc: &str)
Add a description to the descriptions map.
Sourcepub fn add_pattern(&mut self, path: &str, desc: &str)
pub fn add_pattern(&mut self, path: &str, desc: &str)
Add a pattern to the patterns map.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Describer
impl<'de> Deserialize<'de> for Describer
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Describer
impl RefUnwindSafe for Describer
impl Send for Describer
impl Sync for Describer
impl Unpin for Describer
impl UnwindSafe for Describer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more