elemental-core 0.3.0

Core library for Elemental, providing common utilities and abstractions used across the project.
Documentation
#[cfg(feature = "microsoft-auth")]
pub mod microsoft;
pub mod offline;
#[cfg(feature = "yggdrasil-auth")]
pub mod yggdrasil;

use std::any::Any;

use async_trait::async_trait;
use serde::de::DeserializeOwned;

use crate::auth::accounts::Account;
use crate::auth::error::{Error, Result};
use crate::auth::texture::{ProfileTextures, SkinModel};

fn unsupported<T>(operation: &'static str) -> Result<T> {
    Err(Error::TextureUnsupported { operation })
}

#[async_trait]
pub trait TypedAccountProvider: Send + Sync {
    type Account: Account + DeserializeOwned + 'static;

    fn authorizer_id(&self) -> &'static str;

    async fn refresh(&self, account: &Self::Account) -> Result<Self::Account>;
}

#[async_trait]
pub trait TypedTextureProvider: TypedAccountProvider {
    async fn textures(&self, account: &Self::Account) -> Result<ProfileTextures>;

    async fn upload_skin(
        &self,
        _account: &Self::Account,
        _image: Vec<u8>,
        _model: SkinModel,
    ) -> Result<()> {
        unsupported("upload skin")
    }

    async fn delete_skin(&self, _account: &Self::Account) -> Result<()> {
        unsupported("delete skin")
    }

    async fn upload_cape(&self, _account: &Self::Account, _image: Vec<u8>) -> Result<()> {
        unsupported("upload cape")
    }

    async fn delete_cape(&self, _account: &Self::Account) -> Result<()> {
        unsupported("delete cape")
    }

    async fn select_cape(&self, _account: &Self::Account, _cape_id: Option<String>) -> Result<()> {
        unsupported("select cape")
    }
}

#[async_trait]
pub trait AccountProvider: Send + Sync {
    fn authorizer_id(&self) -> &'static str;

    fn decode(&self, data: &str) -> std::result::Result<Box<dyn Account>, serde_json::Error>;

    async fn refresh(&self, account: &dyn Account) -> Result<Box<dyn Account>>;
}

#[async_trait]
pub trait TextureProvider: AccountProvider {
    async fn textures(&self, account: &dyn Account) -> Result<ProfileTextures>;

    async fn upload_skin(
        &self,
        account: &dyn Account,
        image: Vec<u8>,
        model: SkinModel,
    ) -> Result<()>;

    async fn delete_skin(&self, account: &dyn Account) -> Result<()>;

    async fn upload_cape(&self, account: &dyn Account, image: Vec<u8>) -> Result<()>;

    async fn delete_cape(&self, account: &dyn Account) -> Result<()>;

    async fn select_cape(&self, account: &dyn Account, cape_id: Option<String>) -> Result<()>;
}

fn downcast_account<'a, P>(provider: &P, account: &'a dyn Account) -> Result<&'a P::Account>
where
    P: TypedAccountProvider + ?Sized,
{
    (account as &dyn Any)
        .downcast_ref::<P::Account>()
        .ok_or_else(|| Error::AccountTypeMismatch {
            expected_authorizer: provider.authorizer_id(),
            actual_authorizer: account.authorizer_id().to_owned(),
        })
}

#[async_trait]
impl<P> AccountProvider for P
where
    P: TypedAccountProvider,
{
    fn authorizer_id(&self) -> &'static str {
        TypedAccountProvider::authorizer_id(self)
    }

    fn decode(&self, data: &str) -> std::result::Result<Box<dyn Account>, serde_json::Error> {
        serde_json::from_str::<P::Account>(data)
            .map(|account| Box::new(account) as Box<dyn Account>)
    }

    async fn refresh(&self, account: &dyn Account) -> Result<Box<dyn Account>> {
        let typed = downcast_account(self, account)?;
        let refreshed = TypedAccountProvider::refresh(self, typed).await?;
        Ok(Box::new(refreshed))
    }
}

#[async_trait]
impl<P> TextureProvider for P
where
    P: TypedTextureProvider,
{
    async fn textures(&self, account: &dyn Account) -> Result<ProfileTextures> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::textures(self, typed).await
    }

    async fn upload_skin(
        &self,
        account: &dyn Account,
        image: Vec<u8>,
        model: SkinModel,
    ) -> Result<()> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::upload_skin(self, typed, image, model).await
    }

    async fn delete_skin(&self, account: &dyn Account) -> Result<()> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::delete_skin(self, typed).await
    }

    async fn upload_cape(&self, account: &dyn Account, image: Vec<u8>) -> Result<()> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::upload_cape(self, typed, image).await
    }

    async fn delete_cape(&self, account: &dyn Account) -> Result<()> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::delete_cape(self, typed).await
    }

    async fn select_cape(&self, account: &dyn Account, cape_id: Option<String>) -> Result<()> {
        let typed = downcast_account(self, account)?;
        TypedTextureProvider::select_cape(self, typed, cape_id).await
    }
}