use serde::{Deserialize, Serialize};
pub const MAX_ARTIFACT_SEND_BYTES: usize = 2 * 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactKind {
Markdown,
Html,
Chart,
Image,
Text,
}
impl ArtifactKind {
pub fn for_filename(name: &str) -> Self {
let lower = name.to_ascii_lowercase();
let ends = |exts: &[&str]| exts.iter().any(|e| lower.ends_with(e));
if ends(&[".chart.json"]) {
Self::Chart
} else if ends(&[".md", ".markdown"]) {
Self::Markdown
} else if ends(&[".html", ".htm"]) {
Self::Html
} else if ends(&[".png", ".jpg", ".jpeg"]) {
Self::Image
} else {
Self::Text
}
}
pub fn label(self) -> &'static str {
match self {
Self::Markdown => "document",
Self::Html => "app",
Self::Chart => "chart",
Self::Image => "image",
Self::Text => "file",
}
}
pub fn mime(self, filename: &str) -> &'static str {
match self {
Self::Markdown => "text/markdown; charset=utf-8",
Self::Html => "text/html; charset=utf-8",
Self::Chart => "application/json",
Self::Image if filename.to_ascii_lowercase().ends_with(".png") => "image/png",
Self::Image => "image/jpeg",
Self::Text => "text/plain; charset=utf-8",
}
}
pub fn is_text(self) -> bool {
!matches!(self, Self::Image)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactVersion {
pub n: u32,
pub path: String,
pub created_at: i64,
#[serde(default)]
pub note: String,
#[serde(default)]
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Artifact {
pub id: String,
pub kind: ArtifactKind,
pub title: String,
pub filename: String,
pub versions: Vec<ArtifactVersion>,
}
impl Artifact {
pub fn latest(&self) -> Option<&ArtifactVersion> {
self.versions.iter().max_by_key(|v| v.n)
}
pub fn version(&self, n: u32) -> Option<&ArtifactVersion> {
self.versions.iter().find(|v| v.n == n)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactRef {
pub id: String,
pub version: u32,
}
pub fn valid_id(id: &str) -> bool {
(1..=64).contains(&id.len()) && id.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') && !id.starts_with('-')
}
pub const THEME_VARS: [&str; 26] = [
"--byob-bg",
"--byob-surface",
"--byob-fg",
"--byob-muted",
"--byob-accent",
"--byob-on-accent",
"--byob-border",
"--byob-danger",
"--byob-success",
"--byob-warning",
"--byob-link",
"--byob-strong",
"--byob-faint",
"--byob-panel",
"--byob-field",
"--byob-code-bg",
"--byob-code-fg",
"--byob-radius",
"--byob-chart-a",
"--byob-chart-b",
"--byob-chart-c",
"--byob-chart-d",
"--byob-chart-e",
"--byob-chart-f",
"--byob-chart-g",
"--byob-chart-h",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kinds_follow_the_file_name() {
assert_eq!(ArtifactKind::for_filename("Sales.chart.json"), ArtifactKind::Chart);
assert_eq!(ArtifactKind::for_filename("data.json"), ArtifactKind::Text);
assert_eq!(ArtifactKind::for_filename("summary.MD"), ArtifactKind::Markdown);
assert_eq!(ArtifactKind::for_filename("calc.html"), ArtifactKind::Html);
assert_eq!(ArtifactKind::for_filename("plot.jpeg"), ArtifactKind::Image);
assert_eq!(ArtifactKind::for_filename("script.py"), ArtifactKind::Text);
assert_eq!(ArtifactKind::for_filename("noext"), ArtifactKind::Text);
assert_eq!(ArtifactKind::Image.mime("a.png"), "image/png");
assert!(!ArtifactKind::Image.is_text() && ArtifactKind::Html.is_text());
}
#[test]
fn ids() {
assert!(valid_id("loan-calculator-3f2a"));
for bad in ["", "-x", "A", "a/b", "..", "a b", &"x".repeat(65)] {
assert!(!valid_id(bad), "{bad}");
}
}
#[test]
fn latest_and_lookup() {
let v = |n| ArtifactVersion { n, path: format!("v{n}"), created_at: 0, note: String::new(), size: 1 };
let a = Artifact { id: "a".into(), kind: ArtifactKind::Html, title: "A".into(), filename: "a.html".into(), versions: vec![v(1), v(3), v(2)] };
assert_eq!(a.latest().map(|v| v.n), Some(3));
assert_eq!(a.version(2).map(|v| v.path.as_str()), Some("v2"));
assert!(a.version(9).is_none());
}
}