use crate::error::ValidationError;
use rovo::schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(try_from = "String", into = "String")]
pub struct TargetRepo(String);
impl TargetRepo {
pub fn new(value: String) -> Result<Self, ValidationError> {
Self::try_from(value)
}
pub fn into_inner(self) -> String {
self.0
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for TargetRepo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
crate::derive_sqlx_traits!(TargetRepo);
impl TryFrom<String> for TargetRepo {
type Error = ValidationError;
fn try_from(value: String) -> Result<Self, Self::Error> {
let parts: Vec<&str> = value.split('/').collect();
match parts.as_slice() {
[owner, repo] if !owner.is_empty() && !repo.is_empty() => Ok(TargetRepo(value)),
_ => {
tracing::warn!(
"Validation failed for TargetRepo: {}. Must be in owner/repo format",
value
);
Err(ValidationError::InvalidValue(format!(
"Target repository must be in owner/repo format: {}",
value
)))
}
}
}
}
impl From<TargetRepo> for String {
fn from(val: TargetRepo) -> Self {
val.0
}
}