prqlc_parser/
generic.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// Generic definitions of various AST items.
//
// This was added in a big refactor by a generous-but-new contributor, and
// hasn't been used much since, and I'm not sure carries its weight. So we
// could consider rolling back to only concrete implementations to delayer the
// code.
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Inclusive-inclusive range.
/// Missing bound means unbounded range.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
pub struct Range<T> {
    pub start: Option<T>,
    pub end: Option<T>,
}

impl<T> Range<T> {
    pub const fn unbounded() -> Self {
        Range {
            start: None,
            end: None,
        }
    }

    pub fn try_map<U, E, F: Fn(T) -> Result<U, E>>(self, f: F) -> Result<Range<U>, E> {
        Ok(Range {
            start: self.start.map(&f).transpose()?,
            end: self.end.map(f).transpose()?,
        })
    }

    pub fn map<U, F: Fn(T) -> U>(self, f: F) -> Range<U> {
        Range {
            start: self.start.map(&f),
            end: self.end.map(f),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema)]
pub enum InterpolateItem<T> {
    String(String),
    Expr {
        expr: Box<T>,
        format: Option<String>,
    },
}

impl<T> InterpolateItem<T> {
    pub fn map<U, F: Fn(T) -> U>(self, f: F) -> InterpolateItem<U> {
        match self {
            Self::String(s) => InterpolateItem::String(s),
            Self::Expr { expr, format } => InterpolateItem::Expr {
                expr: Box::new(f(*expr)),
                format,
            },
        }
    }

    pub fn try_map<U, E, F: Fn(T) -> Result<U, E>>(self, f: F) -> Result<InterpolateItem<U>, E> {
        Ok(match self {
            Self::String(s) => InterpolateItem::String(s),
            Self::Expr { expr, format } => InterpolateItem::Expr {
                expr: Box::new(f(*expr)?),
                format,
            },
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SwitchCase<T> {
    pub condition: T,
    pub value: T,
}