smv 0.5.0

Smart Move - An enhanced mv command with transformation capabilities
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use std::error::Error;
use std::fmt;

/// CNP Grammar Parser for SMV
/// Implements the full CNP grammar specification with filters, routes, and semantic groups

#[derive(Debug, Clone)]
pub struct CnpCommand {
    pub path: String,
    pub filters: Vec<Filter>,
    pub routes: Vec<Route>,
    pub flags: String,
    pub transform_command: Option<TransformCommand>,
    pub remove_command: Option<RemoveCommand>,
    pub case_insensitive: bool,
}

#[derive(Debug, Clone)]
pub struct TransformCommand {
    pub command_type: String,
    pub old_value: Option<String>,
    pub new_value: Option<String>,
}

#[derive(Debug, Clone)]
pub struct RemoveCommand {
    pub command_type: String, // "rm"
    pub preview: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
    Name(String),
    Type(FileType),
    Extension(String),
    SizeGreater(String),
    SizeLess(String),
    DepthGreater(usize),
    DepthLess(usize),
    ModifiedAfter(String),
    ModifiedBefore(String),
    AccessedAfter(String),
    AccessedBefore(String),
    Tag(String),
    Hash(String),
    Where(Vec<Filter>),
    For(SemanticGroup),
}

#[derive(Debug, Clone, PartialEq)]
pub enum FileType {
    File,
    Folder,
    Symlink,
    Other,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SemanticGroup {
    Notes,    // EXT:md + TYPE:file + common note paths
    Media,    // EXT:jpg/png/gif/webm/mp4 + TYPE:file
    Scripts,  // EXT:sh/py/rb/pl/rs + TYPE:file
    Projects, // TYPE:folder + NAME:src/build/docs
    Configs,  // EXT:conf/ini/yaml/toml/json + TYPE:file
}

#[derive(Debug, Clone)]
pub enum Route {
    To { tool: String, args: Vec<String> }, // TO:tool[:arg1,arg2] - delegate to another CNP tool with optional args
    Into(String),                           // INTO:file - write output to file
    Format(OutputFormat),                   // FORMAT:type - change output format
}

#[derive(Debug, Clone)]
pub enum OutputFormat {
    Json,
    Csv,
    Text,
    Yaml,
}

#[derive(Debug)]
pub struct GrammarParseError {
    pub message: String,
}

impl fmt::Display for GrammarParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "CNP Grammar Parse Error: {}", self.message)
    }
}

impl Error for GrammarParseError {}

pub struct CnpGrammarParser;

impl CnpGrammarParser {
    pub fn parse(args: &[String]) -> Result<CnpCommand, Box<dyn Error>> {
        let mut command = CnpCommand {
            path: ".".to_string(),
            filters: Vec::new(),
            routes: Vec::new(),
            flags: String::new(),
            transform_command: None,
            remove_command: None,
            case_insensitive: false,
        };

        let mut i = 0;
        while i < args.len() {
            let arg = &args[i];

            // Parse CNP filters (UPPERCASE keywords)
            if let Some(filter) = Self::parse_filter(arg)? {
                command.filters.push(filter);
                i += 1;
                continue;
            }

            // Parse CNP routes
            if let Some(route) = Self::parse_route(arg)? {
                command.routes.push(route);
                i += 1;
                continue;
            }

            // Parse SMV remove commands FIRST (before transform commands)
            if let Some(remove) = Self::parse_remove_command(args, &mut i)? {
                command.remove_command = Some(remove);
                continue;
            }

            // Parse SMV transform commands
            if let Some(transform) = Self::parse_transform_command(args, &mut i)? {
                command.transform_command = Some(transform);
                continue;
            }

            // Parse flags (starting with -)
            if let Some(flags) = arg.strip_prefix('-') {
                command.flags.push_str(flags);
                // Check for case-insensitive flag (both CNP standard 'ic' and SMV-specific 'i')
                if flags.contains("ic") || flags.contains('i') {
                    command.case_insensitive = true;
                }
                i += 1;
                continue;
            }

            // Check for glob patterns and convert them to appropriate filters
            if Self::is_glob_pattern(arg) {
                if let Some(filter) = Self::convert_glob_to_filter(arg)? {
                    command.filters.push(filter);
                }
                i += 1;
                continue;
            }

            // Parse path (first non-keyword, non-command argument)
            if command.path == "." && !arg.contains(':') && !arg.starts_with('-') && arg != "rm" {
                command.path = arg.clone();
                i += 1;
                continue;
            }

            i += 1;
        }

        Ok(command)
    }

    /// Check if an argument is a glob pattern
    fn is_glob_pattern(arg: &str) -> bool {
        arg.contains('*') || arg.contains('?') || arg.contains('[') || arg.contains('{')
    }

    /// Convert a glob pattern to an appropriate CNP filter
    fn convert_glob_to_filter(pattern: &str) -> Result<Option<Filter>, Box<dyn Error>> {
        // Handle common cases like *.ext -> EXT:ext
        if pattern.starts_with("*.") && !pattern[2..].contains('.') && !pattern[2..].contains('*') {
            let extension = &pattern[2..];
            return Ok(Some(Filter::Extension(extension.to_string())));
        }

        // Handle patterns like *filename* -> NAME:filename
        if pattern.starts_with('*') && pattern.ends_with('*') && pattern.len() > 2 {
            let name_part = &pattern[1..pattern.len() - 1];
            if !name_part.contains('*') && !name_part.contains('?') {
                return Ok(Some(Filter::Name(name_part.to_string())));
            }
        }

        // Handle patterns like filename* -> NAME:filename (startswith)
        if pattern.ends_with('*') && !pattern[..pattern.len() - 1].contains('*') {
            let name_part = &pattern[..pattern.len() - 1];
            return Ok(Some(Filter::Name(format!("{}*", name_part))));
        }

        // Handle patterns like *filename -> NAME:filename (endswith)
        if pattern.starts_with('*') && !pattern[1..].contains('*') {
            let name_part = &pattern[1..];
            return Ok(Some(Filter::Name(format!("*{}", name_part))));
        }

        // For complex patterns, use a generic name filter
        Ok(Some(Filter::Name(pattern.to_string())))
    }

    fn parse_filter(arg: &str) -> Result<Option<Filter>, Box<dyn Error>> {
        if !arg.contains(':')
            && !arg.starts_with("SIZE")
            && !arg.starts_with("DEPTH")
            && !arg.starts_with("MODIFIED")
            && !arg.starts_with("ACCESSED")
        {
            return Ok(None);
        }

        // Handle SIZE comparisons
        if let Some(stripped) = arg.strip_prefix("SIZE>") {
            return Ok(Some(Filter::SizeGreater(stripped.to_string())));
        }
        if let Some(stripped) = arg.strip_prefix("SIZE<") {
            return Ok(Some(Filter::SizeLess(stripped.to_string())));
        }

        // Handle DEPTH comparisons
        if let Some(stripped) = arg.strip_prefix("DEPTH>") {
            let value = stripped.parse::<usize>().map_err(|_| GrammarParseError {
                message: format!("Invalid depth value: {}", stripped),
            })?;
            return Ok(Some(Filter::DepthGreater(value)));
        }
        if let Some(stripped) = arg.strip_prefix("DEPTH<") {
            let value = stripped.parse::<usize>().map_err(|_| GrammarParseError {
                message: format!("Invalid depth value: {}", stripped),
            })?;
            return Ok(Some(Filter::DepthLess(value)));
        }

        // Handle timestamp comparisons
        if let Some(stripped) = arg.strip_prefix("MODIFIED>") {
            return Ok(Some(Filter::ModifiedAfter(stripped.to_string())));
        }
        if let Some(stripped) = arg.strip_prefix("MODIFIED<") {
            return Ok(Some(Filter::ModifiedBefore(stripped.to_string())));
        }
        if let Some(stripped) = arg.strip_prefix("ACCESSED>") {
            return Ok(Some(Filter::AccessedAfter(stripped.to_string())));
        }
        if let Some(stripped) = arg.strip_prefix("ACCESSED<") {
            return Ok(Some(Filter::AccessedBefore(stripped.to_string())));
        }

        // Handle colon-separated filters
        if let Some(colon_pos) = arg.find(':') {
            let key = &arg[..colon_pos];
            let value = &arg[colon_pos + 1..];

            match key {
                "NAME" => Ok(Some(Filter::Name(value.to_string()))),
                "TYPE" => {
                    let file_type = match value.to_lowercase().as_str() {
                        "file" => FileType::File,
                        "folder" | "dir" | "directory" => FileType::Folder,
                        "symlink" | "link" => FileType::Symlink,
                        "other" => FileType::Other,
                        _ => {
                            return Err(Box::new(GrammarParseError {
                                message: format!("Invalid file type: {value}"),
                            }));
                        }
                    };
                    Ok(Some(Filter::Type(file_type)))
                }
                "EXT" => Ok(Some(Filter::Extension(value.to_string()))),
                "TAG" => Ok(Some(Filter::Tag(value.to_string()))),
                "HASH" => Ok(Some(Filter::Hash(value.to_string()))),
                "FOR" => {
                    let semantic_group = match value.to_lowercase().as_str() {
                        "notes" => SemanticGroup::Notes,
                        "media" => SemanticGroup::Media,
                        "scripts" => SemanticGroup::Scripts,
                        "projects" => SemanticGroup::Projects,
                        "configs" => SemanticGroup::Configs,
                        _ => {
                            return Err(Box::new(GrammarParseError {
                                message: format!("Invalid semantic group: {value}"),
                            }));
                        }
                    };
                    Ok(Some(Filter::For(semantic_group)))
                }
                _ => Ok(None), // Unknown filter, ignore
            }
        } else {
            Ok(None)
        }
    }

    fn parse_route(arg: &str) -> Result<Option<Route>, Box<dyn Error>> {
        if !arg.contains(':') {
            return Ok(None);
        }

        if let Some(colon_pos) = arg.find(':') {
            let key = &arg[..colon_pos];
            let value = &arg[colon_pos + 1..];

            match key {
                "TO" => {
                    // Parse TO:tool or TO:tool:arg1,arg2 syntax
                    if let Some(tool_args_pos) = value.find(':') {
                        // Extended syntax: TO:tool:arg1,arg2
                        let tool = value[..tool_args_pos].to_string();
                        let args_str = &value[tool_args_pos + 1..];
                        let args: Vec<String> = args_str
                            .split(',')
                            .map(|s| s.trim().to_string())
                            .filter(|s| !s.is_empty())
                            .collect();
                        Ok(Some(Route::To { tool, args }))
                    } else {
                        // Basic syntax: TO:tool
                        Ok(Some(Route::To {
                            tool: value.to_string(),
                            args: Vec::new(),
                        }))
                    }
                }
                "INTO" => Ok(Some(Route::Into(value.to_string()))),
                "FORMAT" => {
                    let format = match value.to_lowercase().as_str() {
                        "json" => OutputFormat::Json,
                        "csv" => OutputFormat::Csv,
                        "text" | "txt" => OutputFormat::Text,
                        "yaml" | "yml" => OutputFormat::Yaml,
                        _ => {
                            return Err(Box::new(GrammarParseError {
                                message: format!("Invalid output format: {value}"),
                            }));
                        }
                    };
                    Ok(Some(Route::Format(format)))
                }
                _ => Ok(None), // Unknown route, ignore
            }
        } else {
            Ok(None)
        }
    }

    fn parse_transform_command(
        args: &[String],
        i: &mut usize,
    ) -> Result<Option<TransformCommand>, Box<dyn Error>> {
        if *i >= args.len() {
            return Ok(None);
        }

        let arg = &args[*i];

        // Check for SMV transform commands
        match arg.to_lowercase().as_str() {
            "change" => {
                if *i + 3 < args.len() && args[*i + 2] == "INTO" {
                    let old_value = args[*i + 1].clone();
                    let new_value = args[*i + 3].clone();
                    *i += 4;
                    return Ok(Some(TransformCommand {
                        command_type: "change".to_string(),
                        old_value: Some(old_value),
                        new_value: Some(new_value),
                    }));
                }
            }
            "regex" => {
                if *i + 3 < args.len() && args[*i + 2] == "INTO" {
                    let pattern = args[*i + 1].clone();
                    let replacement = args[*i + 3].clone();
                    *i += 4;
                    return Ok(Some(TransformCommand {
                        command_type: "regex".to_string(),
                        old_value: Some(pattern),
                        new_value: Some(replacement),
                    }));
                }
            }
            "snake" | "kebab" | "pascal" | "camel" | "title" | "lower" | "upper" | "clean" => {
                *i += 1;

                // Check if the next argument is a glob pattern and convert it to a filter
                if *i < args.len() && Self::is_glob_pattern(&args[*i]) {
                    // Don't consume the glob pattern here - let it be processed by the main parser loop
                }

                return Ok(Some(TransformCommand {
                    command_type: arg.clone(),
                    old_value: None,
                    new_value: None,
                }));
            }
            _ => {}
        }

        Ok(None)
    }

    /// Expand semantic groups into concrete filters
    pub fn expand_semantic_groups(filters: &[Filter]) -> Vec<Filter> {
        let mut expanded = Vec::new();

        for filter in filters {
            match filter {
                Filter::For(group) => match group {
                    SemanticGroup::Notes => {
                        expanded.push(Filter::Extension("md".to_string()));
                        expanded.push(Filter::Type(FileType::File));
                    }
                    SemanticGroup::Media => {
                        for ext in ["jpg", "png", "gif", "webm", "mp4", "jpeg", "webp", "svg"] {
                            expanded.push(Filter::Extension(ext.to_string()));
                        }
                        expanded.push(Filter::Type(FileType::File));
                    }
                    SemanticGroup::Scripts => {
                        for ext in ["sh", "py", "rb", "pl", "rs", "js", "ts", "bash", "zsh"] {
                            expanded.push(Filter::Extension(ext.to_string()));
                        }
                        expanded.push(Filter::Type(FileType::File));
                    }
                    SemanticGroup::Projects => {
                        expanded.push(Filter::Type(FileType::Folder));
                        for name in ["src", "build", "docs", "target", "dist", "bin"] {
                            expanded.push(Filter::Name(name.to_string()));
                        }
                    }
                    SemanticGroup::Configs => {
                        for ext in [
                            "conf", "ini", "yaml", "yml", "toml", "json", "config", "cfg",
                        ] {
                            expanded.push(Filter::Extension(ext.to_string()));
                        }
                        expanded.push(Filter::Type(FileType::File));
                    }
                },
                _ => expanded.push(filter.clone()),
            }
        }

        expanded
    }

    fn parse_remove_command(
        args: &[String],
        i: &mut usize,
    ) -> Result<Option<RemoveCommand>, Box<dyn Error>> {
        if *i >= args.len() {
            return Ok(None);
        }

        let arg = &args[*i];

        // Check for rm command
        if arg.to_lowercase() == "rm" {
            *i += 1;
            return Ok(Some(RemoveCommand {
                command_type: "rm".to_string(),
                preview: false, // Will be set based on flags later
            }));
        }

        Ok(None)
    }
}

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

    #[test]
    fn test_is_glob_pattern() {
        // Test glob pattern detection
        assert!(CnpGrammarParser::is_glob_pattern("*.txt"));
        assert!(CnpGrammarParser::is_glob_pattern("file?.log"));
        assert!(CnpGrammarParser::is_glob_pattern("file[0-9].txt"));
        assert!(CnpGrammarParser::is_glob_pattern("file{a,b,c}.txt"));

        // Test non-glob patterns
        assert!(!CnpGrammarParser::is_glob_pattern("file.txt"));
        assert!(!CnpGrammarParser::is_glob_pattern("directory"));
        assert!(!CnpGrammarParser::is_glob_pattern("TYPE:file"));
        assert!(!CnpGrammarParser::is_glob_pattern("EXT:md"));
    }

    #[test]
    fn test_convert_glob_to_filter() -> Result<(), Box<dyn std::error::Error>> {
        // Test extension patterns
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*.txt")?,
            Some(Filter::Extension("txt".to_string()))
        );
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*.org")?,
            Some(Filter::Extension("org".to_string()))
        );

        // Test name patterns
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*filename*")?,
            Some(Filter::Name("filename".to_string()))
        );
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("prefix*")?,
            Some(Filter::Name("prefix*".to_string()))
        );
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*suffix")?,
            Some(Filter::Name("*suffix".to_string()))
        );

        // Test complex patterns
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("file?.txt")?,
            Some(Filter::Name("file?.txt".to_string()))
        );

        Ok(())
    }

    #[test]
    fn test_glob_pattern_parsing() -> Result<(), Box<dyn std::error::Error>> {
        // Test transform command with glob pattern
        let args = vec![
            "title".to_string(),
            "*.org".to_string(),
            "test_dir".to_string(),
        ];
        let result = CnpGrammarParser::parse(&args)?;

        assert_eq!(result.path, "test_dir");
        assert!(result.transform_command.is_some());
        assert_eq!(result.transform_command.unwrap().command_type, "title");
        assert_eq!(result.filters.len(), 1);

        match &result.filters[0] {
            Filter::Extension(ext) => assert_eq!(ext, "org"),
            _ => panic!("Expected Extension filter"),
        }

        Ok(())
    }

    #[test]
    fn test_multiple_glob_patterns() -> Result<(), Box<dyn std::error::Error>> {
        // Test multiple glob patterns
        let args = vec![
            "kebab".to_string(),
            "*.txt".to_string(),
            "*.md".to_string(),
            ".".to_string(),
        ];
        let result = CnpGrammarParser::parse(&args)?;

        assert_eq!(result.path, ".");
        assert!(result.transform_command.is_some());
        assert_eq!(result.transform_command.unwrap().command_type, "kebab");
        assert_eq!(result.filters.len(), 2);

        // Check both extension filters
        let mut has_txt = false;
        let mut has_md = false;
        for filter in &result.filters {
            match filter {
                Filter::Extension(ext) if ext == "txt" => has_txt = true,
                Filter::Extension(ext) if ext == "md" => has_md = true,
                _ => {}
            }
        }
        assert!(has_txt && has_md);

        Ok(())
    }

    #[test]
    fn test_glob_with_cnp_filters() -> Result<(), Box<dyn std::error::Error>> {
        // Test glob pattern mixed with CNP filters
        let args = vec![
            "snake".to_string(),
            "*.rs".to_string(),
            "TYPE:file".to_string(),
            "src".to_string(),
        ];
        let result = CnpGrammarParser::parse(&args)?;

        assert_eq!(result.path, "src");
        assert!(result.transform_command.is_some());
        assert_eq!(result.filters.len(), 2);

        // Check we have both extension and type filters
        let mut has_ext = false;
        let mut has_type = false;
        for filter in &result.filters {
            match filter {
                Filter::Extension(_) => has_ext = true,
                Filter::Type(_) => has_type = true,
                _ => {}
            }
        }
        assert!(has_ext && has_type);

        Ok(())
    }

    #[test]
    fn test_edge_case_glob_patterns() -> Result<(), Box<dyn std::error::Error>> {
        // Test edge cases
        // *. becomes just * prefix matching which converts to a Name filter
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*.*")?,
            Some(Filter::Name(".".to_string()))
        );

        // Complex extension patterns fall back to Name filter
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*.tar.gz")?,
            Some(Filter::Name("*.tar.gz".to_string()))
        );

        // Single * becomes a generic Name filter
        assert_eq!(
            CnpGrammarParser::convert_glob_to_filter("*")?,
            Some(Filter::Name("*".to_string()))
        );

        Ok(())
    }
}