imgup 4.2.0

Upload images via APIs
use clap::ValueEnum;

pub struct Link {
    pub image: String,
    pub thumb: Option<String>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum Format {
    Plain,
    Bbcode,
    Html,
    Markdown,
}

/// Join links into a space-separated string in the given format.
pub fn format_links(links: &[Link], fmt: Format) -> String {
    links
        .iter()
        .map(|link| format_link(&link.image, link.thumb.as_deref(), fmt))
        .collect::<Vec<_>>()
        .join(" ")
}

fn format_link(img: &str, thumb: Option<&str>, fmt: Format) -> String {
    match (fmt, thumb) {
        (Format::Plain, _) => img.to_owned(),
        (Format::Bbcode, None) => format!("[img]{img}[/img]"),
        (Format::Bbcode, Some(t)) => format!("[url={img}][img]{t}[/img][/url]"),
        (Format::Html, None) => format!("<img src=\"{img}\" alt=\"image\">"),
        (Format::Html, Some(t)) => {
            format!("<a href=\"{img}\"><img src=\"{t}\" alt=\"thumb\"></a>")
        }
        (Format::Markdown, None) => format!("![image]({img})"),
        (Format::Markdown, Some(t)) => format!("[![thumb]({t})]({img})"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_links() -> Vec<Link> {
        vec![Link {
            image: "https://example.com/img1.png".to_owned(),
            thumb: None,
        }]
    }

    fn sample_links_with_thumb() -> Vec<Link> {
        vec![Link {
            image: "https://example.com/img1.png".to_owned(),
            thumb: Some("https://example.com/thumb1.jpg".to_owned()),
        }]
    }

    #[test]
    fn test_plain() {
        assert_eq!(
            format_links(&sample_links(), Format::Plain),
            "https://example.com/img1.png"
        );
    }

    #[test]
    fn test_plain_multiple() {
        let links = vec![
            Link {
                image: "https://example.com/a.png".to_owned(),
                thumb: None,
            },
            Link {
                image: "https://example.com/b.png".to_owned(),
                thumb: None,
            },
        ];
        assert_eq!(
            format_links(&links, Format::Plain),
            "https://example.com/a.png https://example.com/b.png"
        );
    }

    #[test]
    fn test_bbcode_no_thumb() {
        assert_eq!(
            format_links(&sample_links(), Format::Bbcode),
            "[img]https://example.com/img1.png[/img]"
        );
    }

    #[test]
    fn test_bbcode_with_thumb() {
        assert_eq!(
            format_links(&sample_links_with_thumb(), Format::Bbcode),
            "[url=https://example.com/img1.png][img]https://example.com/thumb1.jpg[/img][/url]"
        );
    }

    #[test]
    fn test_html_no_thumb() {
        assert_eq!(
            format_links(&sample_links(), Format::Html),
            "<img src=\"https://example.com/img1.png\" alt=\"image\">"
        );
    }

    #[test]
    fn test_html_with_thumb() {
        assert_eq!(
            format_links(&sample_links_with_thumb(), Format::Html),
            "<a href=\"https://example.com/img1.png\"><img src=\"https://example.com/thumb1.jpg\" alt=\"thumb\"></a>"
        );
    }

    #[test]
    fn test_markdown_no_thumb() {
        assert_eq!(
            format_links(&sample_links(), Format::Markdown),
            "![image](https://example.com/img1.png)"
        );
    }

    #[test]
    fn test_markdown_with_thumb() {
        assert_eq!(
            format_links(&sample_links_with_thumb(), Format::Markdown),
            "[![thumb](https://example.com/thumb1.jpg)](https://example.com/img1.png)"
        );
    }

    #[test]
    fn test_empty_links() {
        let links: Vec<Link> = vec![];
        assert_eq!(format_links(&links, Format::Plain), "");
    }
}