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
use std::fs;

use crate::ast::Solution;
use jwalk::WalkDir;
use std::option::Option::Some;

pub mod ast;
mod lex;
pub mod msbuild;
mod parser;

#[macro_use]
extern crate lalrpop_util;
extern crate jwalk;
extern crate petgraph;

lalrpop_mod!(
    #[allow(clippy::all)]
    #[allow(unused)]
    pub solp
);

/// Consume provides parsed solution consumer
pub trait Consume {
    /// Called in case of success parsing
    fn ok(&mut self, path: &str, solution: &Solution);
    /// Called on error
    fn err(&self, path: &str);
    /// Whether to use debug mode (usually just print AST into console)
    fn is_debug(&self) -> bool;
}

/// parse parses single solution file specified by path.
pub fn parse(path: &str, consumer: &mut dyn Consume) {
    match fs::read_to_string(path) {
        Ok(contents) => {
            if let Some(solution) = parser::parse_str(&contents, consumer.is_debug()) {
                consumer.ok(path, &solution);
            } else {
                consumer.err(path);
            }
        }
        Err(e) => eprintln!("{} - {}", path, e),
    }
}

/// scan parses directory specified by path. recursively
/// it finds all files with sln extension and parses them.
/// returns the number of scanned solutions
pub fn scan(path: &str, extension: &str, consumer: &mut dyn Consume) -> usize {
    let iter = WalkDir::new(path).skip_hidden(false).follow_links(false);

    let ext = String::from(".") + extension.trim_start_matches('.');

    iter.into_iter()
        .filter(Result::is_ok)
        .map(Result::unwrap)
        .filter(|f| f.file_type().is_file())
        .map(|f| f.path().to_str().unwrap_or("").to_string())
        .filter(|p| p.ends_with(&ext))
        .inspect(|fp| parse(&fp, consumer))
        .count()
}

fn cut_from_back_until(s: &str, ch: char, skip: usize) -> &str {
    let cut = cut_count(s, ch, skip);
    &s[..s.len() - cut]
}

fn cut_count(s: &str, ch: char, skip: usize) -> usize {
    let mut counter = 0;

    let count = s
        .chars()
        .rev()
        .take_while(|c| {
            if *c == ch {
                counter += 1;
            }
            counter <= skip
        })
        .count();

    if count == s.len() {
        s.len()
    } else {
        count + 1 // Last ch
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cut_from_back_until_necessary_chars_more_then_skip_plus_one() {
        // Arrange
        let s = "a.b.c.d";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("a.b", c);
    }

    #[test]
    fn cut_from_back_until_has_necessary_chars_to_skip() {
        // Arrange
        let s = "a.b.c";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("a", c);
    }

    #[test]
    fn cut_from_back_until_necessary_chars_to_skip_following_each_other() {
        // Arrange
        let s = "a..b.c";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("a.", c);
    }

    #[test]
    fn cut_from_back_until_only_necessary_chars() {
        // Arrange
        let s = "...";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!(".", c);
    }

    #[test]
    fn cut_from_back_until_only_necessary_chars_eq_skip_plus_one() {
        // Arrange
        let s = "..";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("", c);
    }

    #[test]
    fn cut_from_back_until_only_necessary_chars_eq_skip() {
        // Arrange
        let s = ".";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("", c);
    }

    #[test]
    fn cut_from_back_until_chars_to_skip_not_enough() {
        // Arrange
        let s = "a.b";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("", c);
    }

    #[test]
    fn cut_from_back_until_chars_to_skip_not_present() {
        // Arrange
        let s = "ab";

        // Act
        let c = cut_from_back_until(s, '.', 1);

        // Assert
        assert_eq!("", c);
    }
}