| 1 |
//! A type for absolute paths or default paths, and a clap parser. |
| 2 |
|
| 3 |
use std::path::PathBuf; |
| 4 |
|
| 5 |
use anyhow::{Result, anyhow}; |
| 6 |
|
| 7 |
/// Either an absolute path, or a default path. |
| 8 |
/// |
| 9 |
/// Even though this type is homomorphic to [`Option<PathBuf>`], we |
| 10 |
/// need a new type for this, because clap handles [`Option`]s |
| 11 |
/// differently, and we cannot return [`Option<PathBuf>`] from |
| 12 |
/// `TypedValueParser::parse_ref`. |
| 13 |
#[derive(Clone, Debug)] |
| 14 |
pub enum StateDirectory { |
| 15 |
/// An absolute path. |
| 16 |
Absolute(PathBuf), |
| 17 |
|
| 18 |
/// The default path. |
| 19 |
Default, |
| 20 |
|
| 21 |
/// Explicitly disable this state. |
| 22 |
None, |
| 23 |
} |
| 24 |
|
| 25 |
impl StateDirectory { |
| 26 |
/// Returns whether this state has been disabled. |
| 27 |
#[allow(dead_code)] |
| 28 |
pub fn is_none(&self) -> bool { |
| 29 |
matches!(self, StateDirectory::None) |
| 30 |
} |
| 31 |
|
| 32 |
/// Returns the absolute path, or `None` if the default path is to |
| 33 |
/// be used. |
| 34 |
pub fn path(&self) -> Result<Option<PathBuf>> { |
| 35 |
match self { |
| 36 |
StateDirectory::Absolute(p) => Ok(Some(p.clone())), |
| 37 |
StateDirectory::Default => Ok(None), |
| 38 |
StateDirectory::None => Err(anyhow!("state is disabled")), |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// A value parser for absolute directories with explicit default. |
| 44 |
/// |
| 45 |
/// If `default` is given, this parses to `StateDirectory::Default`. |
| 46 |
/// If `none` is given, this parses to `StateDirectory::None`. If an |
| 47 |
/// empty path is given, a hint is displayed to give `default` |
| 48 |
/// instead. |
| 49 |
/// |
| 50 |
/// If a relative path is given, a hint is displayed to use an |
| 51 |
/// absolute path instead. |
| 52 |
#[derive(Clone, Default)] |
| 53 |
pub struct StateDirectoryValueParser {} |
| 54 |
|
| 55 |
impl clap::builder::TypedValueParser for StateDirectoryValueParser { |
| 56 |
type Value = StateDirectory; |
| 57 |
|
| 58 |
fn parse_ref( |
| 59 |
&self, |
| 60 |
cmd: &clap::Command, |
| 61 |
arg: Option<&clap::Arg>, |
| 62 |
value: &std::ffi::OsStr, |
| 63 |
) -> Result<Self::Value, clap::error::Error> { |
| 64 |
use clap::error::*; |
| 65 |
|
| 66 |
if value == "default" { |
| 67 |
return Ok(StateDirectory::Default); |
| 68 |
} |
| 69 |
|
| 70 |
if value == "none" { |
| 71 |
return Ok(StateDirectory::None); |
| 72 |
} |
| 73 |
|
| 74 |
if value.is_empty() { |
| 75 |
let mut err = Error::new(ErrorKind::InvalidValue) |
| 76 |
.with_cmd(cmd); |
| 77 |
if let Some(arg) = arg { |
| 78 |
err.insert(ContextKind::InvalidArg, |
| 79 |
ContextValue::String(arg.to_string())); |
| 80 |
} |
| 81 |
err.insert(ContextKind::InvalidValue, |
| 82 |
ContextValue::String("".into())); |
| 83 |
err.insert(ContextKind::SuggestedValue, |
| 84 |
ContextValue::String("default".into())); |
| 85 |
err.insert(ContextKind::Suggested, |
| 86 |
ContextValue::StyledStrs(vec![ |
| 87 |
"to use the default directory, use 'default'".into(), |
| 88 |
])); |
| 89 |
return Err(err); |
| 90 |
} |
| 91 |
|
| 92 |
let p = PathBuf::from(value); |
| 93 |
|
| 94 |
// Expand `~/foo` to `/home/user/foo`. |
| 95 |
let p = expand_tilde(p)?; |
| 96 |
|
| 97 |
if ! p.is_absolute() { |
| 98 |
let mut err = Error::new(ErrorKind::InvalidValue) |
| 99 |
.with_cmd(cmd); |
| 100 |
if let Some(arg) = arg { |
| 101 |
err.insert(ContextKind::InvalidArg, |
| 102 |
ContextValue::String(arg.to_string())); |
| 103 |
} |
| 104 |
err.insert(ContextKind::InvalidValue, |
| 105 |
ContextValue::String(p.display().to_string())); |
| 106 |
err.insert(ContextKind::Suggested, |
| 107 |
ContextValue::StyledStrs(vec![ |
| 108 |
"must be an absolute path".into(), |
| 109 |
])); |
| 110 |
return Err(err); |
| 111 |
} |
| 112 |
|
| 113 |
Ok(StateDirectory::Absolute(p)) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/// Expands tilde in paths. |
| 118 |
/// |
| 119 |
/// There are at least two crates that do this, shellexpand and |
| 120 |
/// tilde-expand, but both operate on strings, which is the wrong data |
| 121 |
/// type. |
| 122 |
/// |
| 123 |
/// Currently, we only handle `~`, not `~user`. At least shellexpand |
| 124 |
/// doesn't either. |
| 125 |
fn expand_tilde(p: PathBuf) -> clap::error::Result<PathBuf> { |
| 126 |
if p.components().next().map(|c| c.as_os_str() == "~").unwrap_or(false) { |
| 127 |
Ok(dirs::home_dir().ok_or_else( |
| 128 |
|| { |
| 129 |
use clap::error; |
| 130 |
|
| 131 |
// Not great, but we cannot return a rich error here. |
| 132 |
eprintln!("Error: no home directory known for this platform"); |
| 133 |
error::Error::new(error::ErrorKind::Io) |
| 134 |
})? |
| 135 |
.join(p.components().skip(1).collect::<PathBuf>())) |
| 136 |
} else { |
| 137 |
Ok(p) |
| 138 |
} |
| 139 |
} |