1use alloc::vec::Vec;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6pub type Path<T> = Vec<(Part<crate::Spanned<T>>, Opt)>;
8
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[derive(Clone, Debug)]
12pub enum Part<I> {
13 Index(I),
15 Range(Option<I>, Option<I>),
17}
18
19impl<I> Default for Part<I> {
20 fn default() -> Self {
21 Self::Range(None, None)
22 }
23}
24
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31#[derive(Copy, Clone, Debug)]
32pub enum Opt {
33 Optional,
35 Essential,
37}
38
39impl<I> Part<I> {
40 pub fn map<J>(self, mut f: impl FnMut(I) -> J) -> Part<J> {
42 match self {
43 Self::Index(i) => Part::Index(f(i)),
44 Self::Range(l, h) => Part::Range(l.map(&mut f), h.map(f)),
45 }
46 }
47}
48
49impl Opt {
50 pub fn fail<T, E>(self, x: T, f: impl FnOnce(T) -> E) -> Result<T, E> {
52 match self {
53 Self::Optional => Ok(x),
54 Self::Essential => Err(f(x)),
55 }
56 }
57
58 pub fn collect<T, E>(self, iter: impl Iterator<Item = Result<T, E>>) -> Result<Vec<T>, E> {
61 match self {
62 Self::Optional => Ok(iter.filter_map(|x| x.ok()).collect()),
63 Self::Essential => iter.collect(),
64 }
65 }
66}