From a80a95d471100151bcda97a996d782f1c8859e52 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 13 Feb 2022 00:52:41 +0900 Subject: [PATCH] Implement OneOrMany<&AnyString>::to_as_string() --- plume-common/src/activity_pub/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index 82514cd0..374ef9bf 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -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, Licensed07>; +pub trait ToAsString { + fn to_as_string(&self) -> Option; +} + +impl ToAsString for OneOrMany<&AnyString> { + fn to_as_string(&self) -> Option { + 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::*;