Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ValueParserFactory for MountOption #288

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ edition = "2021"
readme = "README.md"
authors = ["Christopher Berner <christopherberner@gmail.com>"]
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]
Expand All @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions src/mnt/mount_options.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -86,6 +88,42 @@ impl MountOption {
}
}

impl FromStr for MountOption {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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<Self::Value, Error> {
Ok(MountOption::from_str(&value.to_string_lossy()))
}
}

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());
Expand Down
Loading