use super::Severity;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum NamesError {
#[error("Name is empty or exceeds the 253 character limit")]
NameTooLong,
#[error("Label is empty or exceeds the 63 character limit")]
LabelTooLong,
#[error("Name contains invalid characters (only lowercase letters, digits, and internal hyphens allowed)")]
InvalidCharacter,
#[error("Name is a protected public utility name (e.g., localhost, test)")]
ReservedName,
#[error("Name is a protected infrastructure name (e.g., seed, explorer)")]
InfrastructureName,
#[error("Name has an invalid Top-Level Domain")]
InvalidTLD,
#[error("Only apex names are allowed (subnames must be managed by the apex owner)")]
NotAnApexName,
}
impl NamesError {
pub fn code(&self) -> &'static str {
match self {
Self::NameTooLong => "KIN-NAM-001",
Self::LabelTooLong => "KIN-NAM-002",
Self::InvalidCharacter => "KIN-NAM-003",
Self::ReservedName => "KIN-NAM-004",
Self::InfrastructureName => "KIN-NAM-005",
Self::InvalidTLD => "KIN-NAM-006",
Self::NotAnApexName => "KIN-NAM-007",
}
}
pub fn error_type_uri(&self) -> String {
format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
}
pub fn severity(&self) -> Severity {
Severity::Warning
}
pub fn is_retryable(&self) -> bool {
false
}
pub fn user_message(&self) -> String {
match self {
Self::NameTooLong => {
"The name is empty or exceeds the 253-character limit.".to_string()
}
Self::LabelTooLong => {
"A label within the name exceeds the 63-character limit.".to_string()
}
Self::InvalidCharacter => {
"The name contains invalid characters. Only lowercase letters, digits, and internal hyphens are allowed.".to_string()
}
Self::ReservedName => {
"This name is a permanently protected public utility name.".to_string()
}
Self::InfrastructureName => {
"This name is reserved for critical network infrastructure.".to_string()
}
Self::InvalidTLD => {
"The name does not end with a valid network TLD.".to_string()
}
Self::NotAnApexName => {
"Only apex names (e.g. 'example.kin') can be registered directly.".to_string()
}
}
}
}