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
use nom::{
    IResult,
    character::complete::*,
    bytes::complete::*,
};
use nom::branch::*;
use nom::combinator::*;
use nom::sequence::*;
use nom::multi::*;
use std::time::Duration;

use super::{
    Comment,
    DocComment,
};

// #[inline(always)]
pub fn parentheses_comment<'r>(input: &'r str) -> IResult<&'r str, &'r str> {
    let parser = preceded(
        char('('),
        is_not("\n\r)"),
    );

    terminated(
        parser,
        char(')'),
    )(input)
}

pub struct WithComments<'r, O> {
    pub comments: Option<Vec<&'r str>>,
    pub value: O,
}

// #[inline(always)]
pub fn with_parentheses_comments<
    'r,
    T: FnMut(&'r str,) -> IResult<&'r str, O>,
    O,
>(
    parser: T,
) -> impl FnMut(&'r str,) -> IResult<&'r str, WithComments<'r, O>> {
    let parser = pair(
        parser,
        opt(many1(parentheses_comment)),
    );

    let parser = pair(
        opt(many1(parentheses_comment)),
        parser,
    );

    map(
        parser,
        |(mut comments, (value, more_comments))| {
            // Merge all comments into one optional vec
            if let Some(more_comments) = more_comments {
                comments = Some([
                    comments.unwrap_or_else(|| vec![]),
                    more_comments,
                ].concat())
            }

            WithComments {
                comments,
                value,
            }
        }
    )
}

// #[inline(always)]
pub fn seimcolon_comment<'r>(input: &'r str,) -> IResult<&'r str, &'r str> {
    preceded(
        char(';'),
        not_line_ending,
    )(input)
}

// #[inline(always)]
pub fn comment<'r>(input: &'r str) -> IResult<&'r str, Comment<'r>> {
    map(
        alt((seimcolon_comment, parentheses_comment)),
        |comment| Comment(comment),
    )(input)
}

// #[inline(always)]
pub fn doc_comment<'r>(input: &'r str) -> IResult<&'r str, DocComment<'r>> {
    map_opt(
        preceded(
            char(';'),
            separated_pair(
                take_until(":"),
                char(':'),
                preceded(
                    space0,
                    not_line_ending,
                ),
            ),
        ),
        |(key, value)| {
            let doc = match key {
                "FLAVOR" => DocComment::GCodeFlavor(value),
                "TIME" => DocComment::PrintTime(Duration::from_secs(value.parse().ok()?)),
                "Filament used" => filament_used(value).ok()?.1,
                "Layer height" => DocComment::LayerHeight { millis: value.parse().ok()? },
                _ => return None
            };

            Some(doc)
        }
    )(input)
}

// #[inline(always)]
pub fn filament_used<'r>(input: &'r str,) -> IResult<&'r str, DocComment<'r>> {
    map_opt(
        terminated(
            take_until("m"),
            char('m'),
        ),
        |s: &'r str| {
            let meters = s.parse().ok()?;
            Some(DocComment::FilamentUsed { meters })
        }
    )(input)
}