weeb_api 0.2.0

A Rust library for the Weeb.sh API.
Documentation
use std::marker::PhantomData;

/// A struct used for uploading images to the Api.
#[derive(Clone, Default, Serialize)]
pub struct UploadParams {
    /// The kind of the image.
    #[serde(rename = "baseType")]
    pub base_type: String,
    /// The image file data.
    pub file: Vec<u8>,
    /// Wheather the image is private.
    pub hidden: Option<bool>,
    /// Wheather the imsage is NSFW.
    pub nsfw: Option<bool>,
    /// The source of the image.
    pub source: Option<String>,
    /// The tags for the image.
    pub tags: Option<Vec<String>>,
    // Prevents manual userland instantiation of the struct, to avoid BC
    // breaks if we add another field.
    #[serde(skip)]
    phantom: PhantomData<()>,
}

impl UploadParams {
    /// Create a new [`UploadParams`] struct using all possible values.
    ///
    /// [`UploadParams`]: struct.UploadParams.html
    pub fn new(
        base_type: String,
        file: Vec<u8>,
        hidden: Option<bool>,
        nsfw: Option<bool>,
        source: Option<String>,
        tags: Option<Vec<String>>,
    ) -> Self {
        Self {
            phantom: PhantomData,
            base_type,
            file,
            hidden,
            nsfw,
            source,
            tags,
        }
    }

    /// Create a new [`UploadParams`] struct using the bare minimum number of
    /// values.
    ///
    /// [`UploadParams`]: struct.UploadParams.html
    pub fn simple (
        base_type: String,
        file: Vec<u8>,
    ) -> Self {
        Self {
            phantom: PhantomData,
            base_type,
            file,
            hidden: None,
            nsfw: None,
            source: None,
            tags: None,
        }
    }


    pub(crate) fn into_data(self) -> (Vec<(&'static str, String)>, Vec<u8>) {
        let mut data = vec![];
        data.push(("base_type", self.base_type));

        if let Some(hidden) = self.hidden {
            data.push(("hidden", if hidden { "true" } else { "false" }.to_owned()));
        }

        if let Some(nsfw) = self.nsfw {
            data.push(("nsfw", if nsfw { "true" } else { "false" }.to_owned()));
        }

        if let Some(source) = self.source {
            data.push(("source", source));
        }

        if let Some(tags) = self.tags {
            data.push(("tags", tags.join(",")));
        }

        (data, self.file)
    }
}