Allow &str in places

This commit is contained in:
asonix 2020-06-03 13:41:30 -05:00
parent d37714ff57
commit 05a2bdc98d
6 changed files with 54 additions and 36 deletions

View File

@ -518,12 +518,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new()); /// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams_new::prelude::*; /// use activitystreams_new::prelude::*;
/// ///
/// person.set_preferred_username("user123".into()); /// person.set_preferred_username("user123");
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
fn set_preferred_username(&mut self, string: String) -> &mut Self { fn set_preferred_username<T>(&mut self, string: T) -> &mut Self
self.ap_actor_mut().preferred_username = Some(string); where
T: Into<String>,
{
self.ap_actor_mut().preferred_username = Some(string.into());
self self
} }
@ -547,7 +550,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// ```rust /// ```rust
/// # use activitystreams_new::{actor::{ApActor, Person}, context}; /// # use activitystreams_new::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new()); /// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_preferred_username("hey".into()); /// # person.set_preferred_username("hey");
/// use activitystreams_new::prelude::*; /// use activitystreams_new::prelude::*;
/// ///
/// assert!(person.preferred_username().is_some()); /// assert!(person.preferred_username().is_some());

View File

@ -15,7 +15,7 @@
//! .set_id("https://example.com".parse()?) //! .set_id("https://example.com".parse()?)
//! .set_context(context()) //! .set_context(context())
//! .add_context(security()) //! .add_context(security())
//! .set_name("Hello".to_owned()); //! .set_name("Hello");
//! //!
//! let any_base = video.into_any_base()?; //! let any_base = video.into_any_base()?;
//! //!
@ -454,7 +454,7 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # /// #
/// video.set_name("hi".to_owned()); /// video.set_name("hi");
/// ``` /// ```
fn set_name<T>(&mut self, name: T) -> &mut Self fn set_name<T>(&mut self, name: T) -> &mut Self
where where
@ -473,7 +473,7 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # /// #
/// video.set_many_names(vec!["hi".to_owned(), "hey".to_owned()]); /// video.set_many_names(vec!["hi", "hey"]);
/// ``` /// ```
fn set_many_names<I, T>(&mut self, items: I) -> &mut Self fn set_many_names<I, T>(&mut self, items: I) -> &mut Self
where where
@ -495,8 +495,8 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # /// #
/// video /// video
/// .add_name("hi".to_owned()) /// .add_name("hi")
/// .add_name("hey".to_owned()); /// .add_name("hey");
/// ``` /// ```
fn add_name<T>(&mut self, name: T) -> &mut Self fn add_name<T>(&mut self, name: T) -> &mut Self
where where
@ -535,7 +535,7 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// use activitystreams_new::prelude::*; /// use activitystreams_new::prelude::*;
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # video.set_name("hi".to_owned()); /// # video.set_name("hi");
/// # /// #
/// ///
/// assert!(video.name().is_some()); /// assert!(video.name().is_some());
@ -1800,3 +1800,9 @@ impl From<String> for OneOrMany<AnyBase> {
Self::from_xsd_string(xsd_string) Self::from_xsd_string(xsd_string)
} }
} }
impl From<&str> for OneOrMany<AnyBase> {
fn from(xsd_string: &str) -> Self {
Self::from_xsd_string(xsd_string.to_owned())
}
}

View File

@ -216,7 +216,7 @@
//! .set_id("https://example.com/@example/lions".parse()?) //! .set_id("https://example.com/@example/lions".parse()?)
//! .set_media_type("video/webm".parse()?) //! .set_media_type("video/webm".parse()?)
//! .set_url("https://example.com/@example/lions/video.webm".parse::<XsdAnyUri>()?) //! .set_url("https://example.com/@example/lions/video.webm".parse::<XsdAnyUri>()?)
//! .set_summary("A cool video".to_owned()) //! .set_summary("A cool video")
//! .set_duration("PT4M20S".parse()?) //! .set_duration("PT4M20S".parse()?)
//! .set_shares("https://example.com/@example/lions/video.webm#shares".parse()?); //! .set_shares("https://example.com/@example/lions/video.webm#shares".parse()?);
//! //!
@ -315,7 +315,7 @@ pub mod markers {
//! where //! where
//! T: Activity + BaseExt<Kind>, //! T: Activity + BaseExt<Kind>,
//! { //! {
//! some_type.set_name("hi".to_owned()); //! some_type.set_name("hi");
//! //!
//! some_type //! some_type
//! } //! }
@ -351,8 +351,8 @@ pub mod prelude {
//! ); //! );
//! person //! person
//! .set_outbox("http:/localhost:8080/outbox".parse()?) //! .set_outbox("http:/localhost:8080/outbox".parse()?)
//! .set_name("Demo Account".to_owned()) //! .set_name("Demo Account")
//! .set_preferred_username("demo".to_owned()) //! .set_preferred_username("demo")
//! .set_id("https://localhost:8080/actor".parse()?) //! .set_id("https://localhost:8080/actor".parse()?)
//! .set_url("https://localhost:8080/actor".parse::<XsdAnyUri>()?); //! .set_url("https://localhost:8080/actor".parse::<XsdAnyUri>()?);
//! //!
@ -369,7 +369,7 @@ pub mod prelude {
//! .set_id("http://localhost:8080/video.webm".parse()?) //! .set_id("http://localhost:8080/video.webm".parse()?)
//! .set_url("http://localhost:8080/video.webm".parse::<XsdAnyUri>()?) //! .set_url("http://localhost:8080/video.webm".parse::<XsdAnyUri>()?)
//! .set_media_type("video/webm".parse()?) //! .set_media_type("video/webm".parse()?)
//! .set_summary("A cool video".to_owned()) //! .set_summary("A cool video")
//! .set_preview(preview.into_any_base()?) //! .set_preview(preview.into_any_base()?)
//! .set_duration("PT4M20S".parse()?) //! .set_duration("PT4M20S".parse()?)
//! .set_shares("http://localhost:8080/video.webm#shares".parse()?); //! .set_shares("http://localhost:8080/video.webm#shares".parse()?);

View File

@ -546,7 +546,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video.set_content("hi".to_owned()); /// video.set_content("hi");
/// ``` /// ```
fn set_content<T>(&mut self, content: T) -> &mut Self fn set_content<T>(&mut self, content: T) -> &mut Self
where where
@ -565,10 +565,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video.set_many_contents(vec![ /// video.set_many_contents(vec!["hi", "hello"]);
/// "hi".to_owned(),
/// "hello".to_owned(),
/// ]);
/// ``` /// ```
fn set_many_contents<I, T>(&mut self, items: I) -> &mut Self fn set_many_contents<I, T>(&mut self, items: I) -> &mut Self
where where
@ -590,8 +587,8 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video /// video
/// .add_content("hi".to_owned()) /// .add_content("hi")
/// .add_content("hello".to_owned()); /// .add_content("hello");
/// ``` /// ```
fn add_content<T>(&mut self, content: T) -> &mut Self fn add_content<T>(&mut self, content: T) -> &mut Self
where where
@ -629,7 +626,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// ```rust /// ```rust
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # video.set_content("content".to_owned()); /// # video.set_content("content");
/// # /// #
/// use activitystreams_new::prelude::*; /// use activitystreams_new::prelude::*;
/// ///
@ -670,7 +667,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video.set_summary("hi".to_owned()); /// video.set_summary("hi");
/// ``` /// ```
fn set_summary<T>(&mut self, summary: T) -> &mut Self fn set_summary<T>(&mut self, summary: T) -> &mut Self
where where
@ -689,10 +686,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video.set_many_summaries(vec![ /// video.set_many_summaries(vec![ "hi", "hello"]);
/// "hi".to_owned(),
/// "hello".to_owned(),
/// ]);
/// ``` /// ```
fn set_many_summaries<I, T>(&mut self, items: I) -> &mut Self fn set_many_summaries<I, T>(&mut self, items: I) -> &mut Self
where where
@ -714,8 +708,8 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// ///
/// video /// video
/// .add_summary("hi".to_owned()) /// .add_summary("hi")
/// .add_summary("hello".to_owned()); /// .add_summary("hello");
/// ``` /// ```
fn add_summary<T>(&mut self, summary: T) -> &mut Self fn add_summary<T>(&mut self, summary: T) -> &mut Self
where where
@ -753,7 +747,7 @@ pub trait ObjectExt<Kind>: AsObject<Kind> {
/// ```rust /// ```rust
/// # use activitystreams_new::object::Video; /// # use activitystreams_new::object::Video;
/// # let mut video = Video::new(); /// # let mut video = Video::new();
/// # video.set_summary("summary".to_owned()); /// # video.set_summary("summary");
/// # /// #
/// use activitystreams_new::prelude::*; /// use activitystreams_new::prelude::*;
/// ///

View File

@ -759,6 +759,12 @@ impl<T> From<Vec<T>> for OneOrMany<T> {
} }
} }
impl From<&str> for AnyString {
fn from(s: &str) -> Self {
AnyString::from_xsd_string(s.to_owned())
}
}
impl From<String> for AnyString { impl From<String> for AnyString {
fn from(s: String) -> Self { fn from(s: String) -> Self {
AnyString::from_xsd_string(s) AnyString::from_xsd_string(s)
@ -771,6 +777,12 @@ impl From<RdfLangString> for AnyString {
} }
} }
impl From<&str> for OneOrMany<AnyString> {
fn from(s: &str) -> Self {
OneOrMany::<AnyString>::from_xsd_string(s.to_owned())
}
}
impl From<String> for OneOrMany<AnyString> { impl From<String> for OneOrMany<AnyString> {
fn from(s: String) -> Self { fn from(s: String) -> Self {
OneOrMany::<AnyString>::from_xsd_string(s) OneOrMany::<AnyString>::from_xsd_string(s)

View File

@ -206,8 +206,11 @@
//! /// //! ///
//! /// In a real application, this might take a different type, such as RSA's RSAPublicKey, or //! /// In a real application, this might take a different type, such as RSA's RSAPublicKey, or
//! /// OpenSSL's or Ring's version //! /// OpenSSL's or Ring's version
//! fn set_key_pem(&mut self, pem: String) -> &mut Self { //! fn set_key_pem<T>(&mut self, pem: T) -> &mut Self
//! self.public_key_mut().public_key.public_key_pem = pem; //! where
//! T: Into<String>,
//! {
//! self.public_key_mut().public_key.public_key_pem = pem.into();
//! self //! self
//! } //! }
//! } //! }
@ -236,11 +239,11 @@
//! extended_person //! extended_person
//! .set_kind(PersonType) //! .set_kind(PersonType)
//! .set_id("https://example.com/user".parse()?) //! .set_id("https://example.com/user".parse()?)
//! .set_name("Demo User".to_string()) //! .set_name("Demo User")
//! .set_preferred_username("user".to_string()) //! .set_preferred_username("user")
//! .set_outbox("https://example.com/user/outbox".parse()?) //! .set_outbox("https://example.com/user/outbox".parse()?)
//! .set_key_pem( //! .set_key_pem(
//! "------ BEGIN PUBLIC KEY ------\nasdfasdfasdfasdfasdfasdf...".to_string() //! "------ BEGIN PUBLIC KEY ------\nasdfasdfasdfasdfasdfasdf..."
//! ) //! )
//! .set_key_owner("https://example.com/user".parse()?) //! .set_key_owner("https://example.com/user".parse()?)
//! .set_key_id("https://example.com/user#main-key".parse()?); //! .set_key_id("https://example.com/user#main-key".parse()?);