docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! Source location tracking for error reporting.

use std::fmt;

/// A span representing a location in template source text.
///
/// Tracks the part name (e.g., "word/document.xml" for DOCX integration,
/// or "<input>" for plain string parsing) and byte offsets.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Span {
    /// The name of the part/source being parsed.
    ///
    /// For Phase 3 plain string parsing, this will typically be "<input>".
    /// Phase 5 will use actual DOCX part names like "word/document.xml".
    pub part: String,

    /// Byte offset where this span starts (inclusive).
    pub start: usize,

    /// Byte offset where this span ends (exclusive).
    pub end: usize,
}

impl Span {
    /// Creates a new span.
    pub fn new(part: impl Into<String>, start: usize, end: usize) -> Self {
        Self {
            part: part.into(),
            start,
            end,
        }
    }

    /// Creates a span covering the entire input.
    #[cfg(test)]
    pub fn whole(part: impl Into<String>, len: usize) -> Self {
        Self {
            part: part.into(),
            start: 0,
            end: len,
        }
    }

    /// Returns the length of this span in bytes.
    #[cfg(test)]
    pub fn len(&self) -> usize {
        self.end.saturating_sub(self.start)
    }

    /// Returns whether this span is empty.
    #[cfg(test)]
    pub fn is_empty(&self) -> bool {
        self.start >= self.end
    }
}

impl fmt::Display for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}..{}", self.part, self.start, self.end)
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]

    use super::*;

    #[test]
    fn test_span_new() {
        let span = Span::new("test.html", 5, 10);
        assert_eq!(span.part, "test.html");
        assert_eq!(span.start, 5);
        assert_eq!(span.end, 10);
    }

    #[test]
    fn test_span_whole() {
        let span = Span::whole("input", 100);
        assert_eq!(span.start, 0);
        assert_eq!(span.end, 100);
        assert_eq!(span.len(), 100);
    }

    #[test]
    fn test_span_len() {
        let span = Span::new("test", 10, 20);
        assert_eq!(span.len(), 10);
    }

    #[test]
    fn test_span_is_empty() {
        let empty = Span::new("test", 10, 10);
        assert!(empty.is_empty());

        let non_empty = Span::new("test", 10, 15);
        assert!(!non_empty.is_empty());
    }

    #[test]
    fn test_span_display() {
        let span = Span::new("word/document.xml", 42, 58);
        assert_eq!(span.to_string(), "word/document.xml:42..58");
    }
}