nb-api 0.4.2

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
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
//! Public document model: kinds, partitions, accessors, iterators.

use std::ops::Range;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use super::helpers::{parse_todo_state_from_title, tag_token_spans_in};

/// Distinguishes the three document kinds recognized by the parser.
///
/// [`DocumentKind::Note`] is the default for `.md` files and any
/// path whose extension is not `.todo.md` or `.bookmark.md`.
///
/// [`DocumentKind::Todo`] is for `.todo.md` files. The Todo
/// [`TodoState`] is derived from the title line via
/// [`NoteDocument::todo_state`].
///
/// [`DocumentKind::Bookmark`] is for `.bookmark.md` files.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum DocumentKind {
    Note,
    Todo,
    Bookmark,
}

/// The parsed checkbox state of a Todo title.
///
/// Surfaced via [`NoteDocument::todo_state`] as
/// `Option<TodoState>`; `None` when the title has no checkbox
/// marker (permissive acceptance).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum TodoState {
    Open,
    Done,
}

/// How the parser determines the [`DocumentKind`] for an input.
///
/// [`ParseContext::FromPath`] infers from the file extension:
/// `.todo.md` → Todo, `.bookmark.md` → Bookmark, `.md` (or other)
/// → Note. The bare `.todo` extension (without `.md`) maps to
/// Note deterministically — the `.todo` extension is a `show
/// --type` classification only, not mutation-authoritative.
///
/// [`ParseContext::Explicit`] sets the kind directly, overriding
/// any inference.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum ParseContext {
    FromPath(PathBuf),
    Explicit(DocumentKind),
}

/// A parsed `nb` note, todo, or bookmark.
///
/// `NoteDocument` owns the complete original source as
/// `Vec<u8>`. The source is byte-identical to the input passed
/// to [`parse`]. The kind-specific ownership partition is
/// exposed via accessor methods (e.g., [`NoteDocument::title`],
/// [`NoteDocument::body`], [`NoteDocument::url`]) and the
/// [`NoteDocument::tag_token_spans`] view.
///
/// See the P1 specification for the full contract.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoteDocument {
    pub(crate) source: Vec<u8>,
    pub(crate) partition: Partition,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Partition {
    Note(NotePartition),
    Todo(TodoPartition),
    Bookmark(BookmarkPartition),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct NotePartition {
    pub(crate) prefix_range: Range<usize>,
    pub(crate) title_range: Option<Range<usize>>,
    pub(crate) tags_prefix_range: Option<Range<usize>>,
    pub(crate) separator_ranges: Vec<Range<usize>>,
    pub(crate) body_ranges: Vec<Range<usize>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TodoPartition {
    pub(crate) prefix_range: Range<usize>,
    pub(crate) title_range: Option<Range<usize>>,
    pub(crate) separator_ranges: Vec<Range<usize>>,
    pub(crate) tag_section_range: Option<Range<usize>>,
    pub(crate) body_ranges: Vec<Range<usize>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BookmarkPartition {
    pub(crate) prefix_range: Range<usize>,
    pub(crate) title_range: Option<Range<usize>>,
    pub(crate) url_range: Option<Range<usize>>,
    pub(crate) separator_ranges: Vec<Range<usize>>,
    pub(crate) tag_section_range: Option<Range<usize>>,
    pub(crate) body_ranges: Vec<Range<usize>>,
}

impl NoteDocument {
    /// The original source bytes, byte-identical to the input
    /// passed to [`parse`].
    pub fn source(&self) -> &[u8] {
        &self.source
    }

    /// Returns a slice into the retained `source` bytes,
    /// byte-identical to the original input. Equivalent to
    /// [`NoteDocument::source`].
    pub fn emit(&self) -> &[u8] {
        &self.source
    }

    /// Verify the ownership partition satisfies the P1
    /// invariants: union covers `[0, source.len())` exactly,
    /// ranges are pairwise disjoint, each range is half-open
    /// and within bounds. Returns `Ok(())` on success or an
    /// `Err(&'static str)` describing the first violation.
    ///
    /// Used by integration tests and by `parse` itself when
    /// the `debug_assertions` cfg flag is enabled. Production
    /// callers do not need to invoke this; it exists to surface
    /// parser regressions during testing.
    pub fn verify_partition(&self) -> Result<(), &'static str> {
        let source_len = self.source.len();
        let mut covered: Vec<(usize, usize)> = Vec::new();
        let mut check = |r: &std::ops::Range<usize>,
                         name: &str|
         -> Result<(), &'static str> {
            if r.start > r.end {
                return Err("range not half-open (start > end)");
            }
            if r.end > source_len {
                return Err("range extends past source.len()");
            }
            covered.push((r.start, r.end));
            let _ = name;
            Ok(())
        };
        match &self.partition {
            Partition::Note(note) => {
                check(&note.prefix_range, "prefix")?;
                if let Some(r) = &note.title_range {
                    check(r, "title")?;
                }
                if let Some(r) = &note.tags_prefix_range {
                    check(r, "tags_prefix")?;
                }
                for r in &note.separator_ranges {
                    check(r, "separator")?;
                }
                for r in &note.body_ranges {
                    check(r, "body")?;
                }
            },
            Partition::Todo(todo) => {
                check(&todo.prefix_range, "prefix")?;
                if let Some(r) = &todo.title_range {
                    check(r, "title")?;
                }
                for r in &todo.separator_ranges {
                    check(r, "separator")?;
                }
                if let Some(r) = &todo.tag_section_range {
                    check(r, "tag_section")?;
                }
                for r in &todo.body_ranges {
                    check(r, "body")?;
                }
            },
            Partition::Bookmark(bookmark) => {
                check(&bookmark.prefix_range, "prefix")?;
                if let Some(r) = &bookmark.title_range {
                    check(r, "title")?;
                }
                if let Some(r) = &bookmark.url_range {
                    check(r, "url")?;
                }
                for r in &bookmark.separator_ranges {
                    check(r, "separator")?;
                }
                if let Some(r) = &bookmark.tag_section_range {
                    check(r, "tag_section")?;
                }
                for r in &bookmark.body_ranges {
                    check(r, "body")?;
                }
            },
        }
        covered.sort_by_key(|&(s, _)| s);
        // Adjacency check: ranges must touch end-to-end
        // (previous.end == next.start) with no overlap AND no
        // gap. A gap means a byte was not assigned to any
        // partition region.
        for window in covered.windows(2) {
            if window[0].1 > window[1].0 {
                return Err("ranges overlap");
            }
            if window[0].1 < window[1].0 {
                return Err(
                    "partition leaves a gap (internal unassigned bytes)",
                );
            }
        }
        if covered.first().map(|r| r.0) != Some(0) {
            return Err("partition does not start at byte 0");
        }
        if covered.last().map(|r| r.1) != Some(source_len) {
            return Err("partition does not cover to source.len()");
        }
        Ok(())
    }

    /// The [`DocumentKind`] of this document.
    pub fn kind(&self) -> DocumentKind {
        match &self.partition {
            Partition::Note(_) => DocumentKind::Note,
            Partition::Todo(_) => DocumentKind::Todo,
            Partition::Bookmark(_) => DocumentKind::Bookmark,
        }
    }

    /// For Todo documents, the parsed checkbox state. `None` for
    /// non-Todo documents and for Todo documents whose title has
    /// no `[ ]`/`[x]` prefix (permissive acceptance).
    pub fn todo_state(&self) -> Option<TodoState> {
        match &self.partition {
            Partition::Todo(todo) => {
                let title = todo.title_range.as_ref()?;
                let line = self.source.get(title.start..title.end)?;
                parse_todo_state_from_title(line)
            },
            _ => None,
        }
    }

    /// The raw title line bytes (including trailing newline);
    /// `None` if no valid ATX H1 title.
    pub fn title(&self) -> Option<&[u8]> {
        let range = self.title_range()?;
        Some(&self.source[range])
    }

    /// `Option<Result<&str, std::str::Utf8Error>>` view of the
    /// title. `None` for no title; `Some(Ok(_))` for valid UTF-8;
    /// `Some(Err(_))` for invalid UTF-8.
    pub fn title_str(&self) -> Option<Result<&str, std::str::Utf8Error>> {
        self.title().map(std::str::from_utf8)
    }

    /// For Note documents, the raw tags prefix line bytes
    /// (including trailing newline); `None` otherwise.
    pub fn tags_prefix(&self) -> Option<&[u8]> {
        let NotePartition {
            tags_prefix_range, ..
        } = match &self.partition {
            Partition::Note(note) => note,
            _ => return None,
        };
        Some(&self.source[tags_prefix_range.clone()?])
    }

    /// For Todo/Bookmark documents, the raw tags-section bytes
    /// (the entire H2 Tags section including its trailing
    /// newline); `None` otherwise.
    pub fn tag_section(&self) -> Option<&[u8]> {
        let range = self.tag_section_range()?;
        Some(&self.source[range])
    }

    /// Iterator over tag token byte spans (`&[u8]`). Tokens come
    /// from `tags_prefix_range` for Note documents and from
    /// `tag_section_range` for Todo/Bookmark documents. Spans
    /// reference bytes owned by the source.
    pub fn tags(&self) -> TagsIter<'_> {
        TagsIter {
            doc: self,
            token_spans: self.tag_token_spans().into_iter(),
        }
    }

    /// Iterator yielding `Result<&str, std::str::Utf8Error>` per
    /// tag token.
    pub fn tags_str(&self) -> TagsStrIter<'_> {
        TagsStrIter {
            doc: self,
            token_spans: self.tag_token_spans().into_iter(),
        }
    }

    /// All tag token spans across the document. For Note,
    /// tokens come from `tags_prefix_range`; for Todo/Bookmark,
    /// from `tag_section_range`. Spans reference bytes owned by
    /// the source and SHALL NOT themselves own bytes.
    pub fn tag_token_spans(&self) -> Vec<Range<usize>> {
        match &self.partition {
            Partition::Note(note) => tag_token_spans_in(
                &self.source,
                note.tags_prefix_range.as_ref(),
            ),
            Partition::Todo(todo) => tag_token_spans_in(
                &self.source,
                todo.tag_section_range.as_ref(),
            ),
            Partition::Bookmark(bookmark) => tag_token_spans_in(
                &self.source,
                bookmark.tag_section_range.as_ref(),
            ),
        }
    }

    /// Body partition ranges in source order (half-open byte offsets).
    pub fn body_ranges(&self) -> Vec<Range<usize>> {
        match &self.partition {
            Partition::Note(note) => note.body_ranges.clone(),
            Partition::Todo(todo) => todo.body_ranges.clone(),
            Partition::Bookmark(bookmark) => bookmark.body_ranges.clone(),
        }
    }

    /// Iterator over body byte ranges (in source order).
    pub fn body(&self) -> BodyFragments<'_> {
        BodyFragments {
            source: &self.source,
            ranges: self.body_ranges().into_iter(),
        }
    }

    /// Concatenated body fragment bytes in source order (fingerprint domain).
    pub fn body_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        for fragment in self.body() {
            out.extend_from_slice(fragment);
        }
        out
    }

    /// Title line byte range when present (includes trailing newline).
    pub fn title_byte_range(&self) -> Option<Range<usize>> {
        self.title_range()
    }

    /// Tags-prefix (Note) or tags-section (Todo/Bookmark) byte range.
    pub fn tags_byte_range(&self) -> Option<Range<usize>> {
        match &self.partition {
            Partition::Note(note) => note.tags_prefix_range.clone(),
            Partition::Todo(todo) => todo.tag_section_range.clone(),
            Partition::Bookmark(bookmark) => {
                bookmark.tag_section_range.clone()
            },
        }
    }

    /// For Bookmark documents, the raw URL line bytes (including
    /// trailing newline); `None` for non-Bookmark documents or
    /// Bookmarks without a `<URL>` line.
    pub fn url(&self) -> Option<&[u8]> {
        let BookmarkPartition { url_range, .. } = match &self.partition {
            Partition::Bookmark(bookmark) => bookmark,
            _ => return None,
        };
        url_range.as_ref().map(|r| &self.source[r.clone()])
    }

    /// `Option<Result<&str, std::str::Utf8Error>>` view of the
    /// URL.
    pub fn url_str(&self) -> Option<Result<&str, std::str::Utf8Error>> {
        self.url().map(std::str::from_utf8)
    }

    fn title_range(&self) -> Option<Range<usize>> {
        match &self.partition {
            Partition::Note(note) => note.title_range.clone(),
            Partition::Todo(todo) => todo.title_range.clone(),
            Partition::Bookmark(bookmark) => bookmark.title_range.clone(),
        }
    }

    fn tag_section_range(&self) -> Option<Range<usize>> {
        match &self.partition {
            Partition::Note(_) => None,
            Partition::Todo(todo) => todo.tag_section_range.clone(),
            Partition::Bookmark(bookmark) => {
                bookmark.tag_section_range.clone()
            },
        }
    }
}

/// Iterator over tag token byte slices.
pub struct TagsIter<'a> {
    doc: &'a NoteDocument,
    token_spans: std::vec::IntoIter<Range<usize>>,
}

impl<'a> Iterator for TagsIter<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<Self::Item> {
        self.token_spans.next().map(|r| &self.doc.source[r])
    }
}

/// Iterator over tag token string slices, surfacing UTF-8 errors.
pub struct TagsStrIter<'a> {
    doc: &'a NoteDocument,
    token_spans: std::vec::IntoIter<Range<usize>>,
}

impl<'a> Iterator for TagsStrIter<'a> {
    type Item = Result<&'a str, std::str::Utf8Error>;
    fn next(&mut self) -> Option<Self::Item> {
        self.token_spans
            .next()
            .map(|r| std::str::from_utf8(&self.doc.source[r]))
    }
}

/// Iterator over body byte ranges.
pub struct BodyFragments<'a> {
    source: &'a [u8],
    ranges: std::vec::IntoIter<Range<usize>>,
}

impl<'a> Iterator for BodyFragments<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<Self::Item> {
        self.ranges.next().map(|r| &self.source[r])
    }
}