tekton 0.3.0

A blazingly fast code snippet sorter
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Functions related to creating and manipulating FriendlySnippets (JSON)

use super::snipmate_tekton::build_snippets_from_file;
use crate::{
    errors::TektonError,
    models::{
        friendly::{FriendlySnippetBody, FriendlySnippets, Table},
        snipmate::Snipmate,
    },
    utils::{clear_terminal, get_input, hash2ordered_string},
};
use regex::Regex;
use std::{collections::HashMap, fs};

const MISSING_PREFIX: &str = "File contains snippets with missing prefix field(s). Aborting.";

/// Function to handle Snipmate to JSON
///
/// Arguments:
/// - `lines`: the lines of Snipmate snippets to be converted
///
/// Returns:
/// - The converted JSON string representation or an error
pub fn compose_friendly_snippets(lines: Vec<String>) -> Result<String, TektonError> {
    let snips = build_snippets_from_file(lines);
    let friendlies = convert_snipmate_to_friendlysnippets(snips);
    let result = build_friendly_string(friendlies)?;
    Ok(result)
}

/// A function that takes the FriendlySnippet table and returns an ordered string representation or a TektonError
///
/// Arguments:
/// - `friendlies`: the snippets that will be converted to a string
///
/// Returns:
/// - The string with 1 to many snippets or an error.
pub fn build_friendly_string(friendlies: FriendlySnippets) -> Result<String, TektonError> {
    hash2ordered_string(&friendlies.snippets)
}

/// A function to convert an array of Snipmate structs to an array of FriendlySnippet structs
///
/// Arguments:
/// - `snips`: the vector of Snipmate snippets to be converted
///
/// Returns:
/// - The table of FriendlySnippets with 0 to many snippets (more or less).
pub fn convert_snipmate_to_friendlysnippets(snips: Vec<Snipmate>) -> FriendlySnippets {
    let mut count: usize = 0;
    let target = snips.len();
    let mut friendly_handle: FriendlySnippets = FriendlySnippets::new();

    for snippet in snips {
        let friendly_body = friendly_tekton(snippet);

        match serde_json::to_string_pretty(&friendly_body) {
            Ok(snip) => {
                count += 1;
                clear_terminal();
                println!(
                    "Snippet {} of {}:\n{}\n\nEnter name below:",
                    count, target, snip
                );
                let key = get_input();
                friendly_handle.snippets.insert(key, friendly_body);
            }
            Err(e) => {
                eprintln!("Match had an error in conversion ----> {}", e);
            }
        }
    }
    friendly_handle
}

fn friendly_tekton(snippet: Snipmate) -> FriendlySnippetBody {
    let prefix: Option<String> = Some(snippet.prefix);
    let mut body: Vec<String> = snippet.body;
    let mut description: Option<String> = None;
    let re = Regex::new(r##"\\""##).unwrap();

    // NOTE: Remove the whitespace for the very first line of the snippet
    body.reverse();
    if let Some(first_line) = body.pop() {
        let temp = first_line.trim_start().to_string();
        body.reverse();
        body.insert(0, temp);
    }

    if let Some(descrip) = &snippet.description {
        description = Some(re.replace_all(descrip, "").to_string());
    }

    FriendlySnippetBody::new(prefix, body, description)
}

/// Helper function to read the JSON as a `FriendlySnippets` struct
///
/// Arguments:
/// - `file_name`: a string of the input file
/// - `interactive`: a boolean indicating if the user will be invovled or not
pub fn read_in_json_snippets(
    file_name: &str,
    interactive: bool,
) -> Result<FriendlySnippets, TektonError> {
    let file_contents = fs::read_to_string(file_name)?;
    let snippets: Result<FriendlySnippets, serde_json::Error> =
        serde_json::from_str(&file_contents);
    match snippets {
        Ok(snippets) => Ok(snippets),
        Err(_) => match dynamically_read_json_snippets(file_contents, interactive) {
            Ok(snippets) => Ok(snippets),
            Err(e) => Err(e),
        },
    }
}

/// Helper function to read in the JSON for the `FriendlySnippets`, given uncertain JSON formatting
///
/// The [read_in_json_snippets] function should be preferred, however the ordering of fields in JSON isn't promised
/// and thus, this function builds the HashMap (backing the `FriendlySnippets` structure) by dynamically searching the
/// the table for the necessary fields and handling the missing ones more appropriately.
///
/// Arguments:
/// - `file`: a string of the input file
/// - `interactive`: a boolean indicating if the user will be invovled or not
///
/// Returns:
/// - Result of the snippets read in or an error
pub fn dynamically_read_json_snippets(
    file: String,
    interactive: bool,
) -> Result<FriendlySnippets, TektonError> {
    // The snippet table (what is being created/ read in)
    let mut snippets: Table = HashMap::new();
    // The blob of JSON from serde_json
    let json: serde_json::Value = serde_json::from_str(&file).unwrap();
    // The 'need to fix this' pile
    let mut snippets_to_fix: Vec<(String, FriendlySnippetBody)> = Vec::new();

    if let Some(obj) = json.as_object() {
        for (name, v) in obj {
            // Collect the lines of the snippet body (outsourced to a helper function)
            let body = retrieve_body(&v["body"]);

            // Create snippet body assuming no description
            let mut snip_body = FriendlySnippetBody::new(None, body, None);

            // If we find one, then update the structure
            if let Some(description) = v["description"].as_str() {
                if !description.is_empty() {
                    snip_body.description = Some(description.to_string());
                }
            }

            // Find the prefix or add to the 'fix later' vec
            if let Some(pref_candidate) = retrieve_prefix(&v["prefix"]) {
                snip_body.prefix = Some(pref_candidate);
            } else if interactive {
                // skip inserting a malformed snippet into the table, will fix later
                snippets_to_fix.push((name.to_string(), snip_body));
                continue;
            } else {
                // Return an error, this is useful for automation since errors can be collected
                // and addressed after the batch is finished.
                return Err(TektonError::Reason(MISSING_PREFIX.into()));
            }

            // Insert the finished snippet into the table
            snippets.insert(name.to_string(), snip_body);
        }
        // Congrats, it is later, now to fix the snippets
        correct_missing_prefix_snippets(&mut snippets_to_fix, &mut snippets);
    }

    Ok(FriendlySnippets { snippets })
}

/// A function to handle the correction of snippets that are missing their prefix
///
/// Arguments:
/// - `snippets_to_fix`: A mutable reference to a vector with the name and partial snippet body
/// - `snippets`: A mutable reference to the table that the corrected snippet will be inserted into
///
pub fn correct_missing_prefix_snippets(
    snippets_to_fix: &mut Vec<(String, FriendlySnippetBody)>,
    snippets: &mut Table,
) {
    if !snippets_to_fix.is_empty() {
        let mut count = 0;
        let total = snippets_to_fix.len();
        loop {
            if count == total {
                break;
            }

            if let Some((name, snip_body)) = snippets_to_fix.pop() {
                count += 1;
                println!("Fixing snippet {} of {}", count, total);
                let snip_body = handle_prompt_for_prefix(&name, snip_body);
                snippets.insert(name.to_string(), snip_body);
            }
        }
    }
}

/// A function that gets the users new prefix and updates the snippet, returning the properly formed body.
///
/// Arguments:
/// - `name`: a string slice representing the snippet name
/// - `snip_body`: the snippets partially formed body
///
/// Returns:
/// - `FriendlySnippetBody`: the updated snippet body
fn handle_prompt_for_prefix(name: &str, mut snip_body: FriendlySnippetBody) -> FriendlySnippetBody {
    println!(
        "---- Snippet: {} ---\n{}\n--------",
        name,
        serde_json::to_string_pretty(&snip_body).ok().unwrap() // This unwrap will probably steal our lunch money later on.
    );
    println!("Enter a prefix:");
    loop {
        let prefix_candidate = get_input();

        println!("Proceed? (y/n):");
        let resp = get_input().to_lowercase();

        if resp == "y" {
            snip_body.prefix = Some(prefix_candidate);
            break;
        }

        // The user wants to correct the input, so we re-prompt
        println!("Enter a new prefix: ");
    }
    clear_terminal();
    snip_body
}

/// Function to handle the parsing of the prefix for a JSON snippet
///
/// Arguments:
/// - `val`: a reference to a serde_json::Value that represents the snippet prefix
///
/// Returns:
/// - Optional string if the provided Value can be modeled as a string
fn retrieve_prefix(val: &serde_json::Value) -> Option<String> {
    val.as_str().map(|prefix| prefix.to_string())
}

/// Function to handle processing the body of a JSON snippet
///
/// Arguments:
/// - `val`:  a reference to a serde_json::Value
///
/// Returns:
/// - `Vec<String>` representing the 'content' of the snippet
pub fn retrieve_body(val: &serde_json::Value) -> Vec<String> {
    let mut body: Vec<String> = Vec::new();
    if let Some(lines) = val.as_array() {
        for line in lines.iter() {
            body.push(line.as_str().unwrap_or("").to_string());
        }
    } else {
        body.push(val.as_str().unwrap_or("").to_string());
    }

    body
}

/// Function that builds a string representing the snippets in sorted order
///
/// Arguments:
/// - `snippets`: The snippets to be sorted
///
/// Returns:
/// - A result with the sorted string or an error if there were zero (0) snippets
pub fn sort_friendly_snippets(snippets: FriendlySnippets) -> Result<String, TektonError> {
    let table = &snippets.snippets;
    match table.len() {
        0 => Err(TektonError::Reason(
            "Refusing to build string for 0 snippets".to_string(),
        )),
        _ => hash2ordered_string(table),
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    const INTERACTIVE: bool = false;

    #[test]
    fn standard_json_reading() {
        let file = r#"{
            "beta": {
              "prefix": "println",
              "body": ["println!(\"${1}\");"],
              "description": "println!(…);"
            },
            "alpha": {
              "prefix": "print",
              "body": ["print!(\"${1}\");"],
              "description": "print!(…);"
            }
          }"#
        .to_string();

        let snippets: Result<FriendlySnippets, serde_json::Error> = serde_json::from_str(&file);

        match snippets {
            Ok(res) => {
                let expected_struct = FriendlySnippetBody::new(
                    Some("println".to_string()),
                    vec!["println!(\"${1}\");".to_string()],
                    Some("println!(…);".to_string()),
                );
                assert_eq!(res.snippets.len(), 2);
                let item = res.snippets.get("beta").unwrap();
                assert_eq!(item.prefix, expected_struct.prefix);
                assert_eq!(item, &expected_struct);
            }
            Err(e) => {
                println!("Error: {}", e.to_string());
                assert!(false);
            }
        }
    }

    #[test]
    fn dyn_json_reading() {
        let file = r#"{
          "beta": {
            "prefix": "println",
            "body": ["println!(\"${1}\");"],
            "description": "println!(…);"
          },
          "alpha": {
            "prefix": "print",
            "body": ["print!(\"${1}\");"],
            "description": "print!(…);"
          }
        }"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(res) => {
                let expected_struct = FriendlySnippetBody::new(
                    Some("println".to_string()),
                    vec!["println!(\"${1}\");".to_string()],
                    Some("println!(…);".to_string()),
                );
                assert_eq!(res.snippets.len(), 2);
                let item = res.snippets.get("beta").unwrap();
                assert_eq!(item.prefix, expected_struct.prefix);
                assert_eq!(item, &expected_struct);
            }
            Err(e) => {
                println!("Error: {}", e.to_string());
                assert!(false);
            }
        }
    }

    #[test]
    fn jekyll() {
        let file = r#"{
        "Filter downcase": {
          "prefix": "downcase",
          "description": "String filter: downcase",
          "body": "| downcase }}"
        }}"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(res) => {
                let expected_struct = FriendlySnippetBody::new(
                    Some("downcase".to_string()),
                    vec!["| downcase }}".to_string()],
                    Some("String filter: downcase".to_string()),
                );
                assert_eq!(res.snippets.len(), 1);
                let item = res.snippets.get("Filter downcase").unwrap();
                assert_eq!(item.prefix, expected_struct.prefix);
                assert_eq!(item, &expected_struct);
            }
            Err(e) => {
                println!("Error: {}", e.to_string());
                assert!(false);
            }
        }
    }

    #[test]
    fn serialization() {
        let file = r#"{
    "Filter downcase": {
      "prefix": "downcase",
      "description": "String filter: downcase",
      "body": "| downcase }}"
    }}"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(res) => {
                let expected_struct = FriendlySnippetBody::new(
                    Some("downcase".to_string()),
                    vec!["| downcase }}".to_string()],
                    Some("String filter: downcase".to_string()),
                );
                assert_eq!(res.snippets.len(), 1);
                let item = res.snippets.get("Filter downcase").unwrap();
                assert_eq!(item.prefix, expected_struct.prefix);
                assert_eq!(item, &expected_struct);

                if let Ok(s) = serde_json::to_string(&res) {
                    const EXPECTED: &str = "{\"Filter downcase\":{\"prefix\":\"downcase\",\"body\":[\"| downcase }}\"],\"description\":\"String filter: downcase\"}}";
                    assert_eq!(s, EXPECTED);
                } else {
                    assert!(false);
                }
            }
            Err(e) => {
                println!("Error: {}", e.to_string());
                assert!(false);
            }
        }
    }

    #[test]
    fn serialization_with_empty_description() {
        let file = r#"{
    "Filter downcase": {
      "prefix": "downcase",
      "body": "| downcase }}"
    }}"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(res) => {
                let expected_struct = FriendlySnippetBody::new(
                    Some("downcase".to_string()),
                    vec!["| downcase }}".to_string()],
                    None,
                );
                assert_eq!(res.snippets.len(), 1);
                let item = res.snippets.get("Filter downcase").unwrap();
                assert_eq!(item.prefix, expected_struct.prefix);
                assert_eq!(item, &expected_struct);

                if let Ok(s) = serde_json::to_string(&res) {
                    const EXPECTED: &str = "{\"Filter downcase\":{\"prefix\":\"downcase\",\"body\":[\"| downcase }}\"]}}";
                    assert_eq!(s, EXPECTED);
                } else {
                    assert!(false);
                }
            }
            Err(e) => {
                println!("Error: {}", e.to_string());
                assert!(false);
            }
        }
    }
    #[test]
    fn serialization_missing_prefix() {
        let file = r#"{
    "Filter downcase": {
      "body": "| downcase }}"
    }}"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(_) => {
                assert!(false);
            }
            Err(e) => {
                assert_eq!(e, TektonError::Reason(MISSING_PREFIX.into()));
            }
        }
    }

    #[test]
    fn serialization_missing_prefix_throw_error() {
        let file = r#"{
        "Filter downcase": {
          "body": "| downcase }}"
        }
    }"#
        .to_string();

        let res = dynamically_read_json_snippets(file, INTERACTIVE);

        match res {
            Ok(_) => {
                assert!(false, "Failed to throw the error");
            }
            Err(e) => {
                assert_eq!(e, TektonError::Reason(MISSING_PREFIX.into()));
            }
        }
    }

    #[test]
    fn test_friendly_tekton() {
        let input: Vec<String> = vec![
            "snippet test".to_string(),
            "   test snippet".to_string(),
            "snippet test2 an epic description".to_string(),
            "    a second snippet".to_string(),
            "    with several".to_string(),
            "    lines.".to_string(),
        ];

        let snippets = build_snippets_from_file(input);

        assert_eq!(snippets.len(), 2);
        let vsnip = snippets[0].clone();

        let mut friendlies: FriendlySnippets = FriendlySnippets {
            snippets: HashMap::new(),
        };

        let mut names = vec!["test2".to_string(), "test1".to_string()];
        for snippet in snippets {
            let body = friendly_tekton(snippet);
            // Ugly, will return to clean up ... eventually
            friendlies
                .snippets
                .insert(names.pop().unwrap().to_string(), body);
        }

        assert_eq!(friendlies.snippets.len(), 2);
        let fsnip = friendlies.snippets.get("test1").unwrap();
        assert_eq!(fsnip.prefix, Some(vsnip.prefix));
        assert_eq!(
            fsnip.body.concat(),
            vsnip.body.concat().strip_prefix("   ").unwrap()
        );
        assert_eq!(fsnip.description, vsnip.description);
    }
}