asciidoc_parser/content/
content.rs

1//! Describes the content of a non-compound block after any relevant
2//! [substitutions] have been performed.
3//!
4//! [substitutions]: https://docs.asciidoctor.org/asciidoc/latest/subs/
5
6use crate::{Span, strings::CowStr};
7
8/// Describes the annotated content of a block after any relevant
9/// [substitutions] have been performed.
10///
11/// This is typically used to represent the main body of block types that don't
12/// contain other blocks, such as [`SimpleBlock`] or [`RawDelimitedBlock`].
13///
14/// [substitutions]: https://docs.asciidoctor.org/asciidoc/latest/subs/
15/// [`SimpleBlock`]: crate::blocks::SimpleBlock
16/// [`RawDelimitedBlock`]: crate::blocks::RawDelimitedBlock
17#[derive(Clone, Debug, Eq, PartialEq)]
18pub struct Content<'src> {
19    /// The original [`Span`] from which this content was derived.
20    original: Span<'src>,
21
22    /// The possibly-modified text after substititions have been performed.
23    pub(crate) rendered: CowStr<'src>,
24}
25
26impl<'src> Content<'src> {
27    /// Returns the original span from which this [`Content`] was derived.
28    ///
29    /// This is the source text before any substitions have been applied.
30    pub fn original(&self) -> Span<'src> {
31        self.original
32    }
33
34    /// Returns the final text after all substitutions have been applied.
35    pub fn rendered(&'src self) -> &'src str {
36        self.rendered.as_ref()
37    }
38
39    /// Returns `true` if `self` contains no text.
40    pub fn is_empty(&self) -> bool {
41        self.rendered.as_ref().is_empty()
42    }
43}
44
45impl<'src> From<Span<'src>> for Content<'src> {
46    fn from(span: Span<'src>) -> Self {
47        Self {
48            original: span,
49            rendered: CowStr::from(span.data()),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    #![allow(clippy::unwrap_used)]
57
58    mod is_empty {
59        #[test]
60        fn basic_empty_span() {
61            let content = crate::content::Content::from(crate::Span::default());
62            assert!(content.is_empty());
63        }
64
65        #[test]
66        fn basic_non_empty_span() {
67            let content = crate::content::Content::from(crate::Span::new("blah"));
68            assert!(!content.is_empty());
69        }
70    }
71}