1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use anyhow::Result;
use clap::ArgEnum;
use log::debug;

use printnanny_api_client::models;
use printnanny_services::config::PrintNannyConfig;
use printnanny_services::printnanny_api::ApiService;

#[derive(Copy, Eq, PartialEq, Debug, Clone, clap::ArgEnum)]
pub enum DeviceAction {
    Get,
    Setup,
}

impl DeviceAction {
    pub fn possible_values() -> impl Iterator<Item = clap::PossibleValue<'static>> {
        DeviceAction::value_variants()
            .iter()
            .filter_map(clap::ArgEnum::to_possible_value)
    }
}

impl std::str::FromStr for DeviceAction {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        for variant in Self::value_variants() {
            if variant.to_possible_value().unwrap().matches(s, false) {
                return Ok(*variant);
            }
        }
        Err(format!("Invalid variant: {}", s))
    }
}

pub struct DeviceCmd {
    pub action: DeviceAction,
    pub service: ApiService,
}
impl DeviceCmd {
    pub async fn new(action: DeviceAction, config: PrintNannyConfig) -> Result<Self> {
        let service = ApiService::new(config)?;
        Ok(Self { service, action })
    }
    pub async fn handle(&self) -> Result<String> {
        let result = match self.action {
            DeviceAction::Get => self.service.device_retrieve_hostname().await?,
            DeviceAction::Setup => self.service.device_setup().await?,
        };
        debug!("Success action={:?} result={:?}", self.action, result);
        Ok(self.service.to_string_pretty::<models::Device>(result)?)
    }
}