snip-sync 0.1.2

gRPC sync server for snp snippet manager
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
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};

const MAX_PREMADE_COMMAND_LENGTH: usize = 1024;
const MAX_PREMADE_DESCRIPTION_LENGTH: usize = 1024;
use tonic::Status;

/// Fixes invalid TOML escape sequences (`\<` and `\>`) in double-quoted strings.
/// Converts affected strings to single-quoted (raw literal) form, preserving the
/// backslash content. Only processes double-quoted strings; single-quoted strings
/// and non-string content are left unchanged.
fn fix_invalid_toml_escapes(toml_str: &str) -> String {
    let mut result = String::with_capacity(toml_str.len());
    let mut chars = toml_str.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '"' {
            let mut string_content = String::new();
            let mut needs_fix = false;

            while let Some(&next) = chars.peek() {
                chars.next();
                if next == '"' {
                    break;
                }
                if next == '\\' {
                    if let Some(&after) = chars.peek() {
                        chars.next();
                        string_content.push('\\');
                        string_content.push(after);
                        if after == '<' || after == '>' {
                            needs_fix = true;
                        }
                    } else {
                        string_content.push('\\');
                    }
                } else {
                    string_content.push(next);
                }
            }

            if needs_fix && !string_content.contains('\'') {
                result.push('\'');
                result.push_str(&string_content);
                result.push('\'');
            } else if needs_fix {
                result.push('"');
                for ch in string_content.chars() {
                    if ch == '\\' {
                        result.push('\\');
                        result.push('\\');
                    } else if ch == '"' {
                        result.push('\\');
                        result.push('"');
                    } else {
                        result.push(ch);
                    }
                }
                result.push('"');
            } else {
                result.push('"');
                result.push_str(&string_content);
                result.push('"');
            }
        } else {
            result.push(c);
        }
    }

    result
}

#[derive(Debug, Clone)]
pub struct PremadeLibrary {
    pub name: String,
    pub filename: String,
    pub description: String,
    pub snippet_count: i32,
    pub tags: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct Snippet {
    #[serde(rename = "Command", default)]
    command: Option<String>,
    #[serde(rename = "Description", default)]
    description: Option<String>,
    #[serde(rename = "Tag", default)]
    tags: Vec<String>,
    #[serde(flatten, default)]
    _extra: std::collections::HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct SnippetFile {
    #[serde(rename = "Description", default)]
    description: Option<String>,
    #[serde(rename = "Snippets", default)]
    snippets: Vec<Snippet>,
}

#[derive(Debug, Clone)]
pub struct PremadeManager {
    dir: PathBuf,
    libraries: Vec<PremadeLibrary>,
}

impl PremadeManager {
    pub fn new(dir: PathBuf) -> Self {
        if !dir.exists() {
            if let Err(e) = fs::create_dir_all(&dir) {
                tracing::warn!(
                    "Failed to create premade libraries directory {}: {}",
                    dir.display(),
                    e
                );
            } else {
                tracing::info!("Created premade libraries directory: {}", dir.display());
            }
        }

        let libraries = Self::scan_directory(&dir);
        Self { dir, libraries }
    }

    fn scan_directory(dir: &Path) -> Vec<PremadeLibrary> {
        if !dir.exists() {
            tracing::warn!(
                "Premade libraries directory does not exist: {}",
                dir.display()
            );
            return Vec::new();
        }

        let mut libraries = Vec::new();

        let entries = match fs::read_dir(dir) {
            Ok(entries) => entries,
            Err(e) => {
                tracing::error!("Failed to read premade libraries directory: {}", e);
                return Vec::new();
            }
        };

        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) != Some("toml") {
                continue;
            }

            let filename = match path.file_stem().and_then(|s| s.to_str()) {
                Some(name) => name.to_string(),
                None => continue,
            };

            let content = match fs::read_to_string(&path) {
                Ok(c) => c,
                Err(e) => {
                    tracing::warn!("Failed to read premade library {}: {}", filename, e);
                    continue;
                }
            };

            let content = fix_invalid_toml_escapes(&content);

            let snippet_file: SnippetFile = match toml::from_str(&content) {
                Ok(sf) => sf,
                Err(e) => {
                    tracing::warn!("Failed to parse premade library {}: {}", filename, e);
                    continue;
                }
            };

            if snippet_file.snippets.is_empty() {
                tracing::warn!("Premade library '{}' has no snippets, skipping", filename);
                continue;
            }

            let valid_snippets: Vec<_> = snippet_file
                .snippets
                .into_iter()
                .filter(|s| {
                    let cmd = s.command.as_deref().unwrap_or("");
                    if cmd.is_empty() {
                        tracing::warn!(
                            "Premade library '{}': skipping snippet with empty command",
                            filename
                        );
                        return false;
                    }
                    if cmd.len() > MAX_PREMADE_COMMAND_LENGTH {
                        tracing::warn!(
                            "Premade library '{}': skipping snippet with command exceeding {} bytes",
                            filename,
                            MAX_PREMADE_COMMAND_LENGTH
                        );
                        return false;
                    }
                    if let Some(ref desc) = s.description
                        && desc.len() > MAX_PREMADE_DESCRIPTION_LENGTH
                    {
                        tracing::warn!(
                            "Premade library '{}': skipping snippet with description exceeding {} bytes",
                            filename,
                            MAX_PREMADE_DESCRIPTION_LENGTH
                        );
                        return false;
                    }
                    true
                })
                .collect();

            if valid_snippets.is_empty() {
                tracing::warn!(
                    "Premade library '{}' has no valid snippets, skipping",
                    filename
                );
                continue;
            }

            let snippet_count = valid_snippets.len() as i32;

            let description = snippet_file.description.unwrap_or_else(|| filename.clone());

            let mut all_tags: Vec<String> = Vec::new();
            for s in &valid_snippets {
                for tag in &s.tags {
                    if !all_tags.contains(tag) {
                        all_tags.push(tag.clone());
                    }
                }
            }

            libraries.push(PremadeLibrary {
                name: filename.clone(),
                filename,
                description,
                snippet_count,
                tags: all_tags,
            });
        }

        libraries.sort_by(|a, b| a.filename.cmp(&b.filename));
        tracing::info!("Loaded {} premade libraries", libraries.len());
        libraries
    }

    pub fn list(&self) -> Vec<PremadeLibrary> {
        self.libraries.clone()
    }

    /// Search premade libraries by query string.
    /// Matches against library name, description, and tags.
    pub fn search(&self, query: &str) -> Vec<PremadeLibrary> {
        let q = query.to_lowercase();
        self.libraries
            .iter()
            .filter(|lib| {
                lib.name.to_lowercase().contains(&q)
                    || lib.description.to_lowercase().contains(&q)
                    || lib.tags.iter().any(|t| t.to_lowercase().contains(&q))
            })
            .cloned()
            .collect()
    }

    pub fn get(&self, filename: &str) -> Result<String, Status> {
        let path = self.dir.join(format!("{}.toml", filename));

        let canonical_dir = self
            .dir
            .canonicalize()
            .map_err(|_| Status::internal("failed to canonicalize premade directory"))?;
        let canonical_path = path
            .canonicalize()
            .map_err(|_| Status::invalid_argument("Invalid filename: cannot canonicalize path"))?;

        if !canonical_path.starts_with(&canonical_dir) {
            return Err(Status::invalid_argument(
                "Invalid filename: path traversal detected",
            ));
        }

        fs::read_to_string(&canonical_path)
            .map_err(|e| {
                if e.kind() == std::io::ErrorKind::NotFound {
                    Status::not_found(format!("Premade library '{}' not found", filename))
                } else {
                    tracing::error!("Failed to read premade library: {}", e);
                    Status::internal("Internal error")
                }
            })
            .map(|content| fix_invalid_toml_escapes(&content))
    }

    pub fn is_empty(&self) -> bool {
        self.libraries.is_empty()
    }
}

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

    #[test]
    fn test_get_valid_library() {
        let dir = TempDir::new().unwrap();
        let toml_content = r#"
Description = "Test library"

[[Snippets]]
Command = "echo hello"
Description = "Say hello"
"#;
        fs::write(dir.path().join("test.toml"), toml_content).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let result = manager.get("test");
        assert!(result.is_ok());
        assert!(result.unwrap().contains("echo hello"));
    }

    #[test]
    fn test_get_nonexistent_library() {
        let dir = TempDir::new().unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let result = manager.get("nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_get_path_traversal_rejected() {
        let dir = TempDir::new().unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let result = manager.get("../../etc/passwd");
        assert!(result.is_err());
        let err = result.unwrap_err();
        // When canonicalize fails, we reject for safety (can't verify path is safe)
        assert!(err.message().contains("cannot canonicalize"));
    }

    #[test]
    fn test_get_dot_dot_slash_rejected() {
        let dir = TempDir::new().unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let result = manager.get("../escape");
        assert!(result.is_err());
    }

    #[test]
    fn test_fix_invalid_toml_escapes_backslash_angle() {
        let input = r#"command = "echo \<hello\>""#;
        let output = fix_invalid_toml_escapes(input);
        assert!(output.contains("'echo \\<hello\\>'"));
    }

    #[test]
    fn test_fix_invalid_toml_escapes_no_fix_needed() {
        let input = r#"command = "echo normal""#;
        let output = fix_invalid_toml_escapes(input);
        assert_eq!(output, input);
    }

    #[test]
    fn test_scan_directory_empty() {
        let dir = TempDir::new().unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        assert!(manager.is_empty());
    }

    #[test]
    fn test_scan_directory_with_valid_library() {
        let dir = TempDir::new().unwrap();
        let toml_content = r#"
Description = "Docker snippets"

[[Snippets]]
Command = "docker ps"
Description = "List containers"
"#;
        fs::write(dir.path().join("docker.toml"), toml_content).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        assert_eq!(manager.list().len(), 1);
        assert_eq!(manager.list()[0].name, "docker");
    }

    #[test]
    fn test_scan_directory_skips_non_toml() {
        let dir = TempDir::new().unwrap();
        fs::write(dir.path().join("readme.txt"), "not a toml").unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        assert!(manager.is_empty());
    }

    #[test]
    fn test_scan_directory_skips_empty_snippets() {
        let dir = TempDir::new().unwrap();
        let toml_content = r#"
Description = "Empty library"

[[Snippets]]
Command = ""
Description = "Empty command"
"#;
        fs::write(dir.path().join("empty.toml"), toml_content).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        assert!(manager.is_empty());
    }

    #[test]
    fn test_search_by_name() {
        let dir = TempDir::new().unwrap();
        let toml1 = r#"
Description = "Docker containers"

[[Snippets]]
Command = "docker ps"
Description = "List containers"
Tag = ["docker"]
"#;
        let toml2 = r#"
Description = "Git commands"

[[Snippets]]
Command = "git status"
Description = "Check status"
Tag = ["git"]
"#;
        fs::write(dir.path().join("docker.toml"), toml1).unwrap();
        fs::write(dir.path().join("git.toml"), toml2).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let results = manager.search("docker");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "docker");
    }

    #[test]
    fn test_search_by_description() {
        let dir = TempDir::new().unwrap();
        let toml1 = r#"
Description = "Container orchestration"

[[Snippets]]
Command = "docker compose up"
Description = "Start services"
Tag = ["docker"]
"#;
        fs::write(dir.path().join("docker.toml"), toml1).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let results = manager.search("container");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "docker");
    }

    #[test]
    fn test_search_by_tag() {
        let dir = TempDir::new().unwrap();
        let toml1 = r#"
Description = "Docker basics"

[[Snippets]]
Command = "docker ps"
Description = "List"
Tag = ["docker", "containers"]
"#;
        fs::write(dir.path().join("docker.toml"), toml1).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let results = manager.search("containers");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "docker");
        assert!(results[0].tags.contains(&"docker".to_string()));
        assert!(results[0].tags.contains(&"containers".to_string()));
    }

    #[test]
    fn test_search_case_insensitive() {
        let dir = TempDir::new().unwrap();
        let toml1 = r#"
Description = "Docker commands"

[[Snippets]]
Command = "docker ps"
Description = "List"
Tag = ["docker"]
"#;
        fs::write(dir.path().join("docker.toml"), toml1).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let results = manager.search("DOCKER");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_search_no_results() {
        let dir = TempDir::new().unwrap();
        let toml1 = r#"
Description = "Docker commands"

[[Snippets]]
Command = "docker ps"
Description = "List"
Tag = ["docker"]
"#;
        fs::write(dir.path().join("docker.toml"), toml1).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let results = manager.search("nonexistent");
        assert!(results.is_empty());
    }

    #[test]
    fn test_scan_collects_tags() {
        let dir = TempDir::new().unwrap();
        let toml_content = r#"
Description = "Multi-tag library"

[[Snippets]]
Command = "docker ps"
Description = "List"
Tag = ["docker", "containers"]

[[Snippets]]
Command = "docker images"
Description = "Images"
Tag = ["docker", "images"]
"#;
        fs::write(dir.path().join("multi.toml"), toml_content).unwrap();
        let manager = PremadeManager::new(dir.path().to_path_buf());
        let libs = manager.list();
        assert_eq!(libs.len(), 1);
        assert!(libs[0].tags.contains(&"docker".to_string()));
        assert!(libs[0].tags.contains(&"containers".to_string()));
        assert!(libs[0].tags.contains(&"images".to_string()));
        assert_eq!(libs[0].snippet_count, 2);
    }
}