wallet_standard_base/
icon.rs

1use std::borrow::Cow;
2
3use base64ct::{Base64, Encoding};
4
5#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
6pub struct WalletStandardIcon {
7    bytes: &'static [u8],
8    mime: WalletStandardIconMime,
9}
10
11impl WalletStandardIcon {
12    pub fn new(bytes: &'static [u8], mime: WalletStandardIconMime) -> Self {
13        Self { bytes, mime }
14    }
15
16    pub fn new_svg(bytes: &'static [u8]) -> Self {
17        Self::new(bytes, WalletStandardIconMime::Svg)
18    }
19
20    pub fn new_gif(bytes: &'static [u8]) -> Self {
21        Self::new(bytes, WalletStandardIconMime::Gif)
22    }
23
24    pub fn new_webp(bytes: &'static [u8]) -> Self {
25        Self::new(bytes, WalletStandardIconMime::Webp)
26    }
27
28    pub fn new_png(bytes: &'static [u8]) -> Self {
29        Self::new(bytes, WalletStandardIconMime::Png)
30    }
31
32    pub fn new_jpeg(bytes: &'static [u8]) -> Self {
33        Self::new(bytes, WalletStandardIconMime::Jpeg)
34    }
35
36    pub fn base64<'wa>(&'wa self) -> Cow<'wa, str> {
37        let encoded = Base64::encode_string(self.bytes);
38
39        Cow::Borrowed("data:image/") + self.mime.mime_str() + ";base64," + Cow::Owned(encoded)
40    }
41}
42
43#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
44pub enum WalletStandardIconMime {
45    Svg,
46    Png,
47    Webp,
48    Gif,
49    Jpeg,
50}
51
52impl WalletStandardIconMime {
53    pub fn mime_str(&self) -> &str {
54        match self {
55            Self::Svg => "svg+xml",
56            Self::Gif => "gif",
57            Self::Png => "png",
58            Self::Webp => "webp",
59            Self::Jpeg => "jpeg",
60        }
61    }
62}