gpui-phosphor 0.1.0

An unofficial GPUI component and asset source for Phosphor Icons
Documentation
/// A visual weight from the Phosphor icon family.
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Weight {
    /// The standard outline weight.
    #[default]
    Regular,
    /// The thinnest outline weight.
    Thin,
    /// A light outline weight.
    Light,
    /// A heavy outline weight.
    Bold,
    /// A solid filled weight.
    Fill,
    /// An outline with a translucent secondary layer.
    Duotone,
}

impl Weight {
    /// All six Phosphor weights, in the order used by the crate’s feature list.
    pub const ALL: [Self; 6] = [
        Self::Regular,
        Self::Thin,
        Self::Light,
        Self::Bold,
        Self::Fill,
        Self::Duotone,
    ];

    /// Return the lowercase name used in asset paths and Cargo features.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Regular => "regular",
            Self::Thin => "thin",
            Self::Light => "light",
            Self::Bold => "bold",
            Self::Fill => "fill",
            Self::Duotone => "duotone",
        }
    }

    /// Find a weight by its lowercase name.
    #[must_use]
    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|weight| weight.as_str() == name)
    }

    /// Whether this weight’s Cargo feature is enabled in the current build.
    #[must_use]
    pub const fn is_enabled(self) -> bool {
        match self {
            Self::Regular => cfg!(feature = "regular"),
            Self::Thin => cfg!(feature = "thin"),
            Self::Light => cfg!(feature = "light"),
            Self::Bold => cfg!(feature = "bold"),
            Self::Fill => cfg!(feature = "fill"),
            Self::Duotone => cfg!(feature = "duotone"),
        }
    }

    /// Return the first enabled weight for use by a default component.
    #[must_use]
    pub const fn default_enabled() -> Self {
        if cfg!(feature = "regular") {
            Self::Regular
        } else if cfg!(feature = "thin") {
            Self::Thin
        } else if cfg!(feature = "light") {
            Self::Light
        } else if cfg!(feature = "bold") {
            Self::Bold
        } else if cfg!(feature = "fill") {
            Self::Fill
        } else if cfg!(feature = "duotone") {
            Self::Duotone
        } else {
            // Cargo permits --no-default-features with no replacement weight.
            // The component will report the missing feature through Icon::path.
            Self::Regular
        }
    }
}

impl std::fmt::Display for Weight {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}