Expose Either type
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Aode (lion) 2022-01-28 14:48:38 -06:00
parent f3d394d5d6
commit b7f178f2b6
11 changed files with 52 additions and 15 deletions

View file

@ -1,5 +1,8 @@
# Unreleased # Unreleased
# 0.7.0-alpha.18
- expose `Either` type publicly
# 0.7.0-alpha.17 # 0.7.0-alpha.17
- add `XsdBoolean` for `xsd:boolean` serde compatibility - add `XsdBoolean` for `xsd:boolean` serde compatibility
- add `closed` field for `Question` (range Object | Link | xsd:datetime | xsd:boolean), and related - add `closed` field for `Question` (range Object | Link | xsd:datetime | xsd:boolean), and related

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams" name = "activitystreams"
description = "A set of core types and traits for activitystreams data" 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" license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -25,11 +25,10 @@
use crate::{ use crate::{
base::{AnyBase, AsBase, Base, Extends}, base::{AnyBase, AsBase, Base, Extends},
checked::CheckError, checked::CheckError,
either::Either,
markers, markers,
object::{ApObject, AsObject, Object}, object::{ApObject, AsObject, Object},
prelude::BaseExt, prelude::BaseExt,
primitives::{OneOrMany, XsdBoolean, XsdDateTime}, primitives::{Either, OneOrMany, XsdBoolean, XsdDateTime},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
}; };
use iri_string::types::IriString; use iri_string::types::IriString;
@ -1437,7 +1436,7 @@ pub trait QuestionExt: AsQuestion {
self.question_ref().closed.as_ref().map(|either| { self.question_ref().closed.as_ref().map(|either| {
either either
.as_ref() .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() self.question_mut()
.closed .closed
.take() .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 /// Remove the closed field from the current activity

View file

@ -29,9 +29,8 @@
//! ``` //! ```
use crate::{ use crate::{
checked::{check, CheckError}, checked::{check, CheckError},
either::Either,
markers, markers,
primitives::{AnyString, MimeMediaType, OneOrMany}, primitives::{AnyString, Either, MimeMediaType, OneOrMany},
unparsed::{Unparsed, UnparsedMut}, unparsed::{Unparsed, UnparsedMut},
}; };
use iri_string::types::{IriStr, IriString}; use iri_string::types::{IriStr, IriString};

View file

@ -300,7 +300,6 @@ pub mod actor;
pub mod base; pub mod base;
pub mod checked; pub mod checked;
pub mod collection; pub mod collection;
mod either;
pub mod link; pub mod link;
mod macros; mod macros;
pub mod markers; pub mod markers;

View file

@ -1,7 +1,4 @@
use crate::{ use crate::primitives::{Either, OneOrMany, RdfLangString};
either::Either,
primitives::{OneOrMany, RdfLangString},
};
/// A type representing any kind of string /// A type representing any kind of string
/// ///

View file

@ -2,12 +2,24 @@
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
)] )]
#[serde(untagged)] #[serde(untagged)]
/// Represent either of two types
pub enum Either<L, R> { pub enum Either<L, R> {
Left(L), Left(L),
Right(R), Right(R),
} }
impl<T> Either<T, T> {
/// 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<L, R> Either<L, R> { impl<L, R> Either<L, R> {
/// Try getting just the left
pub fn left(self) -> Option<L> { pub fn left(self) -> Option<L> {
if let Either::Left(l) = self { if let Either::Left(l) = self {
Some(l) Some(l)
@ -16,6 +28,7 @@ impl<L, R> Either<L, R> {
} }
} }
/// Try getting just the right
pub fn right(self) -> Option<R> { pub fn right(self) -> Option<R> {
if let Either::Right(r) = self { if let Either::Right(r) = self {
Some(r) Some(r)
@ -24,6 +37,7 @@ impl<L, R> Either<L, R> {
} }
} }
/// Borrow the Left and Right
pub fn as_ref(&self) -> Either<&L, &R> { pub fn as_ref(&self) -> Either<&L, &R> {
match self { match self {
Either::Left(ref l) => Either::Left(l), Either::Left(ref l) => Either::Left(l),
@ -31,6 +45,7 @@ impl<L, R> Either<L, R> {
} }
} }
/// Mutably borrow the Left and Right
pub fn as_mut(&mut self) -> Either<&mut L, &mut R> { pub fn as_mut(&mut self) -> Either<&mut L, &mut R> {
match self { match self {
Either::Left(ref mut l) => Either::Left(l), Either::Left(ref mut l) => Either::Left(l),
@ -38,6 +53,7 @@ impl<L, R> Either<L, R> {
} }
} }
/// Map over the Left and Right values
pub fn map<F1, F2, L2, R2>(self, f1: F1, f2: F2) -> Either<L2, R2> pub fn map<F1, F2, L2, R2>(self, f1: F1, f2: F2) -> Either<L2, R2>
where where
F1: Fn(L) -> L2, F1: Fn(L) -> L2,
@ -48,4 +64,26 @@ impl<L, R> Either<L, R> {
Either::Right(r) => Either::Right((f2)(r)), Either::Right(r) => Either::Right((f2)(r)),
} }
} }
/// Map just the left value
pub fn map_left<F, L2>(self, f: F) -> Either<L2, R>
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<F, R2>(self, f: F) -> Either<L, R2>
where
F: Fn(R) -> R2,
{
match self {
Either::Left(l) => Either::Left(l),
Either::Right(r) => Either::Right((f)(r)),
}
}
} }

View file

@ -14,6 +14,7 @@
//! ``` //! ```
mod any_string; mod any_string;
mod either;
mod one_or_many; mod one_or_many;
mod rdf_lang_string; mod rdf_lang_string;
mod serde_parse; mod serde_parse;
@ -24,6 +25,7 @@ mod xsd_duration;
pub use self::{ pub use self::{
any_string::AnyString, any_string::AnyString,
either::Either,
one_or_many::OneOrMany, one_or_many::OneOrMany,
rdf_lang_string::RdfLangString, rdf_lang_string::RdfLangString,
unit::Unit, unit::Unit,

View file

@ -1,4 +1,4 @@
use crate::either::Either; use crate::primitives::Either;
/// A type representing at least one value /// A type representing at least one value
/// ///

View file

@ -1,4 +1,4 @@
use crate::either::Either; use crate::primitives::Either;
/// A type representing units of length /// A type representing units of length
/// ///

View file

@ -1,4 +1,4 @@
use crate::either::Either; use crate::primitives::Either;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};