ojak_vocab/lib.rs
1//! The Activity Vocabulary, for Ojak.
2#![no_std]
3//!
4//! Every ActivityStreams type and the extensions the fediverse uses, from
5//! Fedify's vocabulary schemas and Ojak's own additions to them (see
6//! `schemas/` and `extensions/`), generated into [`generated`] and exported
7//! here: `ojak_vocab::Note`, `ojak_vocab::Follow`, `ojak_vocab::AnyObject`.
8//!
9//! A document is read by what it means: [`read`] normalises it over the
10//! contexts Ojak ships and reads it into a type, and [`read_reporting`] says
11//! what the type did not keep. [`write()`] writes a value in Ojak's spelling,
12//! under Ojak's context.
13//!
14//! This crate models protocol data only. It does not fetch, store, deliver,
15//! or decide anything.
16
17extern crate alloc;
18
19use alloc::boxed::Box;
20use iri_string::types::IriString;
21use serde::{Deserialize, Serialize};
22
23pub mod generated;
24pub mod json;
25pub mod loss;
26pub mod meaning;
27mod read;
28
29pub use generated::*;
30pub use loss::Loss;
31pub use read::{Read, ReadError, Registry, read, read_reporting, write, write_with_context};
32
33/// The canonical Activity Streams JSON-LD context URL.
34pub const ACTIVITYSTREAMS_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
35
36/// The special collection addressing every actor (public posts).
37pub const ACTIVITYSTREAMS_PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public";
38
39/// Whether `iri` names the public collection, in any of the spellings
40/// compaction leaves it in: in full, as `as:Public`, or as `Public`.
41/// Mastodon's `ActivityPub::TagManager#public_collection?`.
42#[must_use]
43pub fn is_public_collection(iri: &str) -> bool {
44 matches!(iri, ACTIVITYSTREAMS_PUBLIC | "as:Public" | "Public")
45}
46
47/// An absolute ActivityPub/ActivityStreams identifier.
48pub type Iri = IriString;
49
50/// A value that is either an object's IRI or the object itself.
51///
52/// ActivityStreams properties can hold either, and Ojak keeps the two
53/// apart rather than dereferencing.
54#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
55#[serde(untagged)]
56pub enum Reference<T> {
57 Id(Iri),
58 Object(Box<T>),
59}
60
61impl<T> Reference<T> {
62 #[must_use]
63 pub fn id(id: Iri) -> Self {
64 Self::Id(id)
65 }
66
67 #[must_use]
68 pub fn object(object: T) -> Self {
69 Self::Object(Box::new(object))
70 }
71}