alma 0.1.1

A Bevy-native modal text editor with Vim-style navigation.
Documentation
//! Sanitized editor status-line text.

use crate::fs_utils::EscapedDisplayText;
use std::fmt::{Display, Formatter};

/// Informational status text safe for bottom-line rendering.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StatusInfoText {
    /// One-line status text.
    value: String,
}

impl StatusInfoText {
    /// Builds trusted static status text.
    #[must_use]
    pub fn trusted_static(text: &'static str) -> Self {
        Self {
            value: text.to_owned(),
        }
    }

    /// Builds trusted generated status text from a boundary-owned formatter.
    #[must_use]
    pub(crate) const fn trusted_generated(text: String) -> Self {
        Self { value: text }
    }

    /// Builds status text from already-escaped display text.
    #[must_use]
    pub fn from_escaped_display(text: &EscapedDisplayText) -> Self {
        Self {
            value: text.as_str().to_owned(),
        }
    }

    /// Builds status text from dynamic display text after escaping controls.
    #[must_use]
    pub fn escaped_display(text: impl AsRef<str>) -> Self {
        let escaped = EscapedDisplayText::from_display_text(text);
        Self::from_escaped_display(&escaped)
    }

    /// Reads the safe status text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.value
    }
}

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

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

    #[test]
    fn escaped_display_text_is_single_line() {
        let status = StatusInfoText::escaped_display("bad\nname\tbidi\u{202E}.txt");

        assert_eq!(status.as_str(), "bad\\nname\\tbidi\\u{202E}.txt");
        assert!(!status.as_str().contains('\n'));
        assert!(!status.as_str().contains('\t'));
        assert!(!status.as_str().contains('\u{202E}'));
    }
}