diff --git a/CHANGELOG.md b/CHANGELOG.md index 2db4f95..14bd840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Unreleased +# 0.7.0-alpha.19 +- Add `.as_str()`, `.language()` and `impl AsRef` for `AnyString` + # 0.7.0-alpha.18 - expose `Either` type publicly diff --git a/Cargo.toml b/Cargo.toml index 736b1f2..41c7a59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams" description = "A set of core types and traits for activitystreams data" -version = "0.7.0-alpha.18" +version = "0.7.0-alpha.19" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/README.md b/README.md index 1c61176..87dc363 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ _A set of Traits and Types that make up the ActivityStreams and ActivityPub spec First, add ActivityStreams to your dependencies ```toml [dependencies] -activitystreams = "0.7.0-alpha.11" +activitystreams = "0.7.0-alpha.19" ``` ### Types @@ -164,14 +164,14 @@ If you want to make a function that manipulates an Activity, but not a normal ob bound the function like so: ```rust -use activitystreams::{base::BaseExt, context, markers::Activity, uri}; +use activitystreams::{base::BaseExt, context, markers::Activity, iri}; fn manipulator(mut activity: T) -> Result<(), anyhow::Error> where T: Activity + BaseExt, { activity - .set_id(uri!("https://example.com")) + .set_id(iri!("https://example.com")) .set_context(context()); Ok(()) } @@ -205,7 +205,7 @@ use activitystreams::{ context, object::{ApObject, Video}, prelude::*, - uri, + iri, }; fn main() -> Result<(), anyhow::Error> { @@ -213,12 +213,12 @@ fn main() -> Result<(), anyhow::Error> { video .set_context(context()) - .set_id(uri!("https://example.com/@example/lions")) + .set_id(iri!("https://example.com/@example/lions")) .set_media_type("video/webm".parse()?) - .set_url(uri!("https://example.com/@example/lions/video.webm")) + .set_url(iri!("https://example.com/@example/lions/video.webm")) .set_summary("A cool video") .set_duration("PT4M20S".parse()?) - .set_shares(uri!("https://example.com/@example/lions/video.webm#shares")); + .set_shares(iri!("https://example.com/@example/lions/video.webm#shares")); println!("Video, {:#?}", video); diff --git a/src/lib.rs b/src/lib.rs index 261d44a..501fd15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ //! First, add ActivityStreams to your dependencies //! ```toml //! [dependencies] -//! activitystreams = "0.7.0-alpha.11" +//! activitystreams = "0.7.0-alpha.19" //! ``` //! //! ### Types diff --git a/src/primitives/any_string.rs b/src/primitives/any_string.rs index cf618ee..9fdc320 100644 --- a/src/primitives/any_string.rs +++ b/src/primitives/any_string.rs @@ -155,6 +155,56 @@ impl AnyString { { self.0 = Either::Right(string.into()); } + + /// Borrow the inner str + /// + /// ```rust + /// use activitystreams::primitives::{AnyString, RdfLangString}; + /// let any_string = AnyString::from_xsd_string("hi"); + /// + /// assert_eq!(any_string.as_str(), "hi"); + /// + /// let any_string = AnyString::from_rdf_lang_string(RdfLangString { + /// value: "hi".into(), + /// language: "en".into(), + /// }); + /// + /// assert_eq!(any_string.as_str(), "hi"); + /// ``` + pub fn as_str(&self) -> &str { + match self.0 { + Either::Left(ref s) => s, + Either::Right(ref lang_str) => &lang_str.value, + } + } + + /// Borrow the inner language + /// + /// ```rust + /// use activitystreams::primitives::{AnyString, RdfLangString}; + /// let any_string = AnyString::from_xsd_string("hi"); + /// + /// assert_eq!(any_string.language(), None); + /// + /// let any_string = AnyString::from_rdf_lang_string(RdfLangString { + /// value: "hi".into(), + /// language: "en".into(), + /// }); + /// + /// assert_eq!(any_string.language(), Some("en")); + /// ``` + pub fn language(&self) -> Option<&str> { + match self.0 { + Either::Left(_) => None, + Either::Right(ref lang_str) => Some(&lang_str.language), + } + } +} + +impl AsRef for AnyString { + fn as_ref(&self) -> &str { + self.as_str() + } } impl OneOrMany {