Skip to main content

azul_css/props/basic/
image.rs

1use crate::{corety::AzString, props::basic::parse::strip_quotes};
2
3#[derive(Copy, Clone, PartialEq)]
4pub enum CssImageParseError<'a> {
5    UnclosedQuotes(&'a str),
6}
7
8impl_debug_as_display!(CssImageParseError<'a>);
9impl_display! {CssImageParseError<'a>, {
10    UnclosedQuotes(e) => format!("Unclosed quotes: \"{}\"", e),
11}}
12
13/// Owned version of CssImageParseError.
14#[derive(Debug, Clone, PartialEq)]
15pub enum CssImageParseErrorOwned {
16    UnclosedQuotes(String),
17}
18
19impl<'a> CssImageParseError<'a> {
20    pub fn to_contained(&self) -> CssImageParseErrorOwned {
21        match self {
22            CssImageParseError::UnclosedQuotes(s) => {
23                CssImageParseErrorOwned::UnclosedQuotes(s.to_string())
24            }
25        }
26    }
27}
28
29impl CssImageParseErrorOwned {
30    pub fn to_shared<'a>(&'a self) -> CssImageParseError<'a> {
31        match self {
32            CssImageParseErrorOwned::UnclosedQuotes(s) => {
33                CssImageParseError::UnclosedQuotes(s.as_str())
34            }
35        }
36    }
37}
38
39/// A string slice that has been stripped of its quotes.
40/// In CSS, quotes are optional in url() so we accept both quoted and unquoted strings.
41pub fn parse_image<'a>(input: &'a str) -> Result<AzString, CssImageParseError<'a>> {
42    // Try to strip quotes first, but if there are none, use the input as-is
43    Ok(match strip_quotes(input) {
44        Ok(stripped) => stripped.0.into(),
45        Err(_) => input.trim().into(), // No quotes, use as-is (valid in modern CSS)
46    })
47}