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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use sp_std::{vec, vec::*};

pub type StrLike = Vec<u8>;

pub fn ensure_str_err(condition: bool, err_message: &'static str) -> Result<(), &'static str> {
    if !condition {
        return Err(err_message)
    }
    Ok(())
}

// Constants
const WHITESPACE_MATRIX: [u8; 4] = [b' ', b'\t', b'\r', b'\n'];
const ARGS_SEPARATOR: u8 = b',';
const ARGS_START: u8 = b'(';
const ARGS_END: u8 = b')';

// Helper functions

// Checks if signature is no-empty and ends correctly
fn check_overall_sanity(signature: StrLike) -> Result<(), &'static str> {
    let cloned = trim_whitespace(signature);
    // make sure schedule is not empty
    // probably irrelevant since there is already a check for that
    let last_char = cloned.last();
    ensure_str_err(
        last_char.is_some(),
        "Signature sanity failed - can't be empty",
    )?;
    // make sure the schedule ends correctly and remove ending character or panic
    let ends_correctly = last_char.eq(&Some(&ARGS_END));
    ensure_str_err(
        ends_correctly,
        "Signature sanity failed - must end with ')'",
    )
}

// Trims all whitespace chars from io_schedule vector
pub fn trim_whitespace(input_string: StrLike) -> StrLike {
    let mut result = input_string;

    // checks if character is whitespace
    let is_whitespace = |x: &u8| WHITESPACE_MATRIX.contains(x);

    let mut i = 0;
    while i < result.len() {
        if is_whitespace(&result[i]) {
            result.remove(i);
        } else {
            i += 1;
        }
    }
    result
}

pub fn match_signature(signature: StrLike) -> Result<(StrLike, Vec<StrLike>), &'static str> {
    // Mutable variables
    let mut event_name: Option<StrLike> = None;
    let mut event_args: Vec<StrLike> = Vec::new();
    let mut current_word: StrLike = StrLike::new();

    // Actual signature decoding start
    check_overall_sanity(signature.clone())?;

    for &char in signature.iter() {
        match char {
            // Expect to start with an event name before the arguments start
            ARGS_START => {
                if current_word.is_empty() {
                    return Err("Signature must have non-empty event name")
                }
                event_name = Some(current_word.clone());
                current_word.clear();
            },
            // Before pushing next non-empty argument name make sure the name is already set
            ARGS_SEPARATOR | ARGS_END => {
                if current_word.is_empty() {
                    return Err("Signature's argument name can't be empty")
                }
                if event_name.is_none() {
                    return Err("Signature must start with event name")
                }
                event_args.push(current_word.clone());
                current_word.clear();
            },
            // Push non-special character to the current word
            _ => current_word.push(char),
        };
    }

    // Check sanity of result before returning
    let event_name_res = match event_name {
        Some(name) => Ok(name),
        _ => Err("Signature must have non-empty event name"),
    }?;

    Ok((event_name_res, event_args))
}

pub fn match_dfd(generic_dfd: StrLike) -> Result<Vec<Vec<StrLike>>, &'static str> {
    // Mutable variables
    let mut steps: Vec<Vec<StrLike>> = vec![vec![]];
    let mut curr_step_index: usize = 0;
    let mut current_word: StrLike = StrLike::new();

    // Actual generic_dfd decoding start
    let cloned = trim_whitespace(generic_dfd);
    // make sure schedule is not empty
    // probably irrelevant since there is already a check for that
    let last_char = cloned.last();
    ensure_str_err(
        last_char.is_some(),
        "Signature sanity failed - can't be empty",
    )?;

    for &char in cloned.iter() {
        match char {
            ARGS_START | ARGS_SEPARATOR | ARGS_END => {
                if !current_word.is_empty() {
                    if let Some(last_step) = steps.get_mut(curr_step_index) {
                        last_step.push(current_word.clone());
                    } else {
                        return Err("DFD Decoder - attempt to edit step at incorrect depth")
                    }
                    current_word.clear();
                }
                if char == ARGS_START {
                    curr_step_index += 1;
                    steps.push(vec![])
                }
                if char == ARGS_SEPARATOR {
                    current_word.clear();
                }
                if char == ARGS_END {
                    if let Some(new_step_index) = curr_step_index.checked_sub(1) {
                        curr_step_index = new_step_index;
                    } else {
                        return Err("DFD Decoder - attempt to edit step at incorrect depth")
                    }
                }
            },
            // Push non-special character to the current word
            _ => current_word.push(char),
        };
    }

    if curr_step_index != 0 {
        return Err("DFD Decoder - too many opening brackets")
    }

    // If last word didn't end with , or )
    if !current_word.is_empty() {
        if let Some(last_step) = steps.get_mut(curr_step_index) {
            last_step.push(current_word.clone());
        } else {
            return Err("DFD Decoder - attempt to edit step at incorrect depth")
        }
        current_word.clear();
    }

    // Trim empty steps (support additional depths of DFD, like ((((A,B))) )
    steps.retain(|step| !step.is_empty());

    // Reverse the steps order
    steps.reverse();

    Ok(steps)
}

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

    #[test]
    fn successfully_matches_signature_for_transfer_confirmation_event() {
        let valid_signature_transfer_confirm_event = "Transfer(from,to,value)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok((
                b"Transfer".to_vec(),
                vec![b"from".to_vec(), b"to".to_vec(), b"value".to_vec()]
            ))
        )
    }

    #[test]
    fn fails_to_match_signature_when_does_not_end_with_closing_bracket() {
        let valid_signature_transfer_confirm_event = "Transfer(from,to,value";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Err("Signature sanity failed - must end with ')'")
        )
    }

    #[test]
    fn fails_to_match_signature_when_too_many_closing_brackets() {
        let valid_signature_transfer_confirm_event = "Transfer(from,to,value))))";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(decode_res, Err("Signature's argument name can't be empty"))
    }

    #[test]
    fn fails_to_match_signature_when_empty_arg_name() {
        let valid_signature_transfer_confirm_event = "Transfer(from,to,)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(decode_res, Err("Signature's argument name can't be empty"))
    }

    #[test]
    fn fails_to_match_signature_when_no_opening_bracket() {
        let valid_signature_transfer_confirm_event = "Transfer,from,to,value)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(decode_res, Err("Signature must start with event name"))
    }

    #[test]
    fn fails_to_match_signature_when_empty_event_name() {
        let valid_signature_transfer_confirm_event = "(from,to,value)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res =
            match_signature(valid_signature_transfer_confirm_event.as_bytes().to_vec());
        assert_eq!(decode_res, Err("Signature must have non-empty event name"))
    }

    #[test]
    fn successfully_matches_dfd_for_3_parallel_events() {
        let valid_dfd = "A,B,C";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![vec![b"A".to_vec(), b"B".to_vec(), b"C".to_vec()]])
        )
    }

    #[test]
    fn successfully_matches_dfd_for_3_parallel_events_in_single_brackets() {
        let valid_dfd = "(A,B,C)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![vec![b"A".to_vec(), b"B".to_vec(), b"C".to_vec()]])
        )
    }

    #[test]
    fn successfully_matches_dfd_for_3_parallel_events_in_triple_brackets() {
        let valid_dfd = "(((A,B,C)))";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![vec![b"A".to_vec(), b"B".to_vec(), b"C".to_vec()]])
        )
    }

    #[test]
    fn fails_to_match_dfd_for_3_parallel_events_with_incorrect_closing_brackets() {
        let valid_dfd = "(A,B,C";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(decode_res, Err("DFD Decoder - too many opening brackets"))
    }

    #[test]
    fn fails_to_match_dfd_for_3_parallel_events_with_incorrect_opening_brackets() {
        let valid_dfd = "A,B,C)";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Err("DFD Decoder - attempt to edit step at incorrect depth")
        )
    }

    #[test]
    fn successfully_matches_dfd_for_3_sequential_events() {
        let valid_dfd = "(A(B(C)))";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![
                vec![b"C".to_vec()],
                vec![b"B".to_vec()],
                vec![b"A".to_vec()],
            ])
        )
    }

    #[test]
    fn successfully_matches_dfd_for_3_sequential_32b_long_events() {
        let valid_dfd = "(0909090909090909090909090909090909090909090909090909090909090909(\
            0606060606060606060606060606060606060606060606060606060606060606(\
                0303030303030303030303030303030303030303030303030303030303030303)))";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![
                vec![b"0303030303030303030303030303030303030303030303030303030303030303".to_vec()],
                vec![b"0606060606060606060606060606060606060606060606060606060606060606".to_vec()],
                vec![b"0909090909090909090909090909090909090909090909090909090909090909".to_vec()],
            ])
        )
    }

    #[test]
    fn successfully_matches_dfd_for_2_sequential_events_after_2_parallel() {
        let valid_dfd = "(D(C(A,B)))";
        // Important! If using .encode() instead of .as_bytes() + .to_vec(),
        //  SCALE adds additional byte "92" to event name
        let decode_res = match_dfd(valid_dfd.as_bytes().to_vec());
        assert_eq!(
            decode_res,
            Ok(vec![
                vec![b"A".to_vec(), b"B".to_vec()],
                vec![b"C".to_vec()],
                vec![b"D".to_vec()],
            ])
        )
    }
}