diff options
Diffstat (limited to 'common/src/helpers.rs')
| -rw-r--r-- | common/src/helpers.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/common/src/helpers.rs b/common/src/helpers.rs index db75ba9..431bf8c 100644 --- a/common/src/helpers.rs +++ b/common/src/helpers.rs @@ -4,7 +4,9 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::{CreditCategory, IdentifierType}; +use base64::{Engine, prelude::BASE64_URL_SAFE}; + +use crate::{CreditCategory, IdentifierType, Picture, PictureSlot}; use std::{fmt::Display, ops::Deref, str::FromStr}; #[derive(PartialEq)] @@ -129,3 +131,40 @@ impl FromStr for IdentifierType { }) } } +impl Display for PictureSlot { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + PictureSlot::Backdrop => "backdrop", + PictureSlot::Cover => "cover", + }) + } +} +impl FromStr for PictureSlot { + type Err = (); + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok(match s { + "backdrop" => PictureSlot::Backdrop, + "cover" => PictureSlot::Cover, + _ => return Err(()), + }) + } +} + +impl Display for Picture { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&BASE64_URL_SAFE.encode(self.0)) + } +} +impl FromStr for Picture { + type Err = &'static str; + fn from_str(s: &str) -> Result<Self, Self::Err> { + let mut out = [0; 32]; + let size = BASE64_URL_SAFE + .decode_slice(s, &mut out) + .map_err(|_| "invalid base64 picture key")?; + if size != out.len() { + return Err("picture key parse invalid size"); + } + Ok(Self(out)) + } +} |