divviup_client/
protocol.rs

1use serde::{Deserialize, Serialize};
2use std::{
3    error::Error,
4    fmt::{self, Display, Formatter},
5    str::FromStr,
6};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9pub enum Protocol {
10    #[serde(rename = "DAP-09")]
11    Dap09,
12}
13
14impl AsRef<str> for Protocol {
15    fn as_ref(&self) -> &str {
16        match self {
17            Self::Dap09 => "DAP-09",
18        }
19    }
20}
21
22#[derive(Debug)]
23pub struct UnrecognizedProtocol(String);
24impl Display for UnrecognizedProtocol {
25    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
26        f.write_fmt(format_args!("{} was not a recognized protocol", self.0))
27    }
28}
29impl Error for UnrecognizedProtocol {}
30impl FromStr for Protocol {
31    type Err = UnrecognizedProtocol;
32    fn from_str(s: &str) -> Result<Self, Self::Err> {
33        match &*s.to_lowercase() {
34            "dap-09" => Ok(Self::Dap09),
35            unrecognized => Err(UnrecognizedProtocol(unrecognized.to_string())),
36        }
37    }
38}