Skip to main content

commit_bridge/domain/
event_type.rs

1//! Domain type to represent a GitHub's `repository_dispatch` `event_type`.
2
3use crate::error::ValidationError;
4use rovo::schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use validator::Validate;
7
8/// The GitHub's `repository_dispatch` `event_type`.
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10#[serde(try_from = "String", into = "String")]
11pub struct EventType(String);
12
13impl EventType {
14    /// Create a new `EventType`.
15    pub fn new(value: String) -> Result<Self, ValidationError> {
16        Self::try_from(value)
17    }
18
19    /// Get the inner string.
20    pub fn into_inner(self) -> String {
21        self.0
22    }
23
24    /// Get a reference to the inner string.
25    pub fn as_str(&self) -> &str {
26        &self.0
27    }
28}
29
30crate::derive_sqlx_traits!(EventType);
31
32impl std::fmt::Display for EventType {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "{}", self.0)
35    }
36}
37
38impl TryFrom<String> for EventType {
39    type Error = ValidationError;
40
41    fn try_from(value: String) -> Result<Self, Self::Error> {
42        #[derive(Validate)]
43        struct EventTypeValidator {
44            #[validate(length(max = 100))]
45            val: String,
46        }
47
48        let validator = EventTypeValidator { val: value.clone() };
49        if let Err(e) = validator.validate() {
50            tracing::warn!("Validation failed for EventType: {}. Error: {}", value, e);
51            return Err(ValidationError::InvalidValue(format!(
52                "Event type must be at most 100 characters long. Got: {}",
53                value.len()
54            )));
55        }
56
57        Ok(EventType(value))
58    }
59}
60
61impl From<EventType> for String {
62    fn from(val: EventType) -> Self {
63        val.0
64    }
65}