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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::error::{HttpSigError, HttpSigResult};
use sfv::BareItem;

/* ---------------------------------------------------------------- */
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
/// Http message component identifier
pub enum HttpMessageComponentName {
  /// Http field component, which is in the form of `<field_name>` without being wrapped by double quotations
  HttpField(String),
  /// Derived component
  Derived(DerivedComponentName),
}

impl TryFrom<&BareItem> for HttpMessageComponentName {
  type Error = HttpSigError;
  fn try_from(value: &BareItem) -> HttpSigResult<Self> {
    match value {
      BareItem::String(name) => {
        if name.starts_with('@') {
          Ok(Self::Derived(DerivedComponentName::from(name.as_str())))
        } else {
          Ok(Self::HttpField(name.to_string()))
        }
      }
      _ => Err(HttpSigError::InvalidComponentName(format!(
        "Invalid http message component name: {value:?}"
      ))),
    }
  }
}

impl std::fmt::Display for HttpMessageComponentName {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::HttpField(val) => write!(f, "\"{}\"", val),
      Self::Derived(val) => write!(f, "\"{}\"", val),
    }
  }
}

/* ---------------------------------------------------------------- */
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
/// Derive components from http message, which is expressed as @method, @path, @authority, etc. in @signature-params
/// https://datatracker.ietf.org/doc/html/rfc9421#name-derived-components
pub enum DerivedComponentName {
  Method,
  TargetUri,
  Authority,
  Scheme,
  RequestTarget,
  Path,
  Query,
  QueryParam,
  Status,
  SignatureParams,
}
impl AsRef<str> for DerivedComponentName {
  fn as_ref(&self) -> &str {
    match self {
      Self::Method => "@method",
      Self::TargetUri => "@target-uri",
      Self::Authority => "@authority",
      Self::Scheme => "@scheme",
      Self::RequestTarget => "@request-target",
      Self::Path => "@path",
      Self::Query => "@query",
      Self::QueryParam => "@query-param",
      Self::Status => "@status",
      Self::SignatureParams => "@signature-params",
    }
  }
}
impl From<DerivedComponentName> for String {
  fn from(val: DerivedComponentName) -> Self {
    val.as_ref().to_string()
  }
}
impl From<&str> for DerivedComponentName {
  fn from(val: &str) -> Self {
    match val {
      "@method" => Self::Method,
      "@target-uri" => Self::TargetUri,
      "@authority" => Self::Authority,
      "@scheme" => Self::Scheme,
      "@request-target" => Self::RequestTarget,
      "@path" => Self::Path,
      "@query" => Self::Query,
      "@query-param" => Self::QueryParam,
      "@status" => Self::Status,
      "@signature-params" => Self::SignatureParams,
      _ => panic!("Invalid derived component: {}", val),
    }
  }
}

impl std::fmt::Display for DerivedComponentName {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", AsRef::<str>::as_ref(self))
  }
}