From b7f178f2b6a8e485696b046fd0e2d457c024420a Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Fri, 28 Jan 2022 14:48:38 -0600 Subject: [PATCH] Expose Either type --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- src/activity.rs | 7 +++---- src/base.rs | 3 +-- src/lib.rs | 1 - src/primitives/any_string.rs | 5 +---- src/{ => primitives}/either.rs | 38 ++++++++++++++++++++++++++++++++++ src/primitives/mod.rs | 2 ++ src/primitives/one_or_many.rs | 2 +- src/primitives/unit.rs | 2 +- src/primitives/xsd_boolean.rs | 2 +- 11 files changed, 52 insertions(+), 15 deletions(-) rename src/{ => primitives}/either.rs (54%) diff --git a/CHANGELOG.md b/CHANGELOG.md index edd792d..2db4f95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Unreleased +# 0.7.0-alpha.18 +- expose `Either` type publicly + # 0.7.0-alpha.17 - add `XsdBoolean` for `xsd:boolean` serde compatibility - add `closed` field for `Question` (range Object | Link | xsd:datetime | xsd:boolean), and related diff --git a/Cargo.toml b/Cargo.toml index 168e2c3..736b1f2 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.17" +version = "0.7.0-alpha.18" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/src/activity.rs b/src/activity.rs index c6b95ff..a9fd7b0 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -25,11 +25,10 @@ use crate::{ base::{AnyBase, AsBase, Base, Extends}, checked::CheckError, - either::Either, markers, object::{ApObject, AsObject, Object}, prelude::BaseExt, - primitives::{OneOrMany, XsdBoolean, XsdDateTime}, + primitives::{Either, OneOrMany, XsdBoolean, XsdDateTime}, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use iri_string::types::IriString; @@ -1437,7 +1436,7 @@ pub trait QuestionExt: AsQuestion { self.question_ref().closed.as_ref().map(|either| { either .as_ref() - .map(|l| l, |r| r.as_ref().map(|l| *l.as_datetime(), |r| r.0)) + .map_right(|r| r.as_ref().map_left(|l| *l.as_datetime()).map_right(|r| r.0)) }) } @@ -1576,7 +1575,7 @@ pub trait QuestionExt: AsQuestion { self.question_mut() .closed .take() - .map(|either| either.map(|l| l, |r| r.map(|date| date.into(), |b| b.into()))) + .map(|either| either.map_right(|r| r.map(|date| date.into(), |b| b.into()))) } /// Remove the closed field from the current activity diff --git a/src/base.rs b/src/base.rs index 8447e88..fdd46e4 100644 --- a/src/base.rs +++ b/src/base.rs @@ -29,9 +29,8 @@ //! ``` use crate::{ checked::{check, CheckError}, - either::Either, markers, - primitives::{AnyString, MimeMediaType, OneOrMany}, + primitives::{AnyString, Either, MimeMediaType, OneOrMany}, unparsed::{Unparsed, UnparsedMut}, }; use iri_string::types::{IriStr, IriString}; diff --git a/src/lib.rs b/src/lib.rs index 12adcc8..261d44a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,7 +300,6 @@ pub mod actor; pub mod base; pub mod checked; pub mod collection; -mod either; pub mod link; mod macros; pub mod markers; diff --git a/src/primitives/any_string.rs b/src/primitives/any_string.rs index 1435e48..cf618ee 100644 --- a/src/primitives/any_string.rs +++ b/src/primitives/any_string.rs @@ -1,7 +1,4 @@ -use crate::{ - either::Either, - primitives::{OneOrMany, RdfLangString}, -}; +use crate::primitives::{Either, OneOrMany, RdfLangString}; /// A type representing any kind of string /// diff --git a/src/either.rs b/src/primitives/either.rs similarity index 54% rename from src/either.rs rename to src/primitives/either.rs index ee371c1..daa8d2e 100644 --- a/src/either.rs +++ b/src/primitives/either.rs @@ -2,12 +2,24 @@ Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize, )] #[serde(untagged)] +/// Represent either of two types pub enum Either { Left(L), Right(R), } +impl Either { + /// Extract the inner type when Left and Right are the same + pub fn into_inner(self) -> T { + match self { + Self::Left(t) => t, + Self::Right(t) => t, + } + } +} + impl Either { + /// Try getting just the left pub fn left(self) -> Option { if let Either::Left(l) = self { Some(l) @@ -16,6 +28,7 @@ impl Either { } } + /// Try getting just the right pub fn right(self) -> Option { if let Either::Right(r) = self { Some(r) @@ -24,6 +37,7 @@ impl Either { } } + /// Borrow the Left and Right pub fn as_ref(&self) -> Either<&L, &R> { match self { Either::Left(ref l) => Either::Left(l), @@ -31,6 +45,7 @@ impl Either { } } + /// Mutably borrow the Left and Right pub fn as_mut(&mut self) -> Either<&mut L, &mut R> { match self { Either::Left(ref mut l) => Either::Left(l), @@ -38,6 +53,7 @@ impl Either { } } + /// Map over the Left and Right values pub fn map(self, f1: F1, f2: F2) -> Either where F1: Fn(L) -> L2, @@ -48,4 +64,26 @@ impl Either { Either::Right(r) => Either::Right((f2)(r)), } } + + /// Map just the left value + pub fn map_left(self, f: F) -> Either + where + F: Fn(L) -> L2, + { + match self { + Either::Left(l) => Either::Left((f)(l)), + Either::Right(r) => Either::Right(r), + } + } + + /// Map just the right value + pub fn map_right(self, f: F) -> Either + where + F: Fn(R) -> R2, + { + match self { + Either::Left(l) => Either::Left(l), + Either::Right(r) => Either::Right((f)(r)), + } + } } diff --git a/src/primitives/mod.rs b/src/primitives/mod.rs index 33e2b3d..6a70e72 100644 --- a/src/primitives/mod.rs +++ b/src/primitives/mod.rs @@ -14,6 +14,7 @@ //! ``` mod any_string; +mod either; mod one_or_many; mod rdf_lang_string; mod serde_parse; @@ -24,6 +25,7 @@ mod xsd_duration; pub use self::{ any_string::AnyString, + either::Either, one_or_many::OneOrMany, rdf_lang_string::RdfLangString, unit::Unit, diff --git a/src/primitives/one_or_many.rs b/src/primitives/one_or_many.rs index 5b12b6e..98523ef 100644 --- a/src/primitives/one_or_many.rs +++ b/src/primitives/one_or_many.rs @@ -1,4 +1,4 @@ -use crate::either::Either; +use crate::primitives::Either; /// A type representing at least one value /// diff --git a/src/primitives/unit.rs b/src/primitives/unit.rs index 50c3151..555361f 100644 --- a/src/primitives/unit.rs +++ b/src/primitives/unit.rs @@ -1,4 +1,4 @@ -use crate::either::Either; +use crate::primitives::Either; /// A type representing units of length /// diff --git a/src/primitives/xsd_boolean.rs b/src/primitives/xsd_boolean.rs index 78cffad..f9c2cbe 100644 --- a/src/primitives/xsd_boolean.rs +++ b/src/primitives/xsd_boolean.rs @@ -1,4 +1,4 @@ -use crate::either::Either; +use crate::primitives::Either; use serde::{Deserialize, Serialize}; use std::ops::{Deref, DerefMut};