summaryrefslogtreecommitdiffstats
path: root/rust/src/python/parsing.rs
blob: 48fa64c93d398696b583dabe96952396552baa1e (plain)
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
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::PyDate;
use pyo3::types::PyDateTime;
use pyo3::types::PyTime;

use crate::parsing::Parser;
use crate::python::types::{Duration, FixedTimezone};

#[pyfunction]
pub fn parse_iso8601(py: Python, input: &str) -> PyResult<PyObject> {
    let parsed = Parser::new(input).parse();

    match parsed {
        Ok(parsed) => match (parsed.datetime, parsed.duration, parsed.second_datetime) {
            (Some(datetime), None, None) => match (datetime.has_date, datetime.has_time) {
                (true, true) => match datetime.offset {
                    Some(offset) => {
                        let dt = PyDateTime::new(
                            py,
                            datetime.year as i32,
                            datetime.month as u8,
                            datetime.day as u8,
                            datetime.hour as u8,
                            datetime.minute as u8,
                            datetime.second as u8,
                            datetime.microsecond,
                            Some(
                                Py::new(py, FixedTimezone::new(offset, datetime.tzname))?
                                    .to_object(py)
                                    .extract(py)?,
                            ),
                        )?;

                        Ok(dt.to_object(py))
                    }
                    None => {
                        let dt = PyDateTime::new(
                            py,
                            datetime.year as i32,
                            datetime.month as u8,
                            datetime.day as u8,
                            datetime.hour as u8,
                            datetime.minute as u8,
                            datetime.second as u8,
                            datetime.microsecond,
                            None,
                        )?;

                        Ok(dt.to_object(py))
                    }
                },
                (true, false) => {
                    let dt = PyDate::new(
                        py,
                        datetime.year as i32,
                        datetime.month as u8,
                        datetime.day as u8,
                    )?;

                    Ok(dt.to_object(py))
                }
                (false, true) => match datetime.offset {
                    Some(offset) => {
                        let dt = PyTime::new(
                            py,
                            datetime.hour as u8,
                            datetime.minute as u8,
                            datetime.second as u8,
                            datetime.microsecond,
                            Some(
                                Py::new(py, FixedTimezone::new(offset, datetime.tzname))?
                                    .to_object(py)
                                    .extract(py)?,
                            ),
                        )?;

                        Ok(dt.to_object(py))
                    }
                    None => {
                        let dt = PyTime::new(
                            py,
                            datetime.hour as u8,
                            datetime.minute as u8,
                            datetime.second as u8,
                            datetime.microsecond,
                            None,
                        )?;

                        Ok(dt.to_object(py))
                    }
                },
                (_, _) => Err(exceptions::PyValueError::new_err(
                    "Parsing error".to_string(),
                )),
            },
            (None, Some(duration), None) => Ok(Py::new(
                py,
                Duration::new(
                    Some(duration.years),
                    Some(duration.months),
                    Some(duration.weeks),
                    Some(duration.days),
                    Some(duration.hours),
                    Some(duration.minutes),
                    Some(duration.seconds),
                    Some(duration.microseconds),
                ),
            )?
            .to_object(py)),
            (_, _, _) => Err(exceptions::PyValueError::new_err(
                "Not yet implemented".to_string(),
            )),
        },
        Err(error) => Err(exceptions::PyValueError::new_err(error.to_string())),
    }
}