Add convenience methods to AnyString
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Aode (lion) 2022-02-13 14:24:07 -06:00
parent b7f178f2b6
commit 3123fff5a8
5 changed files with 62 additions and 9 deletions

View file

@ -1,5 +1,8 @@
# Unreleased
# 0.7.0-alpha.19
- Add `.as_str()`, `.language()` and `impl AsRef<str>` for `AnyString`
# 0.7.0-alpha.18
- expose `Either` type publicly

View file

@ -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 <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -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<T, Kind>(mut activity: T) -> Result<(), anyhow::Error>
where
T: Activity + BaseExt<Kind>,
{
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);

View file

@ -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

View file

@ -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<str> for AnyString {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl OneOrMany<AnyString> {