#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
pub mod parser;
#[cfg(feature = "tts")]
pub mod tts;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[cfg(feature = "tts")]
use tts::{CardShape, CustomDeckState};
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum CardError {
CardDoesntExist {
card_name: String,
},
BackImageFileError {
card_name: String,
image_url: String,
},
FrontImageNotFound {
card_name: String,
image_url: String,
},
Custom {
message: String,
},
}
impl Display for CardError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CardDoesntExist { card_name } => write!(f, "Card doesn't exist: {card_name}"),
Self::BackImageFileError {
card_name,
image_url,
} => write!(
f,
"Couldn't find the file for {card_name}'s back: {image_url}"
),
Self::FrontImageNotFound {
card_name,
image_url,
} => write!(
f,
"Couldn't find the file for {card_name}'s front: {image_url}"
),
Self::Custom { message } => write!(f, "{message}"),
}
}
}
impl CardError {
#[must_use]
pub const fn custom(message: String) -> Self {
Self::Custom { message }
}
}
pub trait GetCardInfo: Sized {
fn get_name(&self) -> &str;
fn get_front_image(&self) -> Result<String, CardError>;
fn get_back_image(&self) -> Result<String, CardError>;
#[cfg(feature = "tts")]
fn get_card_shape(&self) -> Result<CardShape, CardError>;
fn parse(string: &str) -> Result<Self, parser::ParseError>;
}
#[derive(Clone)]
pub struct CardEntry<T: GetCardInfo + Clone> {
pub card: T,
pub amount: i64,
}
#[cfg(feature = "tts")]
impl<T: GetCardInfo + Clone> CardEntry<T> {
pub fn get_custom_deck_state(&self) -> Result<CustomDeckState, CardError> {
Ok(CustomDeckState {
name: self.card.get_name().to_owned(),
face_url: self.card.get_front_image()?,
back_url: self.card.get_back_image()?,
num_width: Some(1),
num_height: Some(1),
back_is_hidden: true,
unique_back: false,
r#type: self.card.get_card_shape()?.into(),
})
}
}
#[cfg(feature = "tts")]
fn generate_guid() -> String {
Uuid::new_v4().to_string()
}