Split kinds into own library

This commit is contained in:
Aode (lion) 2021-11-18 18:45:09 -06:00
parent 47efc1c646
commit cba33c4642
9 changed files with 188 additions and 162 deletions

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.12" version = "0.7.0-alpha.13"
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"
@ -14,10 +14,12 @@ edition = "2021"
[workspace] [workspace]
members = [ members = [
"activitystreams-ext" "activitystreams-ext",
"activitystreams-kinds"
] ]
[dependencies] [dependencies]
activitystreams-kinds = { version = "0.1.0", path = "./activitystreams-kinds/" }
chrono = "0.4" chrono = "0.4"
mime = "0.3" mime = "0.3"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View file

@ -0,0 +1,9 @@
[package]
name = "activitystreams-kinds"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1", features = ["derive"] }

View file

@ -0,0 +1,170 @@
//! # activitystreams-kinds
//!
//! Enums representing typed versions of activitypub 'type' fields.
/// Generate an enum implementing serde's Serialize and Deserialize with a single variant
///
/// This is useful for describing constants
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::kind;
///
/// kind!(CustomType, Custom);
///
/// #[derive(serde::Deserialize)]
/// struct MyStruct {
/// #[serde(rename = "type")]
/// kind: CustomType,
/// }
///
/// let s: MyStruct = serde_json::from_str(r#"{"type":"Custom"}"#)?;
///
/// assert_eq!(s.kind, CustomType::Custom);
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! kind {
($x:ident, $y:ident) => {
#[derive(
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
serde::Deserialize,
serde::Serialize,
)]
/// A type stand-in for the constant $y, deriving serde traits
pub enum $x {
$y,
}
impl std::fmt::Display for $x {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, stringify!($y))
}
}
impl Default for $x {
fn default() -> Self {
$x::$y
}
}
};
}
pub mod activity {
//! Kinds of activities defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CreateType` -> `"Create"`
use crate::kind;
kind!(AcceptType, Accept);
kind!(AddType, Add);
kind!(AnnounceType, Announce);
kind!(ArriveType, Arrive);
kind!(BlockType, Block);
kind!(CreateType, Create);
kind!(DeleteType, Delete);
kind!(DislikeType, Dislike);
kind!(FlagType, Flag);
kind!(FollowType, Follow);
kind!(IgnoreType, Ignore);
kind!(InviteType, Invite);
kind!(JoinType, Join);
kind!(LeaveType, Leave);
kind!(LikeType, Like);
kind!(ListenType, Listen);
kind!(MoveType, Move);
kind!(OfferType, Offer);
kind!(QuestionType, Question);
kind!(ReadType, Read);
kind!(RejectType, Reject);
kind!(RemoveType, Remove);
kind!(TentativeAcceptType, TentativeAccept);
kind!(TentativeRejectType, TentativeReject);
kind!(TravelType, Travel);
kind!(UndoType, Undo);
kind!(UpdateType, Update);
kind!(ViewType, View);
}
pub mod actor {
//! Kinds of actors defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PersonType` -> `"Person"`
use crate::kind;
kind!(ApplicationType, Application);
kind!(GroupType, Group);
kind!(OrganizationType, Organization);
kind!(PersonType, Person);
kind!(ServiceType, Service);
}
pub mod collection {
//! Kinds of collections defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CollectionType` -> `"Collection"`
use crate::kind;
kind!(CollectionType, Collection);
kind!(OrderedCollectionType, OrderedCollection);
kind!(CollectionPageType, CollectionPage);
kind!(OrderedCollectionPageType, OrderedCollectionPage);
}
pub mod link {
//! Kinds of links defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `MentionType` -> `"Mention"`
use crate::kind;
kind!(MentionType, Mention);
}
pub mod object {
//! Kinds of objects defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PlaceType` -> `"Place"`
use crate::kind;
kind!(ArticleType, Article);
kind!(AudioType, Audio);
kind!(DocumentType, Document);
kind!(EventType, Event);
kind!(ImageType, Image);
kind!(NoteType, Note);
kind!(PageType, Page);
kind!(PlaceType, Place);
kind!(ProfileType, Profile);
kind!(RelationshipType, Relationship);
kind!(TombstoneType, Tombstone);
kind!(VideoType, Video);
}
#[cfg(test)]
mod tests {
use super::kind;
#[test]
fn to_string_works() {
kind!(MyType, My);
assert_eq!(MyType::My.to_string(), "My")
}
}

View file

@ -33,43 +33,7 @@ use crate::{
use std::convert::TryFrom; use std::convert::TryFrom;
use url::Url; use url::Url;
pub mod kind { pub use activitystreams_kinds::activity as kind;
//! Kinds of activities defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CreateType` -> `"Create"`
use crate::kind;
kind!(AcceptType, Accept);
kind!(AddType, Add);
kind!(AnnounceType, Announce);
kind!(ArriveType, Arrive);
kind!(BlockType, Block);
kind!(CreateType, Create);
kind!(DeleteType, Delete);
kind!(DislikeType, Dislike);
kind!(FlagType, Flag);
kind!(FollowType, Follow);
kind!(IgnoreType, Ignore);
kind!(InviteType, Invite);
kind!(JoinType, Join);
kind!(LeaveType, Leave);
kind!(LikeType, Like);
kind!(ListenType, Listen);
kind!(MoveType, Move);
kind!(OfferType, Offer);
kind!(QuestionType, Question);
kind!(ReadType, Read);
kind!(RejectType, Reject);
kind!(RemoveType, Remove);
kind!(TentativeAcceptType, TentativeAccept);
kind!(TentativeRejectType, TentativeReject);
kind!(TravelType, Travel);
kind!(UndoType, Undo);
kind!(UpdateType, Update);
kind!(ViewType, View);
}
use self::kind::*; use self::kind::*;

View file

@ -31,20 +31,7 @@ use crate::{
}; };
use url::Url; use url::Url;
pub mod kind { pub use activitystreams_kinds::actor as kind;
//! Kinds of actors defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PersonType` -> `"Person"`
use crate::kind;
kind!(ApplicationType, Application);
kind!(GroupType, Group);
kind!(OrganizationType, Organization);
kind!(PersonType, Person);
kind!(ServiceType, Service);
}
use self::kind::*; use self::kind::*;

View file

@ -31,19 +31,7 @@ use crate::{
}; };
use std::convert::TryFrom; use std::convert::TryFrom;
pub mod kind { pub use activitystreams_kinds::collection as kind;
//! Kinds of collections defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CollectionType` -> `"Collection"`
use crate::kind;
kind!(CollectionType, Collection);
kind!(OrderedCollectionType, OrderedCollection);
kind!(CollectionPageType, CollectionPage);
kind!(OrderedCollectionPageType, OrderedCollectionPage);
}
use self::kind::*; use self::kind::*;

View file

@ -29,16 +29,7 @@ use crate::{
use std::convert::TryFrom; use std::convert::TryFrom;
use url::Url; use url::Url;
pub mod kind { pub use activitystreams_kinds::link as kind;
//! Kinds of links defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `MentionType` -> `"Mention"`
use crate::kind;
kind!(MentionType, Mention);
}
use self::kind::MentionType; use self::kind::MentionType;

View file

@ -1,58 +1,3 @@
/// Generate an enum implementing serde's Serialize and Deserialize with a single variant
///
/// This is useful for describing constants
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::kind;
///
/// kind!(CustomType, Custom);
///
/// #[derive(serde::Deserialize)]
/// struct MyStruct {
/// #[serde(rename = "type")]
/// kind: CustomType,
/// }
///
/// let s: MyStruct = serde_json::from_str(r#"{"type":"Custom"}"#)?;
///
/// assert_eq!(s.kind, CustomType::Custom);
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! kind {
($x:ident, $y:ident) => {
#[derive(
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
serde::Deserialize,
serde::Serialize,
)]
/// A type stand-in for the constant $y, deriving serde traits
pub enum $x {
$y,
}
impl std::fmt::Display for $x {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, stringify!($y))
}
}
impl Default for $x {
fn default() -> Self {
$x::$y
}
}
};
}
/// A macro to shorten the `string.parse::<Url>()?` calls inevitably made in downstream code /// A macro to shorten the `string.parse::<Url>()?` calls inevitably made in downstream code
/// ///
/// ```rust /// ```rust
@ -73,13 +18,3 @@ macro_rules! uri {
$x.parse::<Url>()? $x.parse::<Url>()?
}}; }};
} }
#[cfg(test)]
mod tests {
#[test]
fn to_string_works() {
kind!(MyType, My);
assert_eq!(MyType::My.to_string(), "My")
}
}

View file

@ -29,27 +29,7 @@ use chrono::{DateTime, Duration, FixedOffset};
use std::convert::TryFrom; use std::convert::TryFrom;
use url::Url; use url::Url;
pub mod kind { pub use activitystreams_kinds::object as kind;
//! Kinds of objects defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PlaceType` -> `"Place"`
use crate::kind;
kind!(ArticleType, Article);
kind!(AudioType, Audio);
kind!(DocumentType, Document);
kind!(EventType, Event);
kind!(ImageType, Image);
kind!(NoteType, Note);
kind!(PageType, Page);
kind!(PlaceType, Place);
kind!(ProfileType, Profile);
kind!(RelationshipType, Relationship);
kind!(TombstoneType, Tombstone);
kind!(VideoType, Video);
}
use self::kind::*; use self::kind::*;