From b60f00a860ef5c1ac244279351abc5290c8bf9c2 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 8 Jul 2024 16:31:27 -0600 Subject: [PATCH 1/2] add "clap" feature --- Cargo.toml | 8 +++++++- src/mnt/mount_options.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 70c6dfe8..2899ebe7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,12 @@ edition = "2021" readme = "README.md" authors = ["Christopher Berner "] keywords = ["fuse", "filesystem", "system", "bindings"] -categories = ["external-ffi-bindings", "api-bindings", "filesystem", "os::unix-apis"] +categories = [ + "external-ffi-bindings", + "api-bindings", + "filesystem", + "os::unix-apis", +] build = "build.rs" [dependencies] @@ -22,6 +27,7 @@ serde = { version = "1.0.102", features = ["std", "derive"], optional = true } smallvec = "1.6.1" zerocopy = { version = "0.7", features = ["derive"] } nix = { version = "0.28.0", features = ["fs", "user"] } +clap = { version = "4.4", optional = true } [dev-dependencies] env_logger = "0.11.3" diff --git a/src/mnt/mount_options.rs b/src/mnt/mount_options.rs index 1778d28e..3db15717 100644 --- a/src/mnt/mount_options.rs +++ b/src/mnt/mount_options.rs @@ -1,5 +1,7 @@ +use std::convert::Infallible; use std::io; use std::io::ErrorKind; +use std::str::FromStr; use std::{collections::HashSet, ffi::OsStr}; /// Mount options accepted by the FUSE filesystem type @@ -86,6 +88,45 @@ impl MountOption { } } +impl FromStr for MountOption { + type Err = Infallible; + fn from_str(s: &str) -> Result { + Ok(Self::from_str(s)) + } +} + +#[cfg(feature = "clap")] +mod clap { + use ::clap::builder::{TypedValueParser, ValueParserFactory}; + use ::clap::error::ErrorKind; + use ::clap::{Arg, Command, Error}; + + use super::*; + #[derive(Clone, Copy, Debug)] + pub struct MountOptionParser; + impl TypedValueParser for MountOptionParser { + type Value = MountOption; + fn parse_ref( + &self, + _cmd: &Command, + _arg: Option<&Arg>, + value: &std::ffi::OsStr, + ) -> Result { + value + .to_string_lossy() + .parse() + .map_err(|e| clap::Error::raw(ErrorKind::ValueValidation, e)) + } + } + + impl ValueParserFactory for MountOption { + type Parser = MountOptionParser; + fn value_parser() -> Self::Parser { + MountOptionParser + } + } +} + pub fn check_option_conflicts(options: &[MountOption]) -> Result<(), io::Error> { let mut options_set = HashSet::new(); options_set.extend(options.iter().cloned()); From 312c16d7f01311bfeebcf700326c30f6382ad966 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 8 Jul 2024 16:44:04 -0600 Subject: [PATCH 2/2] use pure from_str --- src/mnt/mount_options.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/mnt/mount_options.rs b/src/mnt/mount_options.rs index 3db15717..f87b8cb8 100644 --- a/src/mnt/mount_options.rs +++ b/src/mnt/mount_options.rs @@ -112,10 +112,7 @@ mod clap { _arg: Option<&Arg>, value: &std::ffi::OsStr, ) -> Result { - value - .to_string_lossy() - .parse() - .map_err(|e| clap::Error::raw(ErrorKind::ValueValidation, e)) + Ok(MountOption::from_str(&value.to_string_lossy())) } }