synthphonia/text/parsing/
month.rs

1use std::collections::HashSet;
2
3use chrono::{NaiveDate, Datelike, Month};
4use itertools::Itertools;
5use regex::Regex;
6
7use crate::{galloc::AllocForExactSizeIter, expr::{Expr, ops}, impl_basic, impl_op1_opt, new_op1_opt, value::{ConstValue, Value}};
8use crate::galloc::TryAllocForExactSizeIter;
9use super::ParsingOp;
10
11
12impl_basic!(ParseMonth, "month.parse");
13impl crate::forward::enumeration::Enumerator1 for ParseMonth {
14    fn enumerate(&self, this: &'static ops::Op1Enum, exec: &'static crate::forward::executor::Executor, opnt: [usize; 1]) -> Result<(), ()> { Ok(())}
15}
16
17impl_op1_opt!(ParseMonth, "month.parse",
18    Str -> Int { |s1| -> Option<i64> {
19        todo!()
20    }}
21);
22
23impl ParsingOp for ParseMonth {
24
25    fn parse_into(&self, input: &'static str) -> std::vec::Vec<(&'static str, ConstValue)> {
26        let months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
27        let mut result: Vec<(&'static str, ConstValue)> = Vec::new();
28        let month_literal = "(?<month>Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)";
29        let regex5 = Regex::new(month_literal.to_string().as_str()).unwrap();
30        let iter = regex5.captures_iter(input);
31        for m in iter {
32            if m.name("month").is_some() {
33                let month = months.iter().enumerate().find(|(i, s)| ***s == m.name("month").unwrap().as_str()[0..3]).unwrap().0 as u32 + 1;
34                result.push((m.get(0).unwrap().as_str(), month.into()));
35            }
36        }
37        result
38    }
39
40}