ActivityStreams
A set of Traits and Types that make up the ActivityStreams and ActivityPub specifications
Usage
First, add ActivityStreams to your dependencies
= "0.5.0-alpha.17"
Types
The project is laid out by Kind => vocabulary => Type
So to use an ActivityStreams Video, you'd write
use Video;
And to use an ActivityPub profile, you'd write
use Profile;
Link is a little different, since there's only one defined link type, called Mention.
use Mention;
Properties
Each concrete type implements AsRef<>
for each of their properties fields. A basic
ActivityStreams object will implement AsRef<ObjectProperties>
, while an ActivityPub Actor
might implement AsRef<ObjectProperties>
, AsRef<ApObjectProperties>
, and
AsRef<ApActorProperties>
.
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:
With a sring summary:
With an rdf langstring
With multiple values
It may seem like interacting with these types might get unweildy, so the properties
macro
also generates methods for interacting with each field.
..>;
..>;
..>;
..>;
;
;
;
;
;
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.
..>;
;
;
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:
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
use PersonType;
And this type would only deserialize for JSON where "type":"Person"
Features
There are a number of features that can be disabled in this crate. By default, everything is enabled.
= { = "0.5.0-alpha.17", = "false", = ["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
use ;
use Error;
// We perform configuration in a dedicated function to specify which Properties type we want to
// perform the operations on.
Intermediate
use ;
use ;
use Any;
Advanced
use ;
use ;
/// Using the UnitString derive macro
///
/// This macro implements Serialize and Deserialize for the given type, making this type
/// represent the string "MyLink" in JSON.
;
properties!
/// Using the Properties derive macro
///
/// This macro generates getters and setters for the associated fields.
Contributing
Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.
License
Copyright © 2018 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. This file is part of ActivityStreams.
You should have received a copy of the GNU General Public License along with ActivityStreams. If not, see http://www.gnu.org/licenses/.