xee_xpath/
itemable.rs

1use xee_interpreter::sequence::Item;
2
3use crate::{error::Result, DocumentHandle, Documents};
4
5// TODO: if the underlying APIs take a sequence we could turn this into
6// a sequenceable.
7
8/// Something that can be converted into an [`Item`] using a [`Document`]
9///
10/// This can be an item, but also a [`DocumentHandle`]
11pub trait Itemable {
12    /// Convert this itemable into an [`Item`]
13    fn to_item(&self, documents: &Documents) -> Result<Item>;
14}
15
16impl Itemable for xot::Node {
17    fn to_item(&self, _documents: &Documents) -> Result<Item> {
18        Ok(Item::Node(*self))
19    }
20}
21
22impl Itemable for DocumentHandle {
23    fn to_item(&self, documents: &Documents) -> Result<Item> {
24        // TODO: This unwrap is not great; we should turn this into an error
25        let documents_ref = documents.documents.borrow();
26        let document = documents_ref.get_by_handle(*self).unwrap();
27        Ok(Item::Node(document.root()))
28    }
29}
30
31impl Itemable for &Item {
32    fn to_item(&self, _documents: &Documents) -> Result<Item> {
33        Ok(Clone::clone(self))
34    }
35}