Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
styling: 🔨 move symlink arrow to styling mod and use styling
fix #409
- Loading branch information
Showing
4 changed files
with
106 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use super::Configurable; | ||
|
||
use crate::config_file::Config; | ||
|
||
use clap::ArgMatches; | ||
use yaml_rust::Yaml; | ||
|
||
/// The flag showing how to display symbolic arrow. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct SymlinkArrow(String); | ||
|
||
impl Configurable<Self> for SymlinkArrow { | ||
/// `SymlinkArrow` can not be configured by [ArgMatches] | ||
/// | ||
/// Return `None` | ||
fn from_arg_matches(_: &ArgMatches) -> Option<Self> { | ||
None | ||
} | ||
/// Get a potential `SymlinkArrow` value from a [Config]. | ||
/// | ||
/// If the Config's [Yaml] contains the [String](Yaml::String) value pointed to by | ||
/// "symlink-arrow", this returns its value as the value of the `SymlinkArrow`, in a [Some]. | ||
/// Otherwise this returns [None]. | ||
fn from_config(config: &Config) -> Option<Self> { | ||
if let Some(yaml) = &config.yaml { | ||
match &yaml["styling"]["symlink-arrow"] { | ||
Yaml::BadValue => None, | ||
Yaml::String(value) => Some(SymlinkArrow(value.to_string())), | ||
_ => { | ||
config.print_wrong_type_warning("symlink-arrow", "string"); | ||
None | ||
} | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// The default value for the `SymlinkArrow` is `\u{21d2}(⇒)` | ||
impl Default for SymlinkArrow { | ||
fn default() -> Self { | ||
Self(String::from("\u{21d2}")) // ⇒ | ||
} | ||
} | ||
|
||
use std::fmt; | ||
impl fmt::Display for SymlinkArrow { | ||
// This trait requires `fmt` with this exact signature. | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::config_file::Config; | ||
use crate::flags::Configurable; | ||
|
||
use yaml_rust::YamlLoader; | ||
|
||
use super::SymlinkArrow; | ||
#[test] | ||
fn test_symlink_arrow_from_config_utf8() { | ||
let yaml_string = "styling: | ||
symlink-arrow: ↹"; | ||
let yaml = YamlLoader::load_from_str(yaml_string).unwrap()[0].clone(); | ||
assert_eq!( | ||
Some(SymlinkArrow(String::from("\u{21B9}"))), | ||
SymlinkArrow::from_config(&Config::with_yaml(yaml)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_symlink_arrow_from_config_type_error() { | ||
let yaml_string = "styling: | ||
symlink-arrow: false"; | ||
let yaml = YamlLoader::load_from_str(yaml_string).unwrap()[0].clone(); | ||
assert_eq!(None, SymlinkArrow::from_config(&Config::with_yaml(yaml))); | ||
} | ||
|
||
#[test] | ||
fn test_symlink_arrow_from_args_none() { | ||
use clap::App; | ||
assert_eq!( | ||
None, | ||
SymlinkArrow::from_arg_matches(&App::new("lsd").get_matches()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_symlink_arrow_default() { | ||
assert_eq!( | ||
SymlinkArrow(String::from("\u{21d2}")), | ||
SymlinkArrow::default() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_symlink_display() { | ||
assert_eq!("⇒", format!("{}", SymlinkArrow::default())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters