use std::collections::BTreeMap;
use crate::{LocalizedString, TroxValueError};
#[derive(Debug, Clone, PartialEq)]
pub struct AnnotatedLocalizedString<T> {
localized: LocalizedString,
annotations: BTreeMap<String, T>,
}
impl<T> AnnotatedLocalizedString<T> {
pub fn localized(&self) -> &LocalizedString {
&self.localized
}
pub fn annotations(&self) -> &BTreeMap<String, T> {
&self.annotations
}
pub fn into_parts(self) -> (LocalizedString, BTreeMap<String, T>) {
(self.localized, self.annotations)
}
}
impl LocalizedString {
pub fn annotate<I, K, T>(
self,
annotations: I,
) -> Result<AnnotatedLocalizedString<T>, TroxValueError>
where
I: IntoIterator<Item = (K, T)>,
K: Into<String>,
{
let mut by_name = BTreeMap::new();
for (name, annotation) in annotations {
let name = name.into();
if by_name.insert(name.clone(), annotation).is_some() {
return Err(TroxValueError::new(
"trox.duplicate-annotation",
format!("placeholder annotation `{name}` was supplied more than once"),
));
}
}
if let Some(name) = by_name.keys().find(|name| {
!self.arguments().contains_key(name.as_str())
&& !self
.ron_template_arguments()
.is_some_and(|schemas| schemas.contains_key(name.as_str()))
}) {
return Err(TroxValueError::new(
"trox.unknown-annotation",
format!("annotation `{name}` does not name a declared placeholder"),
));
}
Ok(AnnotatedLocalizedString {
localized: self,
annotations: by_name,
})
}
}