-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub const AVATAR_IMAGES_DIR_NAME: &'static str = "avatars"; | ||
pub const ICON_IMAGES_DIR_NAME: &'static str = "icons"; | ||
pub const BUNDLE_IMAGES_DIR_NAME: &'static str = "bundles"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use anyhow::{bail, Result}; | ||
use rust_decimal::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use std::path::PathBuf; | ||
|
||
use super::{ | ||
helpers::{format_text, full_image_path}, | ||
ICON_IMAGES_DIR_NAME, | ||
}; | ||
use crate::traits::Hashable; | ||
|
||
#[non_exhaustive] | ||
#[derive(medici_macros::Hashable, Serialize, Deserialize, Hash, PartialEq, Eq, Clone, Debug)] | ||
pub struct IconData { | ||
pub key: String, | ||
|
||
pub is_initial: bool, | ||
pub description: Option<String>, | ||
pub price_in_uyu: Option<Decimal>, | ||
pub image_file_name: PathBuf, | ||
|
||
pub hash: String, | ||
} | ||
|
||
impl IconData { | ||
pub fn new( | ||
key: String, | ||
is_initial: bool, | ||
description: Option<String>, | ||
price_in_uyu: Option<Decimal>, | ||
image_file_name: PathBuf, | ||
) -> Result<Self> { | ||
let mut data = Self { | ||
key, | ||
is_initial, | ||
description, | ||
price_in_uyu, | ||
image_file_name, | ||
hash: Default::default(), | ||
}; | ||
|
||
data.process()?; | ||
|
||
Ok(data) | ||
} | ||
|
||
fn process(&mut self) -> Result<()> { | ||
self.format(); | ||
self.check()?; | ||
|
||
self.refresh_hash(); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn check(&self) -> Result<()> { | ||
if let Some(price_in_uyu) = self.price_in_uyu { | ||
if price_in_uyu <= Decimal::ZERO { | ||
bail!("invalid icon price"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn format(&mut self) { | ||
self.key = self.key.trim().to_string(); | ||
|
||
self.description = self.description.as_deref().map(format_text); | ||
} | ||
|
||
pub fn full_image_path(&self) -> String { | ||
full_image_path(&ICON_IMAGES_DIR_NAME, &self.image_file_name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters