fedi-post 0.1.0

A library to create posts on different fediverse services.
Documentation
//! A library for posting to fediverse services like Lemmy and PieFed.

#![warn(missing_docs)]

use thiserror::Error;

/// Code for posting to [Lemmy](https://join-lemmy.org/).
pub mod lemmy;
/// Code for posting to [PieFed](https://join.piefed.social/).
pub mod piefed;

/// A post that can be submitted to a fediverse service.
#[derive(Debug, Clone)]
pub struct ServerPost {
    /// The community to post to.
    pub community: String,
    /// The title of the post.
    pub title: String,
    /// An url to attach to the post.
    /// This can be a link to a website or an image.
    pub url: Option<String>,
    /// The body text of the post.
    pub body: Option<String>,
    /// The language of the post as a short language code, e.g. "en" for English.
    pub language: Option<String>,
    /// The alt text of the attached url, if it is an image.
    pub alt_text: Option<String>,
    /// Whether the post should be marked as NSFW (not safe for work).
    pub nsfw: Option<bool>,
    /// Whether the post should be marked as NSFL (not safe for life).
    pub nsfl: Option<bool>,
    /// Whether the post is generated by A"I" (artificial "intelligence").
    pub ai_generated: Option<bool>,
    /// The thumbnail for the post.
    /// If not set, the service may fetch or generate one.
    pub custom_thumbnail: Option<String>,
}

/// The credentials required to log in to a fediverse service and post.
#[derive(Debug, Clone)]
pub struct ServerCredentials {
    /// The domain of the fediverse service, e.g. `sopuli.xyz`.
    pub domain: String,
    /// The username to log in with.
    pub username: String,
    /// The password to log in with.
    pub password: String,
    /// The kind of fediverse service to post to.
    pub fediverse_service_kind: FediverseServiceKind,
}

/// The kind of a fediverse service.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FediverseServiceKind {
    /// [Lemmy](https://join-lemmy.org/)
    Lemmy,
    /// [PieFed](https://join.piefed.social/)
    PieFed,
}

/// The error type for this library.
#[derive(Debug, Error)]
pub enum Error {
    /// Errors that can occur when posting to Lemmy.
    #[error("An error occurred while posting to Lemmy: {0}")]
    Lemmy(#[from] lemmy::LemmyError),
    /// Errors that can occur when posting to PieFed.
    #[error("An error occurred while posting to PieFed: {0}")]
    PieFed(#[from] piefed::PieFedError),
}

/// Create the post `post` on a fediverse service specified by `credentials`.
pub async fn post_to_server(
    credentials: &ServerCredentials,
    post: ServerPost,
) -> Result<(), Error> {
    match credentials.fediverse_service_kind {
        FediverseServiceKind::Lemmy => lemmy::post_to_lemmy(credentials, post).await?,
        FediverseServiceKind::PieFed => piefed::post_to_piefed(credentials, post).await?,
    }
    Ok(())
}