prqlc_parser/
generic.rs

1/// Generic definitions of various AST items.
2//
3// This was added in a big refactor by a generous-but-new contributor, and
4// hasn't been used much since, and I'm not sure carries its weight. So we
5// could consider rolling back to only concrete implementations to delayer the
6// code.
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10/// Inclusive-inclusive range.
11/// Missing bound means unbounded range.
12#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
13pub struct Range<T> {
14    pub start: Option<T>,
15    pub end: Option<T>,
16}
17
18impl<T> Range<T> {
19    pub const fn unbounded() -> Self {
20        Range {
21            start: None,
22            end: None,
23        }
24    }
25
26    pub fn try_map<U, E, F: Fn(T) -> Result<U, E>>(self, f: F) -> Result<Range<U>, E> {
27        Ok(Range {
28            start: self.start.map(&f).transpose()?,
29            end: self.end.map(f).transpose()?,
30        })
31    }
32
33    pub fn map<U, F: Fn(T) -> U>(self, f: F) -> Range<U> {
34        Range {
35            start: self.start.map(&f),
36            end: self.end.map(f),
37        }
38    }
39}
40
41#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema)]
42pub enum InterpolateItem<T> {
43    String(String),
44    Expr {
45        expr: Box<T>,
46        format: Option<String>,
47    },
48}
49
50impl<T> InterpolateItem<T> {
51    pub fn map<U, F: Fn(T) -> U>(self, f: F) -> InterpolateItem<U> {
52        match self {
53            Self::String(s) => InterpolateItem::String(s),
54            Self::Expr { expr, format } => InterpolateItem::Expr {
55                expr: Box::new(f(*expr)),
56                format,
57            },
58        }
59    }
60
61    pub fn try_map<U, E, F: Fn(T) -> Result<U, E>>(self, f: F) -> Result<InterpolateItem<U>, E> {
62        Ok(match self {
63            Self::String(s) => InterpolateItem::String(s),
64            Self::Expr { expr, format } => InterpolateItem::Expr {
65                expr: Box::new(f(*expr)?),
66                format,
67            },
68        })
69    }
70}
71
72#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema)]
73pub struct SwitchCase<T> {
74    pub condition: T,
75    pub value: T,
76}