rte/
lib.rs

1//! # parse
2//! parse expression string to ast, then you can use any time library to transform moment.
3//! check the [doc](https://github.com/Frezc/relative-time-expression)
4//! ```
5//! use rte::*;
6//! let ast = parse("+ M\\M");
7//! assert_eq!(ast.unwrap(), Expression {
8//!    r#type: "Expression".to_string(),
9//!    start: 0,
10//!    end: 5,
11//!    body: vec![
12//!      Manipulation::Offset {
13//!        r#type: "Offset".to_string(),
14//!        op: "+".to_string(),
15//!        number: 1,
16//!        unit: "M".to_string(),
17//!        start: 0,
18//!        end: 3,
19//!      },
20//!      Manipulation::Period {
21//!        r#type: "Period".to_string(),
22//!        op: "\\".to_string(),
23//!        unit: "M".to_string(),
24//!        start: 3,
25//!       end: 5,
26//!      }
27//!    ],
28//!  });
29//! ```
30//! # standardize
31//! format expression to standard
32//! ```
33//! assert_eq!(rte::standardize(" now   - 1   d /w").unwrap(), "now-d/w")
34//! ```
35//!
36//! # encode
37//! parse expression string to ast, then you can use any time library to transform moment.
38//! ```
39//! use rte::*;
40//! assert_eq!(
41//!  encode(&InputExpression {
42//!    r#type: "Expression".to_string(),
43//!    body: vec![InputManipulation::Offset {
44//!      r#type: "Offset".to_string(),
45//!      op: "+".to_string(),
46//!      number: 12,
47//!      unit: "M".to_string(),
48//!    }],
49//!  }),
50//!  "now+12M"
51//! )
52//! ```
53
54pub mod types;
55pub mod error;
56pub mod tokenizer;
57pub mod parser;
58pub mod encode;
59pub use types::*;
60pub use error::*;
61pub use tokenizer::*;
62pub use parser::*;
63pub use encode::*;
64
65
66pub fn parse(exp: &str) -> Result<Expression, Error> {
67  Parser::parse(&Tokenizer::parse(exp)?)
68}
69
70
71pub fn standardize(exp: &str) -> Result<String, Error> {
72  Ok(encode(&parse(exp)?.into()))
73}