mastodon API rust lib elefren, fixed and updated. and also all ASYNC! NB. most examples are now wrong.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
elefren-fork/src/status_builder.rs

145 lines
4.1 KiB

use isolang::Language;
use std::fmt;
/// A builder pattern struct for constructing a status.
///
/// # Example
///
/// ```
/// # extern crate elefren;
/// # use elefren::{Language, StatusBuilder};
///
/// let status = StatusBuilder {
/// status: "a status".to_string(),
/// sensitive: Some(true),
/// spoiler_text: Some("a CW".to_string()),
/// language: Some(Language::Eng),
/// ..Default::default()
/// };
/// ```
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
8 years ago
pub struct StatusBuilder {
/// The text of the status.
pub status: String,
/// Ids of accounts being replied to.
6 years ago
#[serde(skip_serializing_if = "Option::is_none")]
pub in_reply_to_id: Option<u64>,
/// Ids of media attachments being attached to the status.
6 years ago
#[serde(skip_serializing_if = "Option::is_none")]
pub media_ids: Option<Vec<u64>>,
/// Whether current status is sensitive.
6 years ago
#[serde(skip_serializing_if = "Option::is_none")]
pub sensitive: Option<bool>,
/// Text to precede the normal status text.
6 years ago
#[serde(skip_serializing_if = "Option::is_none")]
pub spoiler_text: Option<String>,
/// Visibility of the status, defaults to `Public`.
6 years ago
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<Visibility>,
/// Language code of the status
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<Language>,
8 years ago
}
/// The visibility of a status.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
8 years ago
pub enum Visibility {
/// A Direct message to a user
8 years ago
Direct,
/// Only available to followers
8 years ago
Private,
/// Not shown in public timelines
8 years ago
Unlisted,
/// Posted to public timelines
8 years ago
Public,
}
impl StatusBuilder {
/// Create a new status with text.
/// ```
/// use elefren::prelude::*;
///
/// let status = StatusBuilder::new("Hello World!");
/// ```
pub fn new<D: fmt::Display>(status: D) -> Self {
8 years ago
StatusBuilder {
status: status.to_string(),
8 years ago
..Self::default()
}
}
}
impl Default for Visibility {
fn default() -> Self {
Visibility::Public
}
}
#[cfg(test)]
mod tests {
use super::*;
use isolang::Language;
use serde_json;
#[test]
fn test_new() {
let s = StatusBuilder::new("a status");
let expected = StatusBuilder {
status: "a status".to_string(),
in_reply_to_id: None,
media_ids: None,
sensitive: None,
spoiler_text: None,
visibility: None,
language: None,
};
assert_eq!(s, expected);
}
#[test]
fn test_default_visibility() {
let v: Visibility = Default::default();
assert_eq!(v, Visibility::Public);
}
#[test]
fn test_serialize_visibility() {
assert_eq!(
serde_json::to_string(&Visibility::Direct).expect("couldn't serialize visibility"),
"\"direct\"".to_string()
);
assert_eq!(
serde_json::to_string(&Visibility::Private).expect("couldn't serialize visibility"),
"\"private\"".to_string()
);
assert_eq!(
serde_json::to_string(&Visibility::Unlisted).expect("couldn't serialize visibility"),
"\"unlisted\"".to_string()
);
assert_eq!(
serde_json::to_string(&Visibility::Public).expect("couldn't serialize visibility"),
"\"public\"".to_string()
);
}
#[test]
fn test_serialize_status() {
let status = StatusBuilder::new("a status");
assert_eq!(
serde_json::to_string(&status).expect("Couldn't serialize status"),
"{\"status\":\"a status\"}".to_string()
);
let status = StatusBuilder {
status: "a status".into(),
language: Some(Language::Eng),
..Default::default()
};
assert_eq!(
serde_json::to_string(&status).expect("Couldn't serialize status"),
"{\"status\":\"a status\",\"language\":\"eng\"}"
);
}
}