cotis-defaults 0.1.0-alpha.1

Modular Rust UI framework core
Documentation
#[derive(Clone)]
enum CotisImagePath {
    StringPath(String),
    StaticPath(&'static str),
}

/// Error returned when constructing path-based image wrappers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CotisImageError {
    /// The path was absolute; Cotis image paths must be relative.
    NotRelativePath,
    /// The provided path was empty.
    EmptyPath,
}

/// JPEG image path wrapper.
#[derive(Clone)]
pub struct JPEGImage {
    path: CotisImagePath,
}

impl JPEGImage {
    /// Creates a JPEG image from a relative path.
    ///
    /// # Errors
    ///
    /// Returns:
    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
    pub fn new(path: &str) -> Result<Self, CotisImageError> {
        if path.is_empty() {
            return Err(CotisImageError::EmptyPath);
        }
        if path.starts_with("/") {
            return Err(CotisImageError::NotRelativePath);
        }
        Ok(Self {
            path: CotisImagePath::StringPath(path.to_string()),
        })
    }

    /// Creates a JPEG image from a relative static path.
    ///
    /// # Panics
    ///
    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
    pub const fn new_const(path: &'static str) -> Self {
        if path.is_empty() {
            panic!("CotisImageError::EmptyPath");
        }
        if path.as_bytes()[0] == b'/' {
            panic!("CotisImageError::NotRelativePath");
        }

        Self {
            path: CotisImagePath::StaticPath(path),
        }
    }

    /// Returns the original image path.
    pub fn get_path(&self) -> &str {
        match &self.path {
            CotisImagePath::StringPath(s) => s,
            CotisImagePath::StaticPath(s) => s,
        }
    }
}

/// SVG image path wrapper.
#[derive(Clone)]
pub struct SVGImage {
    path: CotisImagePath,
}

impl SVGImage {
    /// Creates an SVG image from a relative path.
    ///
    /// # Errors
    ///
    /// Returns:
    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
    pub fn new(path: &str) -> Result<Self, CotisImageError> {
        if path.is_empty() {
            return Err(CotisImageError::EmptyPath);
        }
        if path.starts_with("/") {
            return Err(CotisImageError::NotRelativePath);
        }
        Ok(Self {
            path: CotisImagePath::StringPath(path.to_string()),
        })
    }

    /// Creates an SVG image from a relative static path.
    ///
    /// # Panics
    ///
    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
    pub const fn new_const(path: &'static str) -> Self {
        if path.is_empty() {
            panic!("CotisImageError::EmptyPath");
        }
        if path.as_bytes()[0] == b'/' {
            panic!("CotisImageError::NotRelativePath");
        }

        Self {
            path: CotisImagePath::StaticPath(path),
        }
    }

    /// Returns the original image path.
    pub fn get_path(&self) -> &str {
        match &self.path {
            CotisImagePath::StringPath(s) => s,
            CotisImagePath::StaticPath(s) => s,
        }
    }
}

/// PNG image path wrapper.
#[derive(Clone)]
pub struct PNGImage {
    path: CotisImagePath,
}

impl PNGImage {
    /// Creates a PNG image from a relative path.
    ///
    /// # Errors
    ///
    /// Returns:
    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
    pub fn new(path: &str) -> Result<Self, CotisImageError> {
        if path.is_empty() {
            return Err(CotisImageError::EmptyPath);
        }
        if path.starts_with("/") {
            return Err(CotisImageError::NotRelativePath);
        }
        Ok(Self {
            path: CotisImagePath::StringPath(path.to_string()),
        })
    }

    /// Creates a PNG image from a relative static path.
    ///
    /// # Panics
    ///
    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
    pub const fn new_const(path: &'static str) -> Self {
        if path.is_empty() {
            panic!("CotisImageError::EmptyPath");
        }
        if path.as_bytes()[0] == b'/' {
            panic!("CotisImageError::NotRelativePath");
        }

        Self {
            path: CotisImagePath::StaticPath(path),
        }
    }

    /// Returns the original image path.
    pub fn get_path(&self) -> &str {
        match &self.path {
            CotisImagePath::StringPath(s) => s,
            CotisImagePath::StaticPath(s) => s,
        }
    }
}