relay_base_schema/
project.rs1use std::error::Error;
9use std::fmt;
10use std::str::FromStr;
11
12use serde::{Deserialize, Serialize};
13
14#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
16pub enum ParseProjectIdError {
17 InvalidValue,
19 EmptyValue,
21}
22
23impl fmt::Display for ParseProjectIdError {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self {
26 ParseProjectIdError::InvalidValue => f.write_str("invalid value for project id"),
27 ParseProjectIdError::EmptyValue => f.write_str("empty or missing project id"),
28 }
29 }
30}
31
32impl Error for ParseProjectIdError {}
33
34#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
36pub struct ProjectId(u64);
37
38impl ProjectId {
39 #[inline]
41 pub fn new(id: u64) -> Self {
42 Self(id)
43 }
44
45 #[inline]
47 pub fn value(self) -> u64 {
48 self.0
49 }
50}
51
52impl fmt::Display for ProjectId {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 write!(f, "{}", self.value())
55 }
56}
57
58impl FromStr for ProjectId {
59 type Err = ParseProjectIdError;
60
61 fn from_str(s: &str) -> Result<ProjectId, ParseProjectIdError> {
62 if s.is_empty() {
63 return Err(ParseProjectIdError::EmptyValue);
64 }
65
66 match s.parse::<u64>() {
67 Ok(val) => Ok(ProjectId::new(val)),
68 Err(_) => Err(ParseProjectIdError::InvalidValue),
69 }
70 }
71}
72
73#[derive(Clone, Copy, Debug)]
75pub struct ParseProjectKeyError;
76
77impl fmt::Display for ParseProjectKeyError {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 write!(f, "invalid project key")
80 }
81}
82
83impl std::error::Error for ParseProjectKeyError {}
84
85#[derive(Clone, Copy, Eq, Hash, Ord, PartialOrd, PartialEq)]
89pub struct ProjectKey([u8; 32]);
90
91relay_common::impl_str_serde!(ProjectKey, "a project key string");
92
93impl ProjectKey {
94 pub fn parse(key: &str) -> Result<Self, ParseProjectKeyError> {
96 if key.len() != 32 || !key.is_ascii() {
97 return Err(ParseProjectKeyError);
98 }
99
100 let mut project_key = Self(Default::default());
101 project_key.0.copy_from_slice(key.as_bytes());
102 Ok(project_key)
103 }
104
105 pub fn parse_with_flags(key: &str) -> Result<(Self, Vec<&str>), ParseProjectKeyError> {
107 let mut iter = key.split('.');
108 let key = ProjectKey::parse(iter.next().ok_or(ParseProjectKeyError)?)?;
109 Ok((key, iter.collect()))
110 }
111
112 pub fn as_bytes(&self) -> &[u8; 32] {
114 &self.0
115 }
116
117 #[inline]
119 pub fn as_str(&self) -> &str {
120 unsafe { std::str::from_utf8_unchecked(&self.0) }
123 }
124}
125
126impl fmt::Debug for ProjectKey {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "ProjectKey(\"{}\")", self.as_str())
129 }
130}
131
132impl fmt::Display for ProjectKey {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 self.as_str().fmt(f)
135 }
136}
137
138impl FromStr for ProjectKey {
139 type Err = ParseProjectKeyError;
140
141 fn from_str(s: &str) -> Result<Self, Self::Err> {
142 Self::parse(s)
143 }
144}