]> Untitled Git - lemmy.git/blob - crates/apub_lib/src/values/mod.rs
Merge pull request #1678 from LemmyNet/rewrite-post
[lemmy.git] / crates / apub_lib / src / values / mod.rs
1 //! The enums here serve to limit a json string value to a single, hardcoded value which can be
2 //! verified at compilation time. When using it as the type of a struct field, the struct can only
3 //! be constructed or deserialized if the field has the exact same value.
4 //!
5 //! If we used String as the field type, any value would be accepted, and we would have to check
6 //! manually at runtime that it contains the expected value.
7 //!
8 //! The enums in [`activitystreams::activity::kind`] work in the same way, and can be used to
9 //! distinguish different activity types.
10 //!
11 //! In the example below, `MyObject` can only be constructed or
12 //! deserialized if `media_type` is `text/markdown`, but not if it is `text/html`.
13 //!
14 //! ```
15 //! use lemmy_apub_lib::values::MediaTypeMarkdown;
16 //! use serde_json::from_str;
17 //! use serde::{Deserialize, Serialize};
18 //!
19 //! #[derive(Deserialize, Serialize)]
20 //! struct MyObject {
21 //!   content: String,
22 //!   media_type: MediaTypeMarkdown,
23 //! }
24 //!
25 //! let markdown_json = r#"{"content": "**test**", "media_type": "text/markdown"}"#;
26 //! let from_markdown = from_str::<MyObject>(markdown_json);
27 //! assert!(from_markdown.is_ok());
28 //!
29 //! let markdown_html = r#"{"content": "<b>test</b>", "media_type": "text/html"}"#;
30 //! let from_html = from_str::<MyObject>(markdown_html);
31 //! assert!(from_html.is_err());
32 //! ```
33
34 use serde::{Deserialize, Serialize};
35
36 /// The identifier used to address activities to the public.
37 ///
38 /// <https://www.w3.org/TR/activitypub/#public-addressing>
39 #[derive(Debug, Clone, Deserialize, Serialize)]
40 pub enum PublicUrl {
41   #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")]
42   Public,
43 }
44
45 /// Media type for markdown text.
46 ///
47 /// <https://www.iana.org/assignments/media-types/media-types.xhtml>
48 #[derive(Clone, Debug, Deserialize, Serialize)]
49 pub enum MediaTypeMarkdown {
50   #[serde(rename = "text/markdown")]
51   Markdown,
52 }
53
54 /// Media type for HTML text/
55 ///
56 /// <https://www.iana.org/assignments/media-types/media-types.xhtml>
57 #[derive(Clone, Debug, Deserialize, Serialize)]
58 pub enum MediaTypeHtml {
59   #[serde(rename = "text/html")]
60   Html,
61 }