nb-api 0.4.3

Typed Rust interface to the nb note-taking CLI (subprocess wrapper: escaping, validation, output parsing)
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
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::error::NbError;
use crate::git;
use crate::types::NoteTarget;

use super::index::{index_rel_for_folder, parse_index_lines};

#[derive(Debug, Clone)]
pub(super) enum VirtualNode {
    File(Vec<u8>),
    Folder,
}

pub(super) struct VirtualTree {
    /// Notebook-relative paths using `/` separators.
    pub(super) nodes: HashMap<String, VirtualNode>,
}

impl VirtualTree {
    pub(super) fn from_disk(root: &Path) -> Result<Self, NbError> {
        let mut nodes = HashMap::new();
        let paths = git::list_notebook_paths(root)?;
        for rel in paths {
            let key = normalize_rel(&rel);
            let abs = root.join(&rel);
            let meta =
                std::fs::symlink_metadata(&abs).map_err(|e| NbError::Io {
                    path: abs.clone(),
                    source: e.into(),
                })?;
            if meta.file_type().is_symlink() {
                return Err(NbError::UnsupportedStructure {
                    reason: format!(
                        "notebook path `{key}` is a symlink; transactions refuse symlink snapshot/materialization"
                    ),
                });
            }
            if meta.is_dir() {
                nodes.insert(key, VirtualNode::Folder);
            } else if meta.is_file() {
                let bytes = std::fs::read(&abs).map_err(|e| NbError::Io {
                    path: abs,
                    source: e.into(),
                })?;
                // Non-empty directories may be absent from the path list
                // (only their children are listed); chain the parents so a
                // create/move onto an existing directory path collides
                // instead of materializing a file over the directory.
                if let Some(parent) = parent_path(&key) {
                    let mut acc = String::new();
                    for part in parent.split('/').filter(|p| !p.is_empty()) {
                        if !acc.is_empty() {
                            acc.push('/');
                        }
                        acc.push_str(part);
                        nodes
                            .entry(acc.clone())
                            .or_insert(VirtualNode::Folder);
                    }
                }
                nodes.insert(key, VirtualNode::File(bytes));
            }
        }
        Ok(Self { nodes })
    }

    pub(super) fn exists(&self, path: &str) -> bool {
        self.nodes.contains_key(path)
    }

    pub(super) fn get_file(&self, path: &str) -> Option<&[u8]> {
        match self.nodes.get(path) {
            Some(VirtualNode::File(b)) => Some(b),
            _ => None,
        }
    }

    pub(super) fn insert_file(&mut self, path: String, bytes: Vec<u8>) {
        // Ensure parent folders exist virtually.
        if let Some(parent) = parent_path(&path) {
            self.ensure_folder_chain(&parent);
        }
        self.nodes.insert(path, VirtualNode::File(bytes));
    }

    pub(super) fn insert_folder(&mut self, path: String) {
        self.ensure_folder_chain(&path);
        self.nodes.insert(path, VirtualNode::Folder);
    }

    fn ensure_folder_chain(&mut self, path: &str) {
        let mut acc = String::new();
        for part in path.split('/').filter(|p| !p.is_empty()) {
            if !acc.is_empty() {
                acc.push('/');
            }
            acc.push_str(part);
            self.nodes.entry(acc.clone()).or_insert(VirtualNode::Folder);
        }
    }

    pub(super) fn remove(&mut self, path: &str) {
        self.nodes.remove(path);
    }

    pub(super) fn rename(
        &mut self,
        from: &str,
        to: &str,
    ) -> Result<(), NbError> {
        let node =
            self.nodes.remove(from).ok_or_else(|| NbError::NotFound {
                selector: from.to_string(),
            })?;
        if let Some(parent) = parent_path(to) {
            self.ensure_folder_chain(&parent);
        }
        self.nodes.insert(to.to_string(), node);
        Ok(())
    }
}

/// Reject `rel` when any existing path component under `root` is a symlink.
///
/// Uses no-follow metadata so ignored directory symlinks (absent from the
/// virtual snapshot) cannot redirect `create_dir_all` / `write` outside the
/// notebook root.
pub(super) fn refuse_symlink_ancestors(
    root: &Path,
    rel: &str,
) -> Result<(), NbError> {
    let mut acc = PathBuf::new();
    for part in rel.split('/').filter(|p| !p.is_empty()) {
        acc.push(part);
        let abs = root.join(&acc);
        match std::fs::symlink_metadata(&abs) {
            Ok(meta) if meta.file_type().is_symlink() => {
                let key = acc.to_string_lossy().replace('\\', "/");
                return Err(NbError::UnsupportedStructure {
                    reason: format!(
                        "refusing path through symlink `{key}`; transactions do not follow symlinks"
                    ),
                });
            },
            Ok(_) => {},
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => break,
            Err(e) => {
                return Err(NbError::Io {
                    path: abs,
                    source: e.into(),
                });
            },
        }
    }
    Ok(())
}

pub(super) fn outcome_selector(
    notebook: &str,
    target: &NoteTarget,
    resolved_path: &str,
) -> String {
    match target {
        NoteTarget::Selector { value } => {
            if value.contains(':') {
                value.clone()
            } else {
                format!("{notebook}:{value}")
            }
        },
        NoteTarget::Path { .. } => format!("{notebook}:{resolved_path}"),
    }
}

pub(super) fn refuse_create_collision(
    notebook_root: &Path,
    tree: &VirtualTree,
    ignored_existing: &HashSet<String>,
    path: &str,
) -> Result<(), NbError> {
    if ignored_existing.contains(path) {
        return Err(NbError::PathIgnored {
            path: path.to_string(),
            guidance: "path exists as a Git-ignored file; un-ignore or remove it outside the transaction before creating here".into(),
            plan_index: None,
        });
    }
    // Ignored directory/file symlinks are absent from the virtual tree; reject
    // creates whose prefix is an ignored symlink (or any on-disk symlink).
    refuse_symlink_ancestors(notebook_root, path)?;
    for ig in ignored_existing {
        if path == ig || path.starts_with(&format!("{ig}/")) {
            let abs = notebook_root.join(ig);
            if let Ok(meta) = std::fs::symlink_metadata(&abs)
                && meta.file_type().is_symlink()
            {
                return Err(NbError::UnsupportedStructure {
                    reason: format!(
                        "refusing path through symlink `{ig}`; transactions do not follow symlinks"
                    ),
                });
            }
        }
    }
    if tree.exists(path) {
        return Err(NbError::PathCollision {
            path: path.to_string(),
            plan_index: None,
        });
    }
    Ok(())
}

pub(super) fn refuse_existing_ignored(
    ignored_existing: &HashSet<String>,
    path: &str,
) -> Result<(), NbError> {
    if ignored_existing.contains(path) {
        return Err(NbError::PathIgnored {
            path: path.to_string(),
            guidance:
                "cannot edit, delete, or move an existing Git-ignored path inside a transaction"
                    .into(),
            plan_index: None,
        });
    }
    Ok(())
}

pub(super) fn resolve_target_path(
    tree: &VirtualTree,
    ignored_existing: &HashSet<String>,
    target: &NoteTarget,
) -> Result<String, NbError> {
    match target {
        NoteTarget::Path { value } => {
            let path = normalize_rel(value);
            refuse_existing_ignored(ignored_existing, &path)?;
            if tree.exists(&path) {
                return Ok(path);
            }
            // Allow selector-like numeric ids only via Selector variant.
            Err(NbError::NotFound {
                selector: value.clone(),
            })
        },
        NoteTarget::Selector { value } => {
            // Prefer exact path match when value looks like a path.
            let stripped = value
                .rsplit_once(':')
                .map(|(_, rest)| rest)
                .unwrap_or(value.as_str());
            let candidate = normalize_rel(stripped);
            refuse_existing_ignored(ignored_existing, &candidate)?;
            if tree.exists(&candidate) {
                return Ok(candidate);
            }
            // Numeric `<folder>/<id>` / `<id>` via the virtual `.index`
            // (blank lines are dead ids -> NotFound, never reused).
            if let Some(resolved) = resolve_numeric_in_tree(tree, &candidate) {
                return resolved;
            }
            // Match by basename or unique suffix.
            let matches: Vec<_> = tree
                .nodes
                .iter()
                .filter_map(|(p, n)| match n {
                    VirtualNode::File(_)
                        if p == &candidate
                            || p.ends_with(&format!("/{candidate}"))
                            || Path::new(p)
                                .file_stem()
                                .and_then(|s| s.to_str())
                                == Some(stripped)
                            || p.ends_with(stripped) =>
                    {
                        Some(p.clone())
                    },
                    _ => None,
                })
                .collect();
            match matches.as_slice() {
                [one] => Ok(one.clone()),
                [] => {
                    // Existing ignored paths are not in the editable tree.
                    if ignored_existing.contains(&candidate) {
                        return Err(NbError::PathIgnored {
                            path: candidate,
                            guidance: "cannot edit, delete, or move an existing Git-ignored path inside a transaction".into(),
                            plan_index: None,
                        });
                    }
                    for ig in ignored_existing {
                        if ig.ends_with(&format!("/{candidate}"))
                            || ig.ends_with(stripped)
                        {
                            return Err(NbError::PathIgnored {
                                path: ig.clone(),
                                guidance: "cannot edit, delete, or move an existing Git-ignored path inside a transaction".into(),
                                plan_index: None,
                            });
                        }
                    }
                    Err(NbError::NotFound {
                        selector: value.clone(),
                    })
                },
                _ => Err(NbError::ValidationError {
                    reason: format!(
                        "selector `{value}` is ambiguous across multiple paths"
                    ),
                    location: None,
                }),
            }
        },
    }
}

/// Resolve `<folder>/<id>` or `<id>` against the virtual `.index`.
///
/// Returns `Some(Ok(path))` on a live entry, `Some(Err(NotFound))` when the
/// line exists but is blank (deleted id) or out of range, and `None` when
/// the value is not numeric-id shaped (caller falls through to name
/// matching).
fn resolve_numeric_in_tree(
    tree: &VirtualTree,
    candidate: &str,
) -> Option<Result<String, NbError>> {
    let (folder, id_str) = match candidate.rfind('/') {
        Some(i) => {
            (candidate[..i].to_string(), candidate[i + 1..].to_string())
        },
        None => (String::new(), candidate.to_string()),
    };
    let id: usize = match id_str.parse() {
        Ok(n) => n,
        Err(_) => return None,
    };
    if id == 0 {
        return Some(Err(NbError::NotFound {
            selector: candidate.to_string(),
        }));
    }
    let rel = index_rel_for_folder(&folder);
    let bytes = match tree.nodes.get(&rel) {
        Some(VirtualNode::File(b)) => b.clone(),
        _ => return None,
    };
    let lines = parse_index_lines(&bytes);
    match lines.get(id - 1) {
        Some(name) if !name.trim().is_empty() => {
            let path = if folder.is_empty() {
                name.clone()
            } else {
                format!("{folder}/{name}")
            };
            Some(Ok(path))
        },
        _ => Some(Err(NbError::NotFound {
            selector: candidate.to_string(),
        })),
    }
}

pub(super) fn resolve_move_destination(
    from: &str,
    destination: &str,
) -> String {
    // A trailing slash means "into folder" (basename kept). Check the raw
    // form: `normalize_rel` trims slashes, so a post-normalize check would
    // be dead code.
    let into_folder = destination.trim_end().ends_with('/');
    let dest = normalize_rel(destination);
    if into_folder {
        let base = Path::new(from)
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or(from);
        return normalize_rel(&format!("{dest}/{base}"));
    }
    // Destination is a folder path without slash if no extension and exists as folder —
    // treat as basename rename when it has a file extension or contains a dot filename.
    dest
}

pub(super) fn normalize_rel(path: &str) -> String {
    path.trim()
        .trim_start_matches("./")
        .trim_matches('/')
        .replace('\\', "/")
}

pub(super) fn parent_path(path: &str) -> Option<String> {
    let p = Path::new(path).parent()?;
    let s = p.to_string_lossy().replace('\\', "/");
    if s.is_empty() || s == "." {
        None
    } else {
        Some(s)
    }
}