Expose Either type

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

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.17"
version = "0.7.0-alpha.18"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

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

View file

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

View file

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

View file

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

View file

@ -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<L, R> {
Left(L),
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> {
/// Try getting just the left
pub fn left(self) -> Option<L> {
if let Either::Left(l) = self {
Some(l)
@ -16,6 +28,7 @@ impl<L, R> Either<L, R> {
}
}
/// Try getting just the right
pub fn right(self) -> Option<R> {
if let Either::Right(r) = self {
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> {
match self {
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> {
match self {
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>
where
F1: Fn(L) -> L2,
@ -48,4 +64,26 @@ impl<L, R> Either<L, 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 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,

View file

@ -1,4 +1,4 @@
use crate::either::Either;
use crate::primitives::Either;
/// 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
///

View file

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