use crate::utils::to_kebab_ascii_strict;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RealmInfo {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
pub version: String,
}
impl RealmInfo {
#[allow(clippy::should_implement_trait)]
pub fn default() -> Self {
Self {
name: "default".to_string(),
code: Some("default".to_string()),
version: "1.0".to_string(),
}
}
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
let name_str = name.into();
let code = Some(to_kebab_ascii_strict(&name_str));
Self {
name: name_str,
code,
version: version.into(),
}
}
pub fn new_with_code(
name: impl Into<String>,
code: impl Into<String>,
version: impl Into<String>,
) -> Self {
Self {
name: name.into(),
code: Some(code.into()),
version: version.into(),
}
}
pub fn canonical_code(&self) -> String {
self.code
.clone()
.unwrap_or_else(|| to_kebab_ascii_strict(&self.name))
}
pub fn matches(&self, other: &RealmInfo) -> bool {
self.canonical_code() == other.canonical_code() && self.version == other.version
}
}
impl Default for RealmInfo {
fn default() -> Self {
Self {
name: "default".to_string(),
code: Some("default".to_string()),
version: "1.0".to_string(),
}
}
}