vkclient 6.0.0

Vk Api client implementation
Documentation
use crate::inner::VkApiInner;
use crate::structs::Version;
use crate::vkapi::{Compression, Encoding, VkApi};

/// API Client builder struct.
/// Use `VkApi::from` or `into` to make `VkApi` struct.
#[derive(Clone, Debug)]
pub struct VkApiBuilder {
    inner: VkApiInner,
}

impl VkApiBuilder {
    /// Creates the builder from an access key with default values.
    pub fn new(access_token: String) -> Self {
        cfg_if::cfg_if! {
            if #[cfg(feature = "compression_zstd")] {
                let encoding = Compression::Zstd;
            } else if #[cfg(feature = "compression_gzip")] {
                let encoding = Compression::Gzip;
            } else {
                let encoding = Compression::None;
            }
        }
        cfg_if::cfg_if! {
            if #[cfg(feature = "encode_msgpack")] {
                let format = Encoding::Msgpack;
            } else if #[cfg(feature = "encode_json")] {
                let format = Encoding::Json;
            } else {
                let format = Encoding::None;
            }
        }

        Self {
            inner: VkApiInner {
                access_token,
                version: Version::default(),
                domain: "api.vk.ru".to_owned(),
                format,
                encoding,
            },
        }
    }

    /// Pass new access token to builder
    #[must_use]
    pub fn with_access_token(mut self, access_token: String) -> Self {
        self.inner.access_token = access_token;
        self
    }

    /// Pass new version to builder. Default is 5.131
    #[must_use]
    pub const fn with_version(mut self, version: Version) -> Self {
        self.inner.version = version;
        self
    }

    /// Pass new API domain to builder. The default is api.vk.ru
    #[must_use]
    pub fn with_domain(mut self, domain: String) -> Self {
        self.inner.domain = domain;
        self
    }

    /// Pass new compression to builder. The default is `Compression::Zstd`
    #[must_use]
    pub const fn with_compression(mut self, compression: Compression) -> Self {
        self.inner.encoding = compression;
        self
    }

    /// Pass new encoding to the builder. The default is `Encoding::Msgpack`
    #[must_use]
    pub const fn with_encoding(mut self, encoding: Encoding) -> Self {
        self.inner.format = encoding;
        self
    }
}

impl From<VkApiBuilder> for VkApi {
    fn from(builder: VkApiBuilder) -> Self {
        Self::from_inner(builder.inner)
    }
}