ddmw_client/
types.rs

1//! Various types used when communicating with core servers.
2
3pub mod node;
4
5use std::fmt;
6use std::str::FromStr;
7
8use crate::err::Error;
9
10
11/// Reference an account; with the option to implicitly reference self.
12pub enum OptObjRef {
13  Current,
14  Id(i64),
15  Name(String)
16}
17
18/// Reference an account, either by numeric identifier or name.
19pub enum ObjRef {
20  Id(i64),
21  Name(String)
22}
23
24impl FromStr for ObjRef {
25  type Err = Error;
26
27  /// Parse a `&str` and turn it into an `ObjRef`.
28  fn from_str(o: &str) -> Result<Self, Self::Err> {
29    match o.parse::<i64>() {
30      Ok(id) => Ok(ObjRef::Id(id)),
31      Err(_) => Ok(ObjRef::Name(o.to_string()))
32    }
33  }
34}
35
36
37#[derive(Clone, Debug)]
38pub enum AppChannel {
39  Num(u8),
40  Name(String)
41}
42
43impl FromStr for AppChannel {
44  type Err = Error;
45
46  /// Parse a `&str` and turn it into an `AppChannel`.
47  fn from_str(ch: &str) -> Result<Self, Self::Err> {
48    match ch.parse::<u8>() {
49      Ok(ch) => Ok(AppChannel::Num(ch)),
50      Err(_) => Ok(AppChannel::Name(ch.to_string()))
51    }
52  }
53}
54
55impl fmt::Display for AppChannel {
56  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57    match self {
58      AppChannel::Num(ch) => {
59        write!(f, "{}", ch)
60      }
61      AppChannel::Name(ch) => {
62        write!(f, "{}", ch)
63      }
64    }
65  }
66}
67
68// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :