fn serialize()

in cli/src/highlight.rs [67:116]


    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let entry_count = self.ansi_styles.iter().filter(|i| i.is_some()).count();
        let mut map = serializer.serialize_map(Some(entry_count))?;
        for (i, style) in self.ansi_styles.iter().enumerate() {
            let highlight = Highlight::from_usize(i).unwrap();
            if highlight == Highlight::Unknown {
                break;
            }
            if let Some(style) = style {
                let color = style.foreground.map(|color| match color {
                    Color::Black => json!("black"),
                    Color::Blue => json!("blue"),
                    Color::Cyan => json!("cyan"),
                    Color::Green => json!("green"),
                    Color::Purple => json!("purple"),
                    Color::Red => json!("red"),
                    Color::White => json!("white"),
                    Color::Yellow => json!("yellow"),
                    Color::RGB(r, g, b) => json!(format!("#{:x?}{:x?}{:x?}", r, g, b)),
                    Color::Fixed(n) => json!(n),
                });
                if style.is_bold || style.is_italic || style.is_underline {
                    let mut entry = HashMap::new();
                    if let Some(color) = color {
                        entry.insert("color", color);
                    }
                    if style.is_bold {
                        entry.insert("bold", Value::Bool(true));
                    }
                    if style.is_italic {
                        entry.insert("italic", Value::Bool(true));
                    }
                    if style.is_underline {
                        entry.insert("underline", Value::Bool(true));
                    }
                    map.serialize_entry(&highlight, &entry)?;
                } else if let Some(color) = color {
                    map.serialize_entry(&highlight, &color)?;
                } else {
                    map.serialize_entry(&highlight, &Value::Null)?;
                }
            } else {
                map.serialize_entry(&highlight, &Value::Null)?;
            }
        }
        map.end()
    }