Browse Source

+ [ap] added types representing valid values for the 'tag' and 'attachment' fields

main
Riley Apeldoorn 9 months ago
parent
commit
d8af7e4b2d
  1. 132
      src/ap/media.rs
  2. 22
      src/ap/mod.rs

132
src/ap/media.rs

@ -0,0 +1,132 @@
use serde::{ Serialize, Deserialize, };
use sealed::Sealed;
mod sealed { pub trait Sealed {} }
/// Attachable media files.
pub trait File: Sealed {
/// Get the source of the media file.
fn url (&self) -> &url::Url;
/// Get the alt text of the media file.
fn alt (&self) -> Option<&str>;
}
/// A video attachment.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Video {
url: url::Url,
#[serde(rename = "summary", alias = "name")]
alt: Option<String>,
#[serde(rename = "type")]
kind: VideoKind,
}
impl Sealed for Video {}
impl File for Video {
fn alt (&self) -> Option<&str> {
self.alt
.as_ref()
.map(|s| s.as_ref())
}
fn url (&self) -> &url::Url {
&self.url
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum VideoKind {
/// This enforces that types must have a field `type` with value `"Video"`
/// in order to serialize to an [`Video`].
Video,
}
/// An audio file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Audio {
url: url::Url,
#[serde(rename = "summary", alias = "name")]
alt: Option<String>,
#[serde(rename = "type")]
kind: AudioKind,
}
impl Sealed for Audio {}
impl File for Audio {
fn alt (&self) -> Option<&str> {
self.alt
.as_ref()
.map(|s| s.as_ref())
}
fn url (&self) -> &url::Url {
&self.url
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum AudioKind {
/// This enforces that types must have a field `type` with value `"Audio"`
/// in order to serialize to an [`Audio`].
Audio,
}
/// An image file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Image {
url: url::Url,
#[serde(rename = "summary", alias = "name")]
alt: Option<String>,
#[serde(rename = "type")]
kind: ImageKind,
}
impl Sealed for Image {}
impl File for Image {
fn alt (&self) -> Option<&str> {
self.alt
.as_ref()
.map(|s| s.as_ref())
}
fn url (&self) -> &url::Url {
&self.url
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum ImageKind {
/// This enforces that types must have a field `type` with value `"Image"`
/// in order to serialize to an [`Image`].
Image,
}

22
src/ap/mod.rs

@ -42,6 +42,19 @@ use serde::{ Serialize, Deserialize, };
pub mod obj;
pub mod act;
pub mod media;
pub mod tag {
pub struct Emoji;
pub struct Mention;
pub struct Hashtag;
pub enum Tag {
Mention (Mention),
Hashtag (Hashtag),
Emoji (Emoji),
}
}
/// Types that are recognized as [`Note`] objects and converted to one.
///
/// [`Note`]: obj::Note
@ -53,6 +66,15 @@ pub enum Notelike {
Page,
}
pub enum Attachment {
Video (media::Video),
Audio (media::Audio),
Image (media::Image),
Mention (tag::Mention),
Hashtag (tag::Hashtag),
Emoji (tag::Emoji),
}
/// A builder for [`Activity`] types.
pub struct Builder<O> {

Loading…
Cancel
Save