kanidm_proto/
cli.rs

1use std::str::FromStr;
2
3use clap::{builder::PossibleValue, ValueEnum};
4use serde::Deserialize;
5
6#[derive(Debug, Copy, Clone, Deserialize)]
7pub enum OpType {
8    Read,
9    Write,
10}
11
12impl FromStr for OpType {
13    type Err = String;
14
15    fn from_str(s: &str) -> Result<Self, Self::Err> {
16        match s.to_lowercase().as_str() {
17            "read" => Ok(OpType::Read),
18            "write" => Ok(OpType::Write),
19            _ => Err(format!("Invalid OpType: {}", s)),
20        }
21    }
22}
23
24impl ValueEnum for OpType {
25    fn value_variants<'a>() -> &'a [Self] {
26        &[Self::Read, Self::Write]
27    }
28
29    fn to_possible_value(&self) -> Option<PossibleValue> {
30        Some(PossibleValue::from(self.as_str()))
31    }
32}
33
34impl OpType {
35    pub fn as_str(&self) -> &'static str {
36        match self {
37            OpType::Read => "read",
38            OpType::Write => "write",
39        }
40    }
41}