use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DockerHubLogoResponse {
pub logo_url: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DockerHubOrgResponse {
pub gravatar_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum IconSource {
DockerHubLogo { url: String },
DockerHubOrgGravatar { url: String },
DockerOfficialImage { url: String },
#[cfg(feature = "devicon")]
Devicon { url: String },
#[cfg(feature = "simpleicon")]
Simpleicon { url: String },
GhcrAvatar { url: String },
Custom { url: String },
}
impl IconSource {
pub fn url(&self) -> &str {
match self {
Self::DockerHubLogo { url }
| Self::DockerHubOrgGravatar { url }
| Self::DockerOfficialImage { url }
| Self::GhcrAvatar { url }
| Self::Custom { url } => url,
#[cfg(feature = "devicon")]
Self::Devicon { url } => url,
#[cfg(feature = "simpleicon")]
Self::Simpleicon { url } => url,
}
}
pub fn custom(url: impl Into<String>) -> Self {
Self::Custom { url: url.into() }
}
}
#[derive(Debug, Clone)]
pub struct Icon {
source: IconSource,
data: Vec<u8>,
content_type: Option<String>,
}
impl Icon {
pub(crate) fn new(source: IconSource, data: Vec<u8>, content_type: Option<String>) -> Self {
Self { source, data, content_type }
}
pub fn source(&self) -> &IconSource {
&self.source
}
pub fn url(&self) -> &str {
self.source.url()
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn into_data(self) -> Vec<u8> {
self.data
}
pub fn content_type(&self) -> Option<&str> {
self.content_type.as_deref()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_url() {
let source = IconSource::DockerHubLogo {
url: "https://example.com/logo.png".to_string(),
};
assert_eq!(source.url(), "https://example.com/logo.png");
}
#[test]
#[cfg(feature = "devicon")]
fn test_devicon() {
let icon = IconSource::Devicon {
url: "https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/nginx/nginx-original.svg".to_string(),
};
assert_eq!(
icon.url(),
"https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/nginx/nginx-original.svg"
);
}
#[test]
fn test_custom() {
let icon = IconSource::custom("https://example.com/icon.png");
assert_eq!(icon.url(), "https://example.com/icon.png");
}
}