activitypub-federation-rust/src/protocol/values.rs

65 lines
2.1 KiB
Rust
Raw Normal View History

2023-03-06 01:17:34 +00:00
//! Single value enums used to receive JSON with specific expected string values
//!
2022-06-02 11:17:12 +00:00
//! The enums here serve to limit a json string value to a single, hardcoded value which can be
//! verified at compilation time. When using it as the type of a struct field, the struct can only
//! be constructed or deserialized if the field has the exact same value.
//!
//! In the example below, `MyObject` can only be constructed or
//! deserialized if `media_type` is `text/markdown`, but not if it is `text/html`.
//!
//! ```
//! use serde_json::from_str;
//! use serde::{Deserialize, Serialize};
//! use activitypub_federation::protocol::values::MediaTypeMarkdown;
2022-06-02 11:17:12 +00:00
//!
//! #[derive(Deserialize, Serialize)]
//! struct MyObject {
//! content: String,
//! media_type: MediaTypeMarkdown,
//! }
//!
//! let markdown_json = r#"{"content": "**test**", "media_type": "text/markdown"}"#;
//! let from_markdown = from_str::<MyObject>(markdown_json);
//! assert!(from_markdown.is_ok());
//!
//! let markdown_html = r#"{"content": "<b>test</b>", "media_type": "text/html"}"#;
//! let from_html = from_str::<MyObject>(markdown_html);
//! assert!(from_html.is_err());
//! ```
2023-03-06 01:17:34 +00:00
//!
//! The enums in [activitystreams_kinds] work in the same way, and can be used to
//! distinguish different activity types.
2022-06-02 11:17:12 +00:00
use serde::{Deserialize, Serialize};
/// Media type for markdown text.
///
/// <https://www.iana.org/assignments/media-types/media-types.xhtml>
2023-11-13 09:38:58 +00:00
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
2022-06-02 11:17:12 +00:00
pub enum MediaTypeMarkdown {
2023-03-02 14:18:06 +00:00
/// `text/markdown`
2022-06-02 11:17:12 +00:00
#[serde(rename = "text/markdown")]
Markdown,
}
/// Media type for HTML text.
///
/// <https://www.iana.org/assignments/media-types/media-types.xhtml>
2023-11-13 09:38:58 +00:00
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
2022-06-02 11:17:12 +00:00
pub enum MediaTypeHtml {
2023-03-02 14:18:06 +00:00
/// `text/html`
2022-06-02 11:17:12 +00:00
#[serde(rename = "text/html")]
Html,
}
/// Media type which allows both markdown and HTML.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
2022-06-02 11:17:12 +00:00
pub enum MediaTypeMarkdownOrHtml {
2023-03-02 14:18:06 +00:00
/// `text/markdown`
2022-06-02 11:17:12 +00:00
#[serde(rename = "text/markdown")]
Markdown,
2023-03-02 14:18:06 +00:00
/// `text/html`
2022-06-02 11:17:12 +00:00
#[serde(rename = "text/html")]
Html,
}