openprxl 0.1.0

A Rust spreadsheet library inspired by Python's openpyxl
Documentation
//! Font styling.

/// A font description.
#[derive(Debug, Clone, Default)]
pub struct Font {
    pub(crate) name: Option<String>,
    pub(crate) size: Option<f64>,
    pub(crate) bold: bool,
    pub(crate) italic: bool,
    pub(crate) underline: Option<UnderlineStyle>,
    pub(crate) strike: bool,
    pub(crate) color: Option<String>,
    pub(crate) family: Option<u8>,
    pub(crate) scheme: Option<String>,
}

impl PartialEq for Font {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.size.map(|f| f.to_bits()) == other.size.map(|f| f.to_bits())
            && self.bold == other.bold
            && self.italic == other.italic
            && self.underline == other.underline
            && self.strike == other.strike
            && self.color == other.color
            && self.family == other.family
            && self.scheme == other.scheme
    }
}

impl Eq for Font {}

impl std::hash::Hash for Font {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.size.map(|f| f.to_bits()).hash(state);
        self.bold.hash(state);
        self.italic.hash(state);
        self.underline.hash(state);
        self.strike.hash(state);
        self.color.hash(state);
        self.family.hash(state);
        self.scheme.hash(state);
    }
}

impl Font {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
        self.name = Some(name.into());
        self
    }

    pub fn size(mut self, size: f64) -> Self {
        self.size = Some(size);
        self
    }

    pub fn bold(mut self, bold: bool) -> Self {
        self.bold = bold;
        self
    }

    pub fn italic(mut self, italic: bool) -> Self {
        self.italic = italic;
        self
    }

    pub fn underline(mut self, underline: UnderlineStyle) -> Self {
        self.underline = Some(underline);
        self
    }

    pub fn strike(mut self, strike: bool) -> Self {
        self.strike = strike;
        self
    }

    /// Set the font color as an RGB hex string such as `FF0000`.
    pub fn color<S: Into<String>>(mut self, color: S) -> Self {
        self.color = Some(color.into());
        self
    }

    pub fn family(mut self, family: u8) -> Self {
        self.family = Some(family);
        self
    }

    pub fn scheme<S: Into<String>>(mut self, scheme: S) -> Self {
        self.scheme = Some(scheme.into());
        self
    }

    pub fn get_name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn get_size(&self) -> Option<f64> {
        self.size
    }

    pub fn is_bold(&self) -> bool {
        self.bold
    }

    pub fn is_italic(&self) -> bool {
        self.italic
    }

    pub fn get_underline(&self) -> Option<&UnderlineStyle> {
        self.underline.as_ref()
    }

    pub fn is_strike(&self) -> bool {
        self.strike
    }

    pub fn get_color(&self) -> Option<&str> {
        self.color.as_deref()
    }
}

/// Underline styles.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnderlineStyle {
    Single,
    Double,
    SingleAccounting,
    DoubleAccounting,
}

impl std::fmt::Display for UnderlineStyle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnderlineStyle::Single => write!(f, "single"),
            UnderlineStyle::Double => write!(f, "double"),
            UnderlineStyle::SingleAccounting => write!(f, "singleAccounting"),
            UnderlineStyle::DoubleAccounting => write!(f, "doubleAccounting"),
        }
    }
}