file_name.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use crate::options::{flags, OptionsError};
  2. use crate::options::parser::MatchedFlags;
  3. use crate::options::vars::{self, Vars};
  4. use crate::output::file_name::{Options, Classify, ShowIcons};
  5. impl Options {
  6. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  7. let classify = Classify::deduce(matches)?;
  8. let show_icons = ShowIcons::deduce(matches, vars)?;
  9. Ok(Self { classify, show_icons })
  10. }
  11. }
  12. impl Classify {
  13. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  14. let flagged = matches.has(&flags::CLASSIFY)?;
  15. if flagged { Ok(Self::AddFileIndicators) }
  16. else { Ok(Self::JustFilenames) }
  17. }
  18. }
  19. impl ShowIcons {
  20. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  21. if ! matches.has(&flags::ICONS)? {
  22. Ok(Self::Off)
  23. }
  24. else if let Some(columns) = vars.get(vars::EXA_ICON_SPACING).and_then(|s| s.into_string().ok()) {
  25. match columns.parse() {
  26. Ok(width) => Ok(Self::On(width)),
  27. Err(e) => Err(OptionsError::FailedParse(e)),
  28. }
  29. }
  30. else {
  31. Ok(Self::On(1))
  32. }
  33. }
  34. }