1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/* * This file is part of ActivityStreams. * * Copyright © 2020 Riley Trautman * * ActivityStreams is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ActivityStreams is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ActivityStreams. If not, see <http://www.gnu.org/licenses/>. */ //! ActivityStreams //! //! A set of Traits and Types that make up the ActivityStreams and ActivityPub specifications //! //! ## Usage //! //! First, add ActivityStreams to your dependencies //! ```toml //! activitystreams = "0.6.2" //! ``` //! //! ### Types //! //! The project is laid out by Kind => Type //! //! So to use an ActivityStreams Video, you'd write //! ```rust //! use activitystreams::object::Video; //! ``` //! //! And to use an ActivityStreams profile, you'd write //! ```rust //! use activitystreams::object::Profile; //! ``` //! //! ### Properties //! //! Each concrete type implements `AsRef<>` for each of their properties fields. A basic //! ActivityStreams object will implement `AsRef<ObjectProperties>`. //! //! The Properties types can be found near the kind they're associated with. `ObjectProperties` and //! `ApObjectProperties` are located in `activitystreams::object::properties`. //! //! The Properties types are generated by the `properties` macro, which attempts to create fields //! that represent exactly the bounds of the ActivityStreams and ActivityPub specifications. //! //! For example, the Object type in ActivityStreams has a `summary` field, which can either be //! represented as an `xsd:string` or an `rdf:langString`. It also states that the `summary` field //! is not `functional`, meaning that any number of `xsd:string` or `rdf:langString`, or a //! combination thereof, can be present. To represent this, the `properties` macro generates a //! couple `enum` types. //! //! First, it generates `ObjectPropertiesSummaryTermEnum`, which is a "terminating" enum. //! "terminating" in this context means it is the smallest unit of the type. This enum has two //! variants, named after the types they contain, `XsdString(...)` and `RdfLangString(...)`. //! //! Next, it generates `ObjectPropertiesSummaryEnum`, which contains two variants, `Term(...)` and //! `Array(...)`. The `Term` variant contains an `ObjectPropertiesSummaryTermEnum`, and the `Array` //! variant contains a `Vec<ObjectPropertiesSummaryTermEnum>`. //! //! Finally, when declaring the field, it generates `summary: Option<ObjectPropertiesSummaryEnum>`, //! since `summary` is not a required field. //! //! This resulting type is exactly specific enough to match the following valid ActivityStreams //! json, without matching any invalid json. //! //! With no summary: //! ```json //! {} //! ``` //! //! With a sring summary: //! ```json //! { //! "summary": "A string" //! } //! ``` //! //! With an rdf langstring //! ```json //! { //! "summary": { //! "@value": "A string", //! "@language": "en" //! } //! } //! ``` //! //! With multiple values //! ```json //! { //! "summary": [ //! { //! "@value": "A string", //! "@language": "en" //! }, //! "An xsd:string this time" //! ] //! } //! ``` //! //! It may seem like interacting with these types might get unweildy, so the `properties` macro //! also generates methods for interacting with each field. //! //! ```ignore //! fn set_summary_xsd_string<T>(&mut self, T) -> Result<...>; //! fn set_summary_rdf_lang_string<T>(&mut self, T) -> Result<...>; //! fn set_many_summary_xsd_strings<T>(&mut self, Vec<T>) -> Result<...>; //! fn set_many_summary_rdf_lang_strings<T>(&mut self, Vec<T>) -> Result<...>; //! //! fn delete_summary(&mut self) -> &mut Self; //! //! fn get_summary_xsd_string(&self) -> Option<XsdString>; //! fn get_summary_rdf_lang_string(&self) -> Option<RdfLangString>; //! fn get_many_summary_xsd_strings(&self) -> Option<Vec<&XsdString>>; //! fn get_many_summary_rdf_lang_strings(&self) -> Option<Vec<&RdfLangString>>; //! ``` //! These methods provide access to setting and fetching uniformly typed data, as well as deleting //! the data. In the setter methods, the type parameter T is bound by //! `TryInto<XsdString>` or `TryInto<RdfLangString>`. This allows passing values to the method that //! can be converted into the types, rather than requiring the caller to perform the conversion. //! //! Types like `XsdString` and `RdfLangString` can be found in the `primitives` module. Unless //! you're building your own custom types, you shouldn't need to import them yourself. They each //! implement `FromStr` for parsing and `Display` to convert back to strings, as well as `From` and //! `Into` or `TryFrom` and `TryInto` for types you might expect them to (e.g. //! `XsdNonNegativeInteger` implements `From<u64>` and `Into<u64>`). //! //! For some fields, like `id`, there is only one valid type. methods generated for fields like //! these will leave out the type name from the function name. //! //! ```ignore //! fn set_id<T>(&mut self, T) -> Result<...>; //! fn delete_id(&mut self) -> &mut Self; //! fn get_id(&self) -> Option<XsdAnyUri>; //! ``` //! //! ### Traits //! //! This library provides a number of traits, such as `Object`, `Link`, `Actor`, `Activity`, //! `Collection`, and `CollectionPage`. The majority of these traits exist solely to "mark" types, //! meaning they don't provide value, at runtime, but exist to add constraints to generics at //! compiletime. //! //! If you want to make a function that manipulates an Activity, but not a normal object, you could //! bound the function like so: //! ```ignore //! fn my_manipulator<T>(some_activity: T) -> Result<&mut ObjectProperties, SomeErrorType> //! where //! T: Activity + AsMut<ObjectProperties>, //! { //! some_activity.as_mut().set_whatever_tbh() //! } //! ``` //! //! ### Kinds //! //! This library has a set of unit structs that serialize and deserialize to strings. This is to //! enable different ActivityPub Object types to be deserialized into different Named structs. //! These can be found in `activitystreams::objects::kind`, and similar paths. //! //! To build your own Person struct, for example, you could write //! ```ignore //! use activitystreams::actor::kind::PersonType; //! //! #[derive(serde::Deserialize, serde::Serialize)] //! pub struct MyPerson { //! // Do a rename since `type` is not a valid rust field name //! #[serde(rename = "type")] //! kind: PersonType, //! } //! ``` //! And this type would only deserialize for JSON where `"type":"Person"` //! //! ### Extensions //! //! In some cases, like when dealing with ActivityPub, it is neccessary to extend the //! ActivityStreams specification. For this purpose, two traits and a type have been introduced. //! //! ```ignore //! use activitystreams::ext::{Ext, Extensible, Extension}; //! ``` //! //! The `Ext` type is a simple record containing first, the ActivityStreams type, and second, the //! extension to that type. //! //! There are two provided extensions in the ActivityStreams library. //! - ApObjectProperties, extra properties for all ActivityStreams objects in the ActivityPub spec //! - ApActorProperties, extra properties specifically for Actors in the ActivityPub spec //! //! To use an object with its default extensions, the object's `full()` associated function may be //! invoked. //! ```rust //! # use activitystreams::object::Video; //! let video_with_extensions = Video::full(); //! ``` //! //! ### Features //! There are a number of features that can be disabled in this crate. By default, everything is //! enabled. //! //! ```toml //! activitystreams = { version = "0.6.2", default-features = "false", features = ["derive"] } //! ``` //! //! | feature | what you get | //! | ---------- | --------------------------------------------------------- | //! | none | Just the Marker Traits | //! | derive | Marker Traits + derive macros from activitystreams-derive | //! | kinds | Marker Traits + derive macros + Kind UnitStructs | //! | primitives | Marker Traits + Primitive values | //! | types | Everything, this is the default | //! //! ## Examples //! //! ### Basic //! //! ```rust //! use activitystreams::object::{Video, properties::ObjectProperties}; //! use anyhow::Error; //! //! // We perform configuration in a dedicated function to specify which Properties type we want to //! // perform the operations on. //! fn configure_video(mut v: impl AsMut<ObjectProperties>) -> Result<(), Error> { //! v.as_mut() //! .set_context_xsd_any_uri("https://www.w3.org/ns/activitystreams")? //! .set_id("https://example.com/@example/lions")? //! .set_url_xsd_any_uri("https://example.com/@example/lions/video.webm")? //! .set_name_xsd_string("My Cool Video")? //! .set_summary_xsd_string("A video about some cool lions")? //! .set_media_type("video/webm")? //! .set_duration("PT4M20S")?; //! //! Ok(()) //! } //! //! fn main() -> Result<(), Error> { //! let mut v = Video::default(); //! //! configure_video(&mut v)?; //! //! println!("Video, {:#?}", v); //! //! let s = serde_json::to_string(&v)?; //! //! println!("json, {}", s); //! //! let v: Video = serde_json::from_str(&s)?; //! //! println!("Video again, {:#?}", v); //! //! Ok(()) //! } //! ``` //! //! ### Intermediate //! //! ```rust //! use activitystreams::{ //! context, //! actor::{Actor, ActorBox}, //! ext::Ext, //! object::{ //! properties::{ //! ObjectProperties, //! ProfileProperties //! }, //! Profile, //! Object, //! ObjectBox, //! }, //! primitives::XsdAnyUri, //! Base, BaseBox, PropRefs, //! }; //! use serde::{Deserialize, Serialize}; //! //! #[derive(Clone, Debug, Default, Deserialize, Serialize, PropRefs)] //! #[serde(rename_all = "camelCase")] //! #[prop_refs(Object)] //! #[prop_refs(Actor)] //! pub struct Persona { //! #[serde(rename = "@context")] //! context: XsdAnyUri, //! //! #[serde(rename = "type")] //! kind: String, //! } //! //! fn main() -> Result<(), anyhow::Error> { //! let mut profile = Profile::full(); //! //! let pprops: &mut ProfileProperties = profile.as_mut(); //! //! pprops.set_describes_object_box(Persona { //! context: context(), //! kind: "Persona".to_owned(), //! })?; //! //! let oprops: &mut ObjectProperties = profile.as_mut(); //! oprops.set_context_xsd_any_uri(context())?; //! //! let profile_string = serde_json::to_string(&profile)?; //! //! let profile: Profile = serde_json::from_str(&profile_string)?; //! //! Ok(()) //! } //! ``` //! //! ### Advanced //! //! ```rust //! use activitystreams::{ //! properties, //! ext::Ext, //! link::{ //! properties::LinkProperties, //! Link, LinkBox, Mention, //! }, //! Base, BaseBox, PropRefs, //! UnitString, //! }; //! use serde::{Deserialize, Serialize}; //! //! /// Using the UnitString derive macro //! /// //! /// This macro implements Serialize and Deserialize for the given type, making this type //! /// represent the string "MyLink" in JSON. //! #[derive(Clone, Debug, Default, UnitString)] //! #[unit_string(MyLink)] //! pub struct MyKind; //! //! properties! { //! My { //! docs [ "Defining our own properties struct called MyProperties" ], //! //! required_key { //! docs [ //! "Our own required key field", //! "", //! "'types' defines the range of values that can be stored in required_key", //! "", //! "'functional' means there is at most one value for required_key", //! "'required' means there is at least one value for required_key", //! ], //! types [ String ], //! functional, //! required, //! }, //! } //! } //! //! #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] //! #[serde(transparent)] //! pub struct MyLinkProps(pub LinkProperties); //! //! /// Using the Properties derive macro //! /// //! /// This macro generates getters and setters for the associated fields. //! #[derive(Clone, Debug, Default, Deserialize, Serialize, PropRefs)] //! #[serde(rename_all = "camelCase")] //! #[prop_refs(Link)] //! pub struct My { //! /// Use the UnitString MyKind to enforce the type of the object by "MyLink" //! pub kind: MyKind, //! //! /// Derive AsRef/AsMut for My -> MyProperties //! #[prop_refs] //! pub my_properties: MyProperties, //! //! /// Derive AsRef/AsMut/Link for My -> MyLinkProperties //! #[prop_refs] //! pub link_properties: MyLinkProps, //! } //! //! fn main() -> Result<(), anyhow::Error> { //! let mut my_link = My::default(); //! //! let lprops: &mut MyProperties = my_link.as_mut(); //! lprops.set_required_key("Hey")?; //! //! let my_link_string = serde_json::to_string(&my_link)?; //! //! let my_link: My = serde_json::from_str(&my_link_string)?; //! //! Ok(()) //! } //! ``` pub mod activity; pub mod actor; pub mod collection; #[cfg(feature = "types")] pub mod endpoint; #[cfg(feature = "types")] pub mod ext; pub mod link; pub mod object; #[cfg(feature = "primitives")] pub mod primitives; pub use self::{ activity::{Activity, IntransitiveActivity}, actor::Actor, collection::{Collection, CollectionPage}, link::Link, object::Object, }; #[cfg_attr(feature = "types", wrapper_type)] /// The lowermost trait of the trait structure /// /// Base exists solely so Object and Link can have impls that don't potentially conflict pub trait Base {} #[cfg(feature = "primitives")] /// The context associated with all of the Activity Streams types defined in the crate. pub fn context() -> crate::primitives::XsdAnyUri { "https://www.w3.org/ns/activitystreams".parse().unwrap() } #[cfg(feature = "primitives")] /// The 'security' extension used by some implementations pub fn security() -> crate::primitives::XsdAnyUri { "https://w3id.org/security/v1".parse().unwrap() } #[cfg(feature = "primitives")] /// The 'public' actor, doesn't denote a real actor but describes a publicly available object. pub fn public() -> crate::primitives::XsdAnyUri { "https://www.w3.org/ns/activitystreams#Public" .parse() .unwrap() } #[cfg(feature = "derive")] pub use activitystreams_derive::{properties, wrapper_type, Extensible, PropRefs, UnitString};