taitan_orm_parser/template_parser/structs/values/
text_value.rs

1use crate::template_parser::structs::text::Text;
2use crate::template_parser::structs::values::maybe_value::MaybeValue;
3use crate::template_parser::to_sql::SqlSegment;
4use crate::ToSqlSegment;
5use nom::branch::alt;
6use nom::combinator::map;
7use nom::IResult;
8
9#[derive(Debug, Clone, PartialEq)]
10pub enum TextValue {
11    Value(Text),
12    Maybe(MaybeValue),
13}
14
15impl TextValue {
16    pub fn parse(input: &str) -> IResult<&str, TextValue> {
17        alt((
18            map(Text::parse, TextValue::Value),
19            map(MaybeValue::parse, TextValue::Maybe),
20        ))(input)
21    }
22}
23
24impl From<Text> for TextValue {
25    fn from(v: Text) -> Self {
26        Self::Value(v)
27    }
28}
29
30impl From<MaybeValue> for TextValue {
31    fn from(v: MaybeValue) -> Self {
32        Self::Maybe(v)
33    }
34}
35
36impl ToSqlSegment for TextValue {
37    fn gen_sql_segment(&self) -> SqlSegment {
38        match self {
39            TextValue::Value(v) => SqlSegment::Simple(v.to_string()),
40            TextValue::Maybe(maybe_value) => maybe_value.gen_sql_segment(),
41        }
42    }
43}