commit_bridge/domain/
repo_url.rs1use crate::error::ValidationError;
4use rovo::schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use validator::Validate;
7
8#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
10#[serde(try_from = "String", into = "String")]
11pub struct RepoUrl(String);
12
13impl std::fmt::Display for RepoUrl {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 write!(f, "{}", self.0)
16 }
17}
18
19impl RepoUrl {
20 pub fn new(value: String) -> Result<Self, ValidationError> {
22 Self::try_from(value)
23 }
24
25 pub fn into_inner(self) -> String {
27 self.0
28 }
29
30 pub fn as_str(&self) -> &str {
32 &self.0
33 }
34}
35
36impl AsRef<str> for RepoUrl {
37 fn as_ref(&self) -> &str {
38 &self.0
39 }
40}
41
42impl TryFrom<String> for RepoUrl {
43 type Error = ValidationError;
44
45 fn try_from(value: String) -> Result<Self, Self::Error> {
46 #[derive(Validate)]
47 struct RepoUrlValidator {
48 #[validate(url)]
49 val: String,
50 }
51
52 let validator = RepoUrlValidator { val: value.clone() };
53 if let Err(e) = validator.validate() {
54 tracing::warn!("Validation failed for RepoUrl: {}. Error: {}", value, e);
55 return Err(ValidationError::InvalidValue(format!(
56 "Invalid URL format: {}",
57 value
58 )));
59 }
60
61 if !value.contains("github.com") {
62 tracing::warn!(
63 "Validation failed for RepoUrl: {}. Must be a github.com URL",
64 value
65 );
66 return Err(ValidationError::InvalidValue(format!(
67 "URL must be a github.com URL: {}",
68 value
69 )));
70 }
71
72 Ok(RepoUrl(value))
73 }
74}
75
76impl From<RepoUrl> for String {
77 fn from(val: RepoUrl) -> Self {
78 val.0
79 }
80}
81
82crate::derive_sqlx_traits!(RepoUrl);