Skip to main content

commit_bridge/domain/
api_version.rs

1//! Domain type to represent a GitHub API version in YYYY-MM-DD format.
2
3use crate::error::ValidationError;
4use chrono::NaiveDate;
5use serde::{Deserialize, Serialize};
6
7/// The GitHub API version.
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(try_from = "String", into = "String")]
10pub struct ApiVersion(String);
11
12impl ApiVersion {
13    /// Create a new `ApiVersion`.
14    pub fn new(value: String) -> Result<Self, ValidationError> {
15        Self::try_from(value)
16    }
17
18    /// Get the inner string.
19    pub fn into_inner(self) -> String {
20        self.0
21    }
22}
23
24impl TryFrom<String> for ApiVersion {
25    type Error = ValidationError;
26
27    fn try_from(value: String) -> Result<Self, Self::Error> {
28        if let Err(e) = NaiveDate::parse_from_str(&value, "%Y-%m-%d") {
29            tracing::warn!("Validation failed for ApiVersion: {}. Error: {}", value, e);
30            return Err(ValidationError::InvalidValue(format!(
31                "Invalid API version format (expected YYYY-MM-DD): {}",
32                value
33            )));
34        }
35
36        Ok(ApiVersion(value))
37    }
38}
39
40impl From<ApiVersion> for String {
41    fn from(val: ApiVersion) -> Self {
42        val.0
43    }
44}
45
46impl std::fmt::Display for ApiVersion {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}", self.0)
49    }
50}