Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: use ValueEnum for SortBy
Fintan Halpenny committed 6 months ago
commit 51605c3f6220e9e005ad49c238b7a3f641229873
parent 7ad83286842950e29b22be0ded43f539e01e8dff
2 files changed +5 -33
modified crates/radicle-cli/src/commands/inbox.rs
@@ -38,7 +38,6 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
            reverse,
            show_unknown,
        } => {
-
            let sort_by = sort_by.unwrap_or_default();
            let reverse = reverse.unwrap_or(false);
            let show_unknown = show_unknown.unwrap_or(false);

modified crates/radicle-cli/src/commands/inbox/args.rs
@@ -1,6 +1,6 @@
use std::{fmt::Display, str::FromStr};

-
use clap::{Parser, Subcommand};
+
use clap::{Parser, Subcommand, ValueEnum};
use radicle::{node::notifications::NotificationId, prelude::RepoId};

pub(crate) const ABOUT: &str = "Manage your Radicle notifications";
@@ -41,10 +41,8 @@ pub(crate) enum Command {
    /// List notifications
    List {
        /// Sort by column
-
        #[arg(long)]
-
        #[arg(value_parser = SortByParser)]
-
        #[arg(default_value = "timestamp")]
-
        sort_by: Option<SortBy>,
+
        #[arg(long, value_enum, default_value_t)]
+
        sort_by: SortBy,

        /// Reverse the list
        #[arg(short, long)]
@@ -71,14 +69,14 @@ pub(crate) enum Command {
impl Default for Command {
    fn default() -> Self {
        Command::List {
-
            sort_by: None,
+
            sort_by: SortBy::default(),
            reverse: None,
            show_unknown: None,
        }
    }
}

-
#[derive(Clone, Default, Debug)]
+
#[derive(ValueEnum, Clone, Default, Debug)]
pub enum SortBy {
    Id,
    #[default]
@@ -106,31 +104,6 @@ impl FromStr for SortBy {
    }
}

-
#[derive(Clone, Debug)]
-
struct SortByParser;
-

-
impl clap::builder::TypedValueParser for SortByParser {
-
    type Value = SortBy;
-

-
    fn parse_ref(
-
        &self,
-
        cmd: &clap::Command,
-
        arg: Option<&clap::Arg>,
-
        value: &std::ffi::OsStr,
-
    ) -> Result<Self::Value, clap::Error> {
-
        <SortBy as std::str::FromStr>::from_str.parse_ref(cmd, arg, value)
-
    }
-

-
    fn possible_values(
-
        &self,
-
    ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
-
        use clap::builder::PossibleValue;
-
        Some(Box::new(
-
            [PossibleValue::new("id"), PossibleValue::new("timestamp")].into_iter(),
-
        ))
-
    }
-
}
-

mod error {
    #[derive(Debug, thiserror::Error)]
    #[error("'{0}' is not a valid sort by column")]