From 757df7792dfd4168071d8ca2aee1cd025090bc9b Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 3 Jan 2021 18:11:17 -0600 Subject: [PATCH] Clippy nits, AnyBase::extend --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- src/activity.rs | 15 +++++++++++++++ src/actor.rs | 9 +++++++++ src/base.rs | 32 ++++++++++++++++++++++++++++++++ src/collection.rs | 24 ++++++++++++++++++++++++ src/link.rs | 9 +++++++++ src/object.rs | 33 +++++++++++++++++++++++++++++++++ src/primitives/unit.rs | 25 +++++-------------------- 9 files changed, 133 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ada6329..a0f69e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Unreleased +# 0.7.0-alpha.9 +- Add default impls for many object kinds +- Add `extend` method on AnyBase +- Clippy nits + # 0.7.0-alpha.8 - Add `from_arbitrary_json` to AnyBase diff --git a/Cargo.toml b/Cargo.toml index a4df4ed..908258c 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.8" +version = "0.7.0-alpha.9" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/src/activity.rs b/src/activity.rs index 15b7c4b..16bc406 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -3604,3 +3604,18 @@ impl OriginRefExt for T where T: OriginRef {} impl OptTargetRefExt for T where T: OptTargetRef {} impl OptOriginRefExt for T where T: OptOriginRef {} impl QuestionExt for T where T: AsQuestion {} + +impl Default for Activity +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} + +impl Default for Question { + fn default() -> Self { + Self::new() + } +} diff --git a/src/actor.rs b/src/actor.rs index 9e89658..791535e 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1637,3 +1637,12 @@ where self.inner_mut().ap_actor_mut() } } + +impl Default for Actor +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} diff --git a/src/base.rs b/src/base.rs index 7617fb6..832bb09 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1120,6 +1120,29 @@ impl AnyBase { Ok(base.into()) } + /// Extend this AnyBase into a kind T where T implements Extends + /// + /// This method returns Ok(None) when the AnyBase does not contain an extensible object, i.e. + /// it's just an IRI + /// + /// ```rust + /// # fn main() -> Result<(), anyhow::Error> { + /// # use activitystreams::{object::Video, base::AnyBase}; + /// # let video = Video::new(); + /// # let any_base = AnyBase::from_extended(video)?; + /// let video: Video = any_base.extend()?; + /// # Ok(()) + /// # } + /// ``` + pub fn extend(self) -> Result, T::Error> + where + T: ExtendsExt, + >::Error: From, + for<'de> Kind: serde::Deserialize<'de>, + { + T::from_any_base(self) + } + /// Convert any type that is extended from `Base` into an AnyBase for storing /// /// ```rust @@ -1934,3 +1957,12 @@ impl From<&str> for OneOrMany { Self::from_xsd_string(xsd_string.to_owned()) } } + +impl Default for Base +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} diff --git a/src/collection.rs b/src/collection.rs index 0d60b00..07d05a8 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -1563,3 +1563,27 @@ where impl CollectionExt for T where T: AsCollection {} impl CollectionPageExt for T where T: AsCollectionPage {} impl OrderedCollectionPageExt for T where T: AsOrderedCollectionPage {} + +impl Default for Collection +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} + +impl Default for CollectionPage +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} + +impl Default for OrderedCollectionPage { + fn default() -> Self { + Self::new() + } +} diff --git a/src/link.rs b/src/link.rs index 059152f..dbe768b 100644 --- a/src/link.rs +++ b/src/link.rs @@ -688,3 +688,12 @@ impl AsLink for Link { } impl LinkExt for T where T: AsLink {} + +impl Default for Link +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} diff --git a/src/object.rs b/src/object.rs index 2278471..6fc8e85 100644 --- a/src/object.rs +++ b/src/object.rs @@ -5609,3 +5609,36 @@ impl AsTombstone for Tombstone { self } } + +impl Default for Object +where + Kind: Default, +{ + fn default() -> Self { + Self::new() + } +} + +impl Default for Place { + fn default() -> Self { + Self::new() + } +} + +impl Default for Profile { + fn default() -> Self { + Self::new() + } +} + +impl Default for Relationship { + fn default() -> Self { + Self::new() + } +} + +impl Default for Tombstone { + fn default() -> Self { + Self::new() + } +} diff --git a/src/primitives/unit.rs b/src/primitives/unit.rs index 956ceb5..e9f02b0 100644 --- a/src/primitives/unit.rs +++ b/src/primitives/unit.rs @@ -249,38 +249,23 @@ struct LengthError; impl Length { fn is_centimeters(&self) -> bool { - match self { - Length::Centimeters => true, - _ => false, - } + matches!(self, Length::Centimeters) } fn is_feet(&self) -> bool { - match self { - Length::Feet => true, - _ => false, - } + matches!(self, Length::Feet) } fn is_inches(&self) -> bool { - match self { - Length::Inches => true, - _ => false, - } + matches!(self, Length::Inches) } fn is_kilometers(&self) -> bool { - match self { - Length::Kilometers => true, - _ => false, - } + matches!(self, Length::Kilometers) } fn is_meters(&self) -> bool { - match self { - Length::Meters => true, - _ => false, - } + matches!(self, Length::Meters) } }