roxml 0.4.1

Read-only, zero-copy, lazy XML reader for no_std environments, without allocation
Documentation
// Copyright © 2026 Stephan Kunz
//! Body section node types: `BodyNode`, `BodyComment`, `BodyPI`.

use core::{fmt, ops::Range};

use crate::{
	helpers::{comment_text, next_body_sibling, pi_content, pi_target},
	xml_element::XmlElement,
	xml_text::XmlText,
};

/// The set of node types that may appear as children of an XML element.
///
/// Whitespace-only text between markup is not reported as a node.
///
/// # Examples
/// ```
/// use roxml::prelude::*;
///
/// let reader = XmlReader::parse("<a>text<!-- note --><?app go?><b/></a>");
/// let root = reader.root_element()?;
///
/// let kinds: Vec<&str> = root
///     .children()
///     .map(|node| match node {
///         BodyNode::Element(_) => "element",
///         BodyNode::Text(_) => "text",
///         BodyNode::Comment(_) => "comment",
///         BodyNode::PI(_) => "pi",
///     })
///     .collect();
/// assert_eq!(kinds, ["text", "comment", "pi", "element"]);
/// # Ok::<(), roxml::Error>(())
/// ```
#[derive(Clone, Debug)]
pub enum BodyNode<'xml> {
	/// A regular element: `<tag …>…</tag>` or `<tag/>`.
	Element(XmlElement<'xml>),
	/// Character data or a CDATA section.
	Text(XmlText<'xml>),
	/// An XML comment: `<!-- … -->`.
	Comment(BodyComment<'xml>),
	/// A processing instruction: `<?target content?>`.
	PI(BodyPI<'xml>),
}

impl<'xml> BodyNode<'xml> {
	/// Byte range of this node in the source text.
	#[must_use]
	pub fn range(&self) -> Range<usize> {
		match self {
			Self::Element(e) => e.range(),
			Self::Text(t) => t.range(),
			Self::Comment(c) => c.range(),
			Self::PI(pi) => pi.range(),
		}
	}

	/// Next sibling node within the parent element, or `None`.
	///
	/// Same as calling `next_sibling()` on the wrapped node.
	///
	/// # Examples
	/// ```
	/// use roxml::prelude::*;
	///
	/// let reader = XmlReader::parse("<a><b/>text<c/></a>");
	/// let root = reader.root_element()?;
	///
	/// let mut count = 0;
	/// let mut node = root.first_child();
	/// while let Some(current) = node {
	///     count += 1;
	///     node = current.next_sibling();
	/// }
	/// assert_eq!(count, 3);
	/// # Ok::<(), roxml::Error>(())
	/// ```
	#[must_use]
	pub fn next_sibling(&self) -> Option<Self> {
		match self {
			Self::Element(e) => e.next_sibling(),
			Self::Text(t) => t.next_sibling(),
			Self::Comment(c) => c.next_sibling(),
			Self::PI(pi) => pi.next_sibling(),
		}
	}
}

/// A comment node inside an element body: `<!-- … -->`.
#[derive(Clone)]
pub struct BodyComment<'xml> {
	pub(crate) xml_text: &'xml str,
	/// Byte range of the full `<!-- … -->` in the source.
	pub(crate) range: Range<usize>,
	pub(crate) content_end: usize,
}

impl<'xml> BodyComment<'xml> {
	pub(crate) fn new(xml_text: &'xml str, range: Range<usize>, content_end: usize) -> Self {
		BodyComment {
			xml_text,
			range,
			content_end,
		}
	}

	/// Comment body with `<!--` and `-->` stripped.
	#[must_use]
	pub fn text(&self) -> &'xml str {
		comment_text(&self.xml_text[self.range.clone()])
	}

	/// Byte range of this node in the source text.
	#[must_use]
	pub fn range(&self) -> Range<usize> {
		self.range.clone()
	}

	/// Next sibling node within the parent element, or `None`.
	#[must_use]
	pub fn next_sibling(&self) -> Option<BodyNode<'xml>> {
		next_body_sibling(self.xml_text, self.range.end, self.content_end)
	}
}

/// A processing instruction inside an element body: `<?target content?>`.
#[derive(Clone)]
pub struct BodyPI<'xml> {
	xml_text: &'xml str,
	range: Range<usize>,
	content_end: usize,
}

impl<'xml> BodyPI<'xml> {
	pub(crate) fn new(xml_text: &'xml str, range: Range<usize>, content_end: usize) -> Self {
		BodyPI {
			xml_text,
			range,
			content_end,
		}
	}

	/// PI target name (the token immediately after `<?`).
	#[must_use]
	pub fn target(&self) -> &'xml str {
		pi_target(self.xml_text, &self.range)
	}

	/// PI content: everything after the target and its trailing whitespace,
	/// up to (but not including) `?>`.
	#[must_use]
	pub fn content(&self) -> &'xml str {
		pi_content(self.xml_text, &self.range)
	}

	/// Byte range of this node in the source text.
	#[must_use]
	pub fn range(&self) -> Range<usize> {
		self.range.clone()
	}

	/// Next sibling node within the parent element, or `None`.
	#[must_use]
	pub fn next_sibling(&self) -> Option<BodyNode<'xml>> {
		next_body_sibling(self.xml_text, self.range.end, self.content_end)
	}
}

impl fmt::Debug for BodyComment<'_> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("BodyComment")
			.field("text", &self.text())
			.field("range", &self.range)
			.finish()
	}
}

impl fmt::Debug for BodyPI<'_> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("BodyPI")
			.field("target", &self.target())
			.field("content", &self.content())
			.field("range", &self.range)
			.finish()
	}
}