text-fx 0.4.0

A collection of text processing utilities for Rust.
Documentation
/// A wrapper for text with an associated style label, for diagnostics or formatting.
///
/// `StyledText` is a simple struct that pairs a style name (such as `"error"`, `"info"`, etc.)
/// with a text value. It is useful for tagging text with semantic meaning for later formatting,
/// such as coloring or styling in diagnostics or logs.
///
/// The [`Debug`] implementation prints the text wrapped in XML-like tags using the style name.
///
/// # Examples
///
/// ```
/// use text_fx::style::StyledText;
///
/// let styled = StyledText::new("warning", "be careful!");
/// assert_eq!(format!("{:?}", styled), "<warning>be careful!</warning>");
/// ```
pub struct StyledText<'a> {
    style: &'a str,
    text: &'a str,
}

impl<'a> StyledText<'a> {
    /// Create a new `StyledText` with the given style and text.
    pub fn new(style: &'a str, text: &'a str) -> Self {
        Self { style, text }
    }
}

impl<'a> std::fmt::Debug for StyledText<'a> {
    /// Formats the styled text as `<style>text</style>`.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<{}>{}</{}>", self.style, self.text, self.style)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_styled_text() {
        let styled = StyledText {
            style: "error",
            text: "test",
        };
        let s = format!("{:?}", styled);
        assert_eq!(s, "<error>test</error>");
    }
}