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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
mod bracket_span;
mod command_span;
mod inline;
mod macro_span;
mod semantic_span;

use super::constants::EMPTY;
use crate::Result;
pub use bracket_span::BracketSpan;
use bracket_span::{bracket_span, starts_with_bracket_span};
use command_span::command_span;
pub use command_span::{CommandSpan, ImageOption, VideoOption, VideoPlatform};
use inline::inline;
use macro_span::macro_span;
pub use macro_span::{MacroSpan, RubyOption};
use nom::{
  bytes::complete::{tag, take},
  character::complete::char,
  combinator::all_consuming,
  number::complete::float,
};
pub use semantic_span::SemanticSpan;
use semantic_span::{semantic_span, starts_with_sematic_span};

#[derive(Debug, PartialEq)]
pub enum Span {
  Semantic(SemanticSpan),
  Bracket(BracketSpan),
  Macro(MacroSpan),
  Command(CommandSpan),
  Inline(String),
}

#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub enum Size {
  Auto,
  Numeric(f32),
  Pixel(f32),
  Rem(f32),
  Percent(f32),
}

#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub enum Alignment {
  Auto,
  Start,
  End,
  Left,
  Center,
  Right,
}

#[derive(Debug, PartialEq)]
pub enum SizeLevel {
  One,
  Two,
  Three,
  Four,
  Five,
}

#[derive(Debug, PartialEq)]
pub enum Color {
  Hex(u8, u8, u8),
  Rgb(u8, u8, u8),
  Hsl(u8, u8, u8),
  Raw(String),
}

impl From<usize> for SizeLevel {
  fn from(source: usize) -> SizeLevel {
    use SizeLevel::*;

    match source {
      1 => One,
      2 => Two,
      3 => Three,
      4 => Four,
      5 => Five,
      _ => std::unreachable!(),
    }
  }
}

impl From<&SizeLevel> for usize {
  fn from(source: &SizeLevel) -> Self {
    use SizeLevel::*;

    match source {
      One => 1,
      Two => 2,
      Three => 3,
      Four => 4,
      Five => 5,
    }
  }
}

impl Default for Size {
  fn default() -> Self {
    Self::Auto
  }
}

impl From<&str> for Size {
  fn from(source: &str) -> Self {
    fn numeric(input: &str) -> Result<f32> {
      let (input, numeric) = float(input)?;

      Ok((input, numeric))
    };

    let is_match = |pattern, input| -> Result {
      let _ = all_consuming(pattern)(input)?;

      Ok((EMPTY, ()))
    };

    match numeric(source) {
      Ok((input, numeric)) => {
        if let Ok(_) = is_match(tag("px"), input) {
          Size::Pixel(numeric)
        } else if let Ok(_) = is_match(tag("rem"), input) {
          Size::Rem(numeric)
        } else if let Ok(_) = is_match(tag("%"), input) {
          Size::Percent(numeric)
        } else {
          Size::Numeric(numeric)
        }
      }
      _ => Default::default(),
    }
  }
}

impl Default for Color {
  fn default() -> Self {
    Self::Hex(0, 0, 0)
  }
}

impl From<&str> for Color {
  // TODO(Danuel): RGB, HSL 문법 추가
  fn from(source: &str) -> Self {
    fn hex(input: &str) -> Result<Color> {
      let (input, _) = char('#')(input)?;
      let (input, r) = take(2usize)(input)?;
      let (input, g) = take(2usize)(input)?;
      let (input, b) = take(2usize)(input)?;
      let _ = all_consuming(|input| -> Result { Ok((input, ())) })(input)?;
      let color = Color::Hex(r.parse().unwrap(), g.parse().unwrap(), b.parse().unwrap());

      Ok((EMPTY, color))
    };

    if let Ok((_, color)) = hex(source) {
      color
    } else {
      Default::default()
    }
  }
}

impl Default for Alignment {
  fn default() -> Self {
    Self::Auto
  }
}

impl From<&str> for Alignment {
  fn from(source: &str) -> Self {
    match source {
      "start" => Alignment::Start,
      "end" => Alignment::End,
      "left" => Alignment::Left,
      "center" => Alignment::Center,
      "right" => Alignment::Right,
      _ => Alignment::default(),
    }
  }
}

pub fn span_list(mut input: &str) -> Vec<Span> {
  let mut list = vec![];
  if input.is_empty() {
    return list;
  }

  while let Ok((next_input, span)) = span(input) {
    list.push(span);
    if next_input.is_empty() {
      break;
    }
    input = next_input;
  }

  list
}

fn span(input: &str) -> Result<Span> {
  if let Ok((input, span)) = semantic_span(input) {
    Ok((input, Span::Semantic(span)))
  } else if let Ok((input, span)) = bracket_span(input) {
    Ok((input, Span::Bracket(span)))
  } else if let Ok((input, span)) = command_span(input) {
    Ok((input, Span::Command(span)))
  } else if let Ok((input, span)) = macro_span(input) {
    Ok((input, Span::Macro(span)))
  } else {
    let (input, inline) = inline(input)?;

    Ok((input, Span::Inline(inline)))
  }
}