Implement OneOrMany<&AnyString>::to_as_string()

This commit is contained in:
Kitaiti Makoto 2022-02-13 00:52:41 +09:00
parent e407d58ee9
commit a80a95d471

View file

@ -4,6 +4,7 @@ use activitystreams::{
iri_string::types::IriString,
markers::Activity as Activity07,
object::{ApObject, Article},
primitives::{AnyString, OneOrMany},
unparsed::UnparsedMutExt,
};
use activitystreams_ext::{Ext1, UnparsedExtension};
@ -407,6 +408,27 @@ where
pub type LicensedArticle = Ext1<ApObject<Article>, Licensed07>;
pub trait ToAsString {
fn to_as_string(&self) -> Option<String>;
}
impl ToAsString for OneOrMany<&AnyString> {
fn to_as_string(&self) -> Option<String> {
if let Some(prop) = self.as_one() {
prop.as_xsd_string()
.or_else(|| prop.as_rdf_lang_string().map(|ls| ls.value.as_str()))
} else if let Some(props) = self.as_many() {
props.iter().next().and_then(|prop| {
prop.as_xsd_string()
.or_else(|| prop.as_rdf_lang_string().map(|ls| ls.value.as_str()))
})
} else {
None
}
.map(|s| s.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;