relay_base_schema/
project.rs

1//! Contains [`ProjectKey`] and [`ProjectId`] types and necessary traits implementations.
2//!
3//! - [`ProjectId`] is the unique identifier of a Sentry project. Currently, it is just a wrapper
4//!   over `u64` and should be considered as implementations details, as it can change in the future.
5//! - [`ProjectKey`] is a byte array (`[u8; 32]`) and represents a DSN to identify and authenticate
6//!   a project at Sentry.
7
8use std::error::Error;
9use std::fmt;
10use std::str::FromStr;
11
12use serde::{Deserialize, Serialize};
13
14/// Raised if a project ID cannot be parsed from a string.
15#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
16pub enum ParseProjectIdError {
17    /// Raised if the value is not an integer in the supported range.
18    InvalidValue,
19    /// Raised if an empty value is parsed.
20    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/// The unique identifier of a Sentry project.
35#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
36pub struct ProjectId(u64);
37
38impl ProjectId {
39    /// Creates a new project ID from its numeric value.
40    #[inline]
41    pub fn new(id: u64) -> Self {
42        Self(id)
43    }
44
45    /// Returns the numeric value of this project ID.
46    #[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/// An error parsing [`ProjectKey`].
74#[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/// The public key used in a DSN to identify and authenticate a project at Sentry.
86///
87/// Project keys are always 32-character hexadecimal strings.
88#[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    /// Parses a `ProjectKey` from a string.
95    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    /// Parses a `ProjectKey` from a string with flags.
106    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    /// Returns the bytes of the project key.
113    pub fn as_bytes(&self) -> &[u8; 32] {
114        &self.0
115    }
116
117    /// Returns the string representation of the project key.
118    #[inline]
119    pub fn as_str(&self) -> &str {
120        // Safety: The string is already validated to be of length 32 and valid ASCII when
121        // constructing `ProjectKey`.
122        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}