Skip to main content

einvoice_xpath_eval/
item.rs

1//! Items: nodes and atomic values.
2
3use std::borrow::Cow;
4
5use einvoice_xdm::AtomicValue;
6
7use crate::tree::Node;
8
9/// An item of a sequence.
10#[derive(Debug, Clone)]
11pub enum Item {
12    /// A node.
13    Node(Node),
14    /// An atomic value.
15    Atomic(AtomicValue),
16}
17
18impl Item {
19    /// The atomized value: the typed value of a node, the value itself otherwise.
20    #[must_use]
21    pub fn atomize(&self) -> AtomicValue {
22        match self {
23            Self::Node(node) => node.typed_value(),
24            Self::Atomic(value) => value.clone(),
25        }
26    }
27
28    /// The string value (`fn:string`).
29    #[must_use]
30    pub fn string_value(&self) -> Cow<'_, str> {
31        match self {
32            Self::Node(node) => node.string_value(),
33            Self::Atomic(value) => match value {
34                AtomicValue::String(s) | AtomicValue::UntypedAtomic(s) | AtomicValue::AnyUri(s) => {
35                    Cow::Borrowed(s)
36                }
37                other => Cow::Owned(other.to_string()),
38            },
39        }
40    }
41
42    /// The node, if the item is one.
43    #[must_use]
44    pub const fn as_node(&self) -> Option<&Node> {
45        match self {
46            Self::Node(node) => Some(node),
47            Self::Atomic(_) => None,
48        }
49    }
50}
51
52impl From<Node> for Item {
53    fn from(node: Node) -> Self {
54        Self::Node(node)
55    }
56}
57
58impl From<AtomicValue> for Item {
59    fn from(value: AtomicValue) -> Self {
60        Self::Atomic(value)
61    }
62}