Standardize methods returning OneOrMany<&'a AnyString>

This commit is contained in:
asonix 2021-03-08 18:14:51 -06:00
parent 77c890800c
commit 706c4b251a
3 changed files with 47 additions and 4 deletions

View file

@ -1,4 +1,8 @@
# Unreleased
- Update summary and content to return `OneOrMany<&'a AnyString>`
- Implement as_single_xsd_string and as_single_rdf_lang_string for `OneOrMany<&'a AnyString>`
# 0.7.0-alpha.10
- Fix extraction of `image` and `icon` when creating Objects from AnyBase

View file

@ -544,11 +544,11 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// println!("{:?}", content);
/// }
/// ```
fn content<'a>(&'a self) -> Option<&'a OneOrMany<AnyString>>
fn content<'a>(&'a self) -> Option<OneOrMany<&'a AnyString>>
where
Kind: 'a,
{
self.object_ref().content.as_ref()
self.object_ref().content.as_ref().map(|o| o.as_ref())
}
/// Set the content for the current object
@ -665,11 +665,11 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// println!("{:?}", summary);
/// }
/// ```
fn summary<'a>(&'a self) -> Option<&'a OneOrMany<AnyString>>
fn summary<'a>(&'a self) -> Option<OneOrMany<&'a AnyString>>
where
Kind: 'a,
{
self.object_ref().summary.as_ref()
self.object_ref().summary.as_ref().map(|o| o.as_ref())
}
/// Set the summary for the current object

View file

@ -308,6 +308,45 @@ impl OneOrMany<AnyString> {
}
}
impl OneOrMany<&AnyString> {
/// Try to borrow a single String from the current object
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::primitives::{OneOrMany, AnyString};
/// # let string = OneOrMany::<AnyString>::from_xsd_string("Hey");
/// string
/// .as_single_xsd_string()
/// .ok_or(anyhow::Error::msg("Wrong string type"))?;
/// # Ok(())
/// # }
/// ```
pub fn as_single_xsd_string(&self) -> Option<&str> {
self.as_one()
.and_then(|any_string| any_string.as_xsd_string())
}
/// Try to borrow a single RdfLangString from the current object
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::primitives::{OneOrMany, RdfLangString};
/// # let string = OneOrMany::from_rdf_lang_string(RdfLangString {
/// # value: "hi".into(),
/// # language: "en".into(),
/// # });
/// string
/// .as_single_rdf_lang_string()
/// .ok_or(anyhow::Error::msg("Wrong string type"))?;
/// # Ok(())
/// # }
/// ```
pub fn as_single_rdf_lang_string(&self) -> Option<&RdfLangString> {
self.as_one()
.and_then(|any_string| any_string.as_rdf_lang_string())
}
}
impl From<&str> for AnyString {
fn from(s: &str) -> Self {
AnyString::from_xsd_string(s.to_owned())