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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#[macro_use]
extern crate strum_macros;

use clap::ArgMatches;

use sic_image_engine::engine::Instr;

use crate::errors::SicCliOpsError;
use crate::operations::{
    extend_index_tree_with_unification, IndexTree, IndexedOps, Op, OperationId,
};

pub mod errors;
pub mod operations;

pub fn build_ast_from_matches(
    matches: &ArgMatches,
    tree: &mut IndexTree,
) -> Result<Vec<Instr>, SicCliOpsError> {
    use strum::IntoEnumIterator;

    let operations = OperationId::iter();
    ast_extend_with_operation(tree, matches, operations)?;

    // Build!
    ast_from_index_tree(tree)
}

fn ast_extend_with_operation<T: IntoIterator<Item = OperationId>>(
    tree: &mut IndexTree,
    matches: &ArgMatches,
    operations: T,
) -> Result<(), SicCliOpsError> {
    for operation in operations {
        let argc = operation.takes_number_of_arguments();
        let ops = mk_ops(operation, matches);
        extend_index_tree_with_unification(tree, ops, argc)?;
    }

    Ok(())
}

fn mk_ops(op: OperationId, matches: &ArgMatches) -> Option<IndexedOps> {
    let argc = op.takes_number_of_arguments();
    match argc {
        0 => op_valueless!(matches, op),
        _n => op_with_values!(matches, op),
    }
}

fn ast_from_index_tree(tree: &mut IndexTree) -> Result<Vec<Instr>, SicCliOpsError> {
    tree.iter()
        .map(|(_index, op)| match op {
            Op::Bare(id) => {
                let empty: &[&str; 0] = &[];
                id.mk_statement(empty)
            }
            Op::WithValues(OperationId::Diff, values) => {
                // HACK: From Pest we receive back a string including quotation marks, but
                //       the terminal doesn't; we'll have to figure out how we can instruct Pest
                //       to just give back the string contents without quotation marks
                let mut values = values.to_vec();

                #[allow(clippy::needless_range_loop)]
                for i in 0..values.len() {
                    values[i] = format!("\"{}\"", values[i]);
                }

                (OperationId::Diff).mk_statement(&values)
            }
            Op::WithValues(id, values) => id.mk_statement(values),
        })
        .collect::<Result<Vec<Instr>, SicCliOpsError>>()
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use sic_cli::cli::cli;
    use sic_image_engine::engine::Instr;
    use sic_image_engine::ImgOp;

    use super::*;

    macro_rules! assert_match {
        ($iter:expr, $clause:pat, $assert:expr) => {{
            match $iter.next().unwrap() {
                $clause => $assert,
                err => panic!(format!(
                    "Assertion: {} failed. Value found was: {:?}",
                    stringify!($assert),
                    err
                )),
            }
        }};
    }

    #[test]
    fn build_from_args_all() {
        let input = "sic -i in -o out \
                     --blur 1 \
                     --brighten 2 \
                     --contrast 3 \
                     --crop 0 0 2 2 \
                     --filter3x3 0 1 2 3 4 5 6 7 8 \
                     --flip-horizontal \
                     --flip-vertical \
                     --grayscale \
                     --hue-rotate -90 \
                     --invert \
                     --resize 10 10 \
                     --rotate90 \
                     --rotate180 \
                     --rotate270 \
                     --unsharpen 1.5 1";

        let input = input.split_ascii_whitespace();
        let matches = cli().get_matches_from(input);
        let mut tree: IndexTree = BTreeMap::new();
        let ast = build_ast_from_matches(&matches, &mut tree);
        let ast = ast.unwrap();
        let mut iter = ast.iter();

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Blur(n)),
            sic_testing::approx_eq_f32!(*n, 1f32)
        );

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Brighten(n)),
            assert_eq!(*n, 2i32)
        );

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Contrast(n)),
            sic_testing::approx_eq_f32!(*n, 3f32)
        );

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Crop(n)),
            assert_eq!(*n, (0u32, 0u32, 2u32, 2u32))
        );

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Filter3x3(n)),
            assert_eq!(*n, [0f32, 1f32, 2f32, 3f32, 4f32, 5f32, 6f32, 7f32, 8f32])
        );

        assert_match!(iter, Instr::Operation(ImgOp::FlipHorizontal), ());

        assert_match!(iter, Instr::Operation(ImgOp::FlipVertical), ());

        assert_match!(iter, Instr::Operation(ImgOp::GrayScale), ());

        assert_match!(
            iter,
            Instr::Operation(ImgOp::HueRotate(n)),
            assert_eq!(*n, -90i32)
        );

        assert_match!(iter, Instr::Operation(ImgOp::Invert), ());

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Resize(n)),
            assert_eq!(*n, (10u32, 10u32))
        );

        assert_match!(iter, Instr::Operation(ImgOp::Rotate90), ());

        assert_match!(iter, Instr::Operation(ImgOp::Rotate180), ());

        assert_match!(iter, Instr::Operation(ImgOp::Rotate270), ());

        assert_match!(
            iter,
            Instr::Operation(ImgOp::Unsharpen(n)),
            assert_eq!(*n, (1.5f32, 1i32))
        );

        assert_eq!(iter.next(), None);
    }

    #[test]
    fn mk_ops_0() {
        let input = "sic -i in -o out \
                     --rotate180";

        let input = input.split_ascii_whitespace();
        let matches = cli().get_matches_from(input);

        let ops = mk_ops(OperationId::Rotate180, &matches);
        let ops = ops.unwrap();
        let (i, v) = ops.get(0).unwrap();
        assert_eq!(*i, 5usize);
        assert_eq!(*v, Op::Bare(OperationId::Rotate180))
    }

    #[test]
    fn mk_ops_n() {
        let input = "sic -i in -o out --unsharpen 1.5 2";

        let input = input.split_ascii_whitespace();
        let matches = cli().get_matches_from(input);

        // note that at mk_ops no unification of arguments has taken place.
        let ops = mk_ops(OperationId::Unsharpen, &matches);

        let ops = ops.unwrap();
        let (i, v) = ops.get(0).unwrap();
        assert_eq!(*i, 6usize);
        assert_eq!(
            *v,
            Op::WithValues(OperationId::Unsharpen, vec![String::from("1.5")])
        );

        let (i, v) = ops.get(1).unwrap();
        assert_eq!(*i, 7usize);
        assert_eq!(
            *v,
            Op::WithValues(OperationId::Unsharpen, vec![String::from("2")])
        );
    }

    #[test]
    fn ast_from_index_tree_empty() {
        let mut tree: IndexTree = BTreeMap::new();
        let ast = ast_from_index_tree(&mut tree);

        assert!(ast.unwrap().is_empty())
    }

    #[test]
    fn ast_from_index_tree_with_vals() {
        let mut tree: IndexTree = BTreeMap::new();
        tree.insert(
            1,
            Op::WithValues(OperationId::Brighten, vec![String::from("10")]),
        );
        tree.insert(2, Op::Bare(OperationId::FlipV));
        tree.insert(
            3,
            Op::WithValues(OperationId::HueRotate, vec![String::from("-90")]),
        );
        let ast = ast_from_index_tree(&mut tree);

        let iter = ast.unwrap();
        let mut iter = iter.iter();

        // can't assert eq, because Operation does not implement Eq, since f32 doesn't support it
        assert_match!(
            iter,
            Instr::Operation(ImgOp::Brighten(n)),
            assert_eq!(*n, 10)
        );

        assert_match!(iter, Instr::Operation(ImgOp::FlipVertical), ());

        assert_match!(
            iter,
            Instr::Operation(ImgOp::HueRotate(n)),
            assert_eq!(*n, -90)
        );

        assert_eq!(iter.next(), None);
    }
}