doksnet 1.1.2

A CLI tool for documentation-code mapping verification using cryptographic hashes
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
use anyhow::{anyhow, Result};
use std::path::Path;

#[derive(Debug, Clone, PartialEq)]
pub struct Partition {
    pub file_path: String,
    pub start_line: Option<usize>,
    pub end_line: Option<usize>,
    pub start_col: Option<usize>,
    pub end_col: Option<usize>,
}

impl Partition {
    /// Parse a partition string in the format:
    /// <relative_path>:<start_line>-<end_line>@<start_col>-<end_col>
    /// Line or column ranges can be optional
    pub fn parse(partition_str: &str) -> Result<Self> {
        if partition_str.trim().is_empty() {
            return Err(anyhow!("Partition string cannot be empty"));
        }

        let parts: Vec<&str> = partition_str.split(':').collect();
        let file_path = parts[0].to_string();

        if file_path.trim().is_empty() {
            return Err(anyhow!("File path cannot be empty"));
        }

        if parts.len() == 1 {
            // Just a file path, no ranges
            return Ok(Partition {
                file_path,
                start_line: None,
                end_line: None,
                start_col: None,
                end_col: None,
            });
        }

        let range_part = parts[1];
        let (line_range, col_range) = if range_part.contains('@') {
            let range_parts: Vec<&str> = range_part.split('@').collect();
            (range_parts[0], Some(range_parts[1]))
        } else {
            (range_part, None)
        };

        let (start_line, end_line) = if line_range.is_empty() {
            (None, None)
        } else {
            let line_parts: Vec<&str> = line_range.split('-').collect();
            match line_parts.len() {
                1 => {
                    let line = line_parts[0].parse::<usize>()?;
                    (Some(line), Some(line))
                }
                2 => {
                    let start = line_parts[0].parse::<usize>()?;
                    let end = line_parts[1].parse::<usize>()?;
                    (Some(start), Some(end))
                }
                _ => return Err(anyhow!("Invalid line range format")),
            }
        };

        let (start_col, end_col) = if let Some(col_range) = col_range {
            if col_range.is_empty() {
                (None, None)
            } else {
                let col_parts: Vec<&str> = col_range.split('-').collect();
                match col_parts.len() {
                    1 => {
                        let col = col_parts[0].parse::<usize>()?;
                        (Some(col), Some(col))
                    }
                    2 => {
                        let start = col_parts[0].parse::<usize>()?;
                        let end = col_parts[1].parse::<usize>()?;
                        (Some(start), Some(end))
                    }
                    _ => return Err(anyhow!("Invalid column range format")),
                }
            }
        } else {
            (None, None)
        };

        Ok(Partition {
            file_path,
            start_line,
            end_line,
            start_col,
            end_col,
        })
    }

    /// Extract content from the file based on the partition specification
    pub fn extract_content(&self) -> Result<String> {
        let file_path = Path::new(&self.file_path);
        if !file_path.exists() {
            return Err(anyhow!("File not found: {}", self.file_path));
        }

        let content = std::fs::read_to_string(file_path)?;
        let lines: Vec<&str> = content.lines().collect();

        match (self.start_line, self.end_line) {
            (Some(start), Some(end)) => {
                if start == 0 || end == 0 {
                    return Err(anyhow!("Line numbers must be 1-indexed"));
                }
                if start > lines.len() || end > lines.len() {
                    return Err(anyhow!("Line numbers exceed file length"));
                }
                if start > end {
                    return Err(anyhow!("Start line must be <= end line"));
                }

                let mut result = String::new();
                for (idx, line) in lines.iter().enumerate().take(end).skip(start - 1) {
                    let i = idx;
                    let line = *line;
                    let line_content = match (self.start_col, self.end_col) {
                        (Some(start_col), Some(end_col)) => {
                            if i == start - 1 && i == end - 1 {
                                // Single line with column range
                                let chars: Vec<char> = line.chars().collect();
                                if start_col > chars.len() || end_col > chars.len() {
                                    return Err(anyhow!("Column numbers exceed line length"));
                                }
                                chars[(start_col - 1)..end_col].iter().collect()
                            } else if i == start - 1 {
                                // First line with start column
                                let chars: Vec<char> = line.chars().collect();
                                if start_col > chars.len() {
                                    return Err(anyhow!("Start column exceeds line length"));
                                }
                                chars[(start_col - 1)..].iter().collect()
                            } else if i == end - 1 {
                                // Last line with end column
                                let chars: Vec<char> = line.chars().collect();
                                if end_col > chars.len() {
                                    return Err(anyhow!("End column exceeds line length"));
                                }
                                chars[..end_col].iter().collect()
                            } else {
                                // Middle lines, full content
                                line.to_string()
                            }
                        }
                        _ => line.to_string(),
                    };

                    if i > start - 1 {
                        result.push('\n');
                    }
                    result.push_str(&line_content);
                }
                Ok(result)
            }
            _ => {
                // No line range specified, return entire file
                Ok(content)
            }
        }
    }

    /// Convert the partition back to string format
    #[allow(dead_code)]
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(&self) -> String {
        let mut result = self.file_path.clone();

        if let (Some(start_line), Some(end_line)) = (self.start_line, self.end_line) {
            if start_line == end_line {
                result.push_str(&format!(":{}", start_line));
            } else {
                result.push_str(&format!(":{}-{}", start_line, end_line));
            }
        }

        if let (Some(start_col), Some(end_col)) = (self.start_col, self.end_col) {
            if start_col == end_col {
                result.push_str(&format!("@{}", start_col));
            } else {
                result.push_str(&format!("@{}-{}", start_col, end_col));
            }
        }

        result
    }
}

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

    #[test]
    fn test_parse_file_only() {
        let partition = Partition::parse("src/main.rs").unwrap();
        assert_eq!(partition.file_path, "src/main.rs");
        assert_eq!(partition.start_line, None);
        assert_eq!(partition.end_line, None);
        assert_eq!(partition.start_col, None);
        assert_eq!(partition.end_col, None);
    }

    #[test]
    fn test_parse_with_line_range() {
        let partition = Partition::parse("src/main.rs:10-20").unwrap();
        assert_eq!(partition.file_path, "src/main.rs");
        assert_eq!(partition.start_line, Some(10));
        assert_eq!(partition.end_line, Some(20));
        assert_eq!(partition.start_col, None);
        assert_eq!(partition.end_col, None);
    }

    #[test]
    fn test_parse_with_line_and_column_range() {
        let partition = Partition::parse("src/main.rs:10-20@5-15").unwrap();
        assert_eq!(partition.file_path, "src/main.rs");
        assert_eq!(partition.start_line, Some(10));
        assert_eq!(partition.end_line, Some(20));
        assert_eq!(partition.start_col, Some(5));
        assert_eq!(partition.end_col, Some(15));
    }

    #[test]
    fn test_parse_single_line() {
        let partition = Partition::parse("README.md:42").unwrap();
        assert_eq!(partition.file_path, "README.md");
        assert_eq!(partition.start_line, Some(42));
        assert_eq!(partition.end_line, Some(42));
    }

    #[test]
    fn test_parse_single_column() {
        let partition = Partition::parse("file.txt:10@5").unwrap();
        assert_eq!(partition.file_path, "file.txt");
        assert_eq!(partition.start_line, Some(10));
        assert_eq!(partition.end_line, Some(10));
        assert_eq!(partition.start_col, Some(5));
        assert_eq!(partition.end_col, Some(5));
    }

    #[test]
    fn test_parse_with_empty_ranges() {
        let partition = Partition::parse("file.txt:@").unwrap();
        assert_eq!(partition.file_path, "file.txt");
        assert_eq!(partition.start_line, None);
        assert_eq!(partition.end_line, None);
        assert_eq!(partition.start_col, None);
        assert_eq!(partition.end_col, None);
    }

    #[test]
    fn test_parse_invalid_format() {
        // Empty string should error because no file path
        let result = Partition::parse("");
        assert!(result.is_err());

        // Invalid line numbers should error
        assert!(Partition::parse("file.txt:abc").is_err());
        assert!(Partition::parse("file.txt:10@abc").is_err());

        // This is actually valid parsing (validation happens during extraction)
        assert!(Partition::parse("file.txt:10-5").is_ok());
    }

    #[test]
    fn test_extract_content_entire_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "line1\nline2\nline3").unwrap();

        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: None,
            end_line: None,
            start_col: None,
            end_col: None,
        };

        let content = partition.extract_content().unwrap();
        assert_eq!(content, "line1\nline2\nline3");
    }

    #[test]
    fn test_extract_content_line_range() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "line1\nline2\nline3\nline4").unwrap();

        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(2),
            end_line: Some(3),
            start_col: None,
            end_col: None,
        };

        let content = partition.extract_content().unwrap();
        assert_eq!(content, "line2\nline3");
    }

    #[test]
    fn test_extract_content_single_line() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "line1\nline2\nline3").unwrap();

        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(2),
            end_line: Some(2),
            start_col: None,
            end_col: None,
        };

        let content = partition.extract_content().unwrap();
        assert_eq!(content, "line2");
    }

    #[test]
    fn test_extract_content_with_columns() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "hello world\nrust programming").unwrap();

        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(1),
            end_line: Some(1),
            start_col: Some(7),
            end_col: Some(11),
        };

        let content = partition.extract_content().unwrap();
        assert_eq!(content, "world");
    }

    #[test]
    fn test_extract_content_multiline_with_columns() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "hello world\nrust programming\ngreat language").unwrap();

        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(1),
            end_line: Some(2),
            start_col: Some(7),
            end_col: Some(4),
        };

        let content = partition.extract_content().unwrap();
        assert_eq!(content, "world\nrust");
    }

    #[test]
    fn test_extract_content_file_not_found() {
        let partition = Partition {
            file_path: "nonexistent.txt".to_string(),
            start_line: None,
            end_line: None,
            start_col: None,
            end_col: None,
        };

        assert!(partition.extract_content().is_err());
    }

    #[test]
    fn test_extract_content_invalid_line_numbers() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        fs::write(&file_path, "line1\nline2").unwrap();

        // Zero-indexed line numbers should fail
        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(0),
            end_line: Some(1),
            start_col: None,
            end_col: None,
        };
        assert!(partition.extract_content().is_err());

        // Line numbers exceeding file length should fail
        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(1),
            end_line: Some(5),
            start_col: None,
            end_col: None,
        };
        assert!(partition.extract_content().is_err());

        // Start line > end line should fail
        let partition = Partition {
            file_path: file_path.to_string_lossy().to_string(),
            start_line: Some(2),
            end_line: Some(1),
            start_col: None,
            end_col: None,
        };
        assert!(partition.extract_content().is_err());
    }

    #[test]
    fn test_to_string() {
        let partition = Partition {
            file_path: "src/main.rs".to_string(),
            start_line: Some(10),
            end_line: Some(20),
            start_col: Some(5),
            end_col: Some(15),
        };
        assert_eq!(partition.to_string(), "src/main.rs:10-20@5-15");

        let partition = Partition {
            file_path: "README.md".to_string(),
            start_line: Some(5),
            end_line: Some(5),
            start_col: None,
            end_col: None,
        };
        assert_eq!(partition.to_string(), "README.md:5");

        let partition = Partition {
            file_path: "file.txt".to_string(),
            start_line: None,
            end_line: None,
            start_col: None,
            end_col: None,
        };
        assert_eq!(partition.to_string(), "file.txt");
    }
}