use crate::domain::asset::error::AssetError;
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AssetReference {
schema: String,
name: String,
}
impl AssetReference {
pub fn new(schema: impl Into<String>, name: impl Into<String>) -> Self {
Self {
schema: schema.into(),
name: name.into(),
}
}
pub fn schema(&self) -> &str {
&self.schema
}
pub fn name(&self) -> &str {
&self.name
}
}
impl fmt::Display for AssetReference {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.schema, self.name)
}
}
impl FromStr for AssetReference {
type Err = AssetError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split('.').collect();
if parts.len() != 2 {
return Err(AssetError::InvalidReference(s.to_string()));
}
let schema = parts[0].trim();
let name = parts[1].trim();
if schema.is_empty() || name.is_empty() {
return Err(AssetError::InvalidReference(s.to_string()));
}
Ok(Self {
schema: schema.to_string(),
name: name.to_string(),
})
}
}