aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/method.rs
blob: 73829b091f82b0104de5fc65b5e3aab81261418b (plain)
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
use anyhow::bail;
use std::{fmt::Display, str::FromStr};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Method {
    Register,
    Invite,
    Ack,
    Option,
    Cancel,
    Bye,
}

impl Display for Method {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Method::Register => "REGISTER",
            Method::Invite => "INVITE",
            Method::Ack => "ACK",
            Method::Option => "OPTION",
            Method::Cancel => "CANCEL",
            Method::Bye => "BYE",
        })
    }
}
impl FromStr for Method {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "REGISTER" => Method::Register,
            "INVITE" => Method::Invite,
            "ACK" => Method::Ack,
            "OPTION" => Method::Option,
            "CANCEL" => Method::Cancel,
            "BYE" => Method::Bye,
            _ => bail!("unknown method"),
        })
    }
}