use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum L10nError {
Decompression(String),
InvalidUtf8(std::string::FromUtf8Error),
InvalidLanguage {
lang: String,
reason: String,
},
Builtins(String),
ResourceParse(Vec<String>),
AddResource(Vec<String>),
MessageNotFound {
id: String,
},
MessageHasNoValue {
id: String,
},
AttributeNotFound {
id: String,
attribute: String,
},
Format {
id: String,
attribute: Option<String>,
errors: Vec<String>,
},
}
impl fmt::Display for L10nError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decompression(e) => write!(f, "Could not decompress the .ftl data: {e}"),
Self::InvalidUtf8(e) => write!(f, "The .ftl content is not valid UTF-8: {e}"),
Self::InvalidLanguage { lang, reason } => {
write!(f, "Invalid language identifier '{lang}': {reason}")
}
Self::Builtins(e) => write!(f, "Could not register the Fluent builtins: {e}"),
Self::ResourceParse(errors) => {
write!(f, "Could not parse the .ftl resource:")?;
for e in errors {
write!(f, "\n {e}")?;
}
Ok(())
}
Self::AddResource(errors) => {
write!(f, "Could not add the .ftl resource to the bundle:")?;
for e in errors {
write!(f, "\n {e}")?;
}
Ok(())
}
Self::MessageNotFound { id } => write!(f, "No message '{id}' found"),
Self::MessageHasNoValue { id } => {
write!(f, "Message '{id}' has no value of its own")
}
Self::AttributeNotFound { id, attribute } => {
write!(f, "Message '{id}' has no attribute '{attribute}'")
}
Self::Format {
id,
attribute,
errors,
} => {
match attribute {
Some(a) => write!(f, "Could not format attribute '{a}' of message '{id}':")?,
None => write!(f, "Could not format message '{id}':")?,
}
for e in errors {
write!(f, "\n {e}")?;
}
Ok(())
}
}
}
}
impl std::error::Error for L10nError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidUtf8(e) => Some(e),
_ => None,
}
}
}