bear-query 0.0.3

A read-only Rust library for querying the Bear note-taking app's SQLite database with minimal interference
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
//! Data models for Bear database entities.
//!
//! This module contains all the types representing Bear's database entities:
//! notes, tags, and their identifiers.

use rusqlite::types::{FromSql, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::{Row, ToSql};
use std::collections::{HashMap, HashSet};
use time::OffsetDateTime;

/// Internal database ID wrapper.
///
/// This wraps SQLite's INTEGER PRIMARY KEY values.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub(crate) struct CoreDbId(pub(crate) i64);

/// Internal Core Data note identifier.
///
/// This wraps the note's SQLite primary key (`Z_PK` in Bear's schema).
/// This ID is internal to the database and should not be exposed in public APIs.
/// Use `NoteId` for the public API instead.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub(crate) struct CoreDbNoteId(pub(crate) CoreDbId);

impl CoreDbNoteId {
  // Internal construction is done via FromSql trait when reading from database
}

/// Bear note identifier (UUID-based).
///
/// This wraps Bear's UUID identifier for notes. This is the identifier that Bear
/// uses in its UI, x-callback-url API, and for syncing notes across devices.
///
/// # Creating IDs
///
/// You can create a `NoteId` from a UUID string:
///
/// ```
/// use bear_query::NoteId;
///
/// let note_id = NoteId::new("ABC123-DEF456-...".to_string());
/// ```
///
/// # Usage
///
/// Use this ID for:
/// - Opening notes in Bear via x-callback-url: `bear://x-callback-url/open-note?id={uuid}`
/// - Storing stable references to notes that work across devices
/// - Matching notes in sync operations
///
/// This is Bear's primary identifier for notes.
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct NoteId(String);

impl NoteId {
  /// Creates a new `NoteId` from a UUID string.
  ///
  /// # Example
  ///
  /// ```
  /// use bear_query::NoteId;
  ///
  /// let note_id = NoteId::new("ABC123-DEF456-...".to_string());
  /// ```
  pub fn new(uuid: String) -> Self {
    NoteId(uuid)
  }

  /// Returns the UUID as a string slice.
  ///
  /// # Example
  ///
  /// ```
  /// use bear_query::NoteId;
  ///
  /// let note_id = NoteId::new("ABC123-DEF456-...".to_string());
  /// assert_eq!(note_id.as_str(), "ABC123-DEF456-...");
  /// ```
  pub fn as_str(&self) -> &str {
    &self.0
  }

  /// Consumes the NoteId and returns the inner UUID string.
  pub fn into_string(self) -> String {
    self.0
  }
}

/// Unique identifier for a Bear tag.
///
/// This wraps the tag's SQLite primary key.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct TagId(pub(crate) CoreDbId);

impl TagId {
  /// Creates a new `TagId` from an `i64` primary key value.
  pub fn new(id: i64) -> Self {
    TagId(CoreDbId(id))
  }

  /// Returns the underlying `i64` value of this ID.
  pub fn as_i64(self) -> i64 {
    self.0.0
  }
}

impl FromSql for CoreDbId {
  fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
    Ok(Self(value.as_i64()?))
  }
}

impl FromSql for CoreDbNoteId {
  fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
    Ok(Self(FromSql::column_result(value)?))
  }
}

impl FromSql for TagId {
  fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
    Ok(Self(FromSql::column_result(value)?))
  }
}

impl ToSql for CoreDbId {
  fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
    self.0.to_sql()
  }
}

impl ToSql for CoreDbNoteId {
  fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
    self.0.to_sql()
  }
}

/// A tag from Bear's database.
///
/// Tags in Bear organize notes hierarchically (e.g., "work/projects/bear-query").
///
/// # Nullable Fields
///
/// - **`name`**: Tag name. In theory should never be NULL, but we handle it defensively.
///   A tag without a name would be unusual but possible in a corrupted database.
/// - **`modified`**: Timestamp of last modification. May be `None` for tags that have
///   never been explicitly modified.
///
/// # Example
///
/// ```no_run
/// # use bear_query::BearDb;
/// # fn main() -> Result<(), bear_query::BearError> {
/// let db = BearDb::new()?;
/// let tags = db.tags()?;
///
/// for tag in tags.iter() {
///     let name = tag.name().unwrap_or("[unnamed]");
///     match tag.modified() {
///         Some(modified) => println!("Tag '{}' modified: {}", name, modified),
///         None => println!("Tag '{}' (never modified)", name),
///     }
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Tag {
  id: TagId,
  name: Option<String>,
  modified: Option<OffsetDateTime>,
}

impl Tag {
  /// Returns the tag's primary key identifier.
  ///
  /// This is always present (never NULL) and stable across the tag's lifetime.
  pub fn id(&self) -> TagId {
    self.id
  }

  /// Returns the tag's name, if present.
  ///
  /// Returns `None` in the rare case of a tag without a name (corrupted data).
  ///
  /// Tag names can be hierarchical, using `/` as separator:
  /// - `"work"` - top-level tag
  /// - `"work/projects"` - nested tag
  /// - `"work/projects/bear-query"` - deeply nested tag
  pub fn name(&self) -> Option<&str> {
    self.name.as_deref()
  }

  /// Returns the timestamp of the tag's last modification, if available.
  ///
  /// Returns `None` for tags that have never been explicitly modified.
  pub fn modified(&self) -> Option<OffsetDateTime> {
    self.modified
  }
}

/// Helper to construct Tag from a database row
pub(crate) fn tag_from_row(row: &Row) -> rusqlite::Result<Tag> {
  Ok(Tag {
    id: row.get("id")?,
    name: row.get("name")?,
    modified: row.get("modified")?,
  })
}

/// Collection of tags from Bear's database.
///
/// This is a map of tag IDs to tags, returned by `BearDb::tags()`.
/// It provides convenient methods for looking up tags and converting
/// tag ID sets into tag names.
///
/// # Example
///
/// ```no_run
/// # use bear_query::BearDb;
/// # fn main() -> Result<(), bear_query::BearError> {
/// let db = BearDb::new()?;
/// let tags = db.tags()?;
///
/// println!("Total tags: {}", tags.count());
///
/// for tag in tags.iter() {
///     if let Some(name) = tag.name() {
///         println!("Tag: {}", name);
///     }
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct TagsMap {
  pub(crate) tags: HashMap<TagId, Tag>,
}

impl TagsMap {
  /// Gets a tag by its ID.
  pub fn get(
    &self,
    tag_id: &TagId,
  ) -> Option<&Tag> {
    self.tags.get(tag_id)
  }

  /// Returns the number of tags.
  pub fn count(&self) -> usize {
    self.tags.len()
  }

  /// Returns an iterator over all tags.
  pub fn iter(&self) -> impl Iterator<Item = &Tag> {
    self.tags.values()
  }

  /// Returns the names of the tags with the given IDs.
  ///
  /// Tags with NULL names are omitted from the result.
  pub fn names(
    &self,
    tag_ids: &HashSet<TagId>,
  ) -> HashSet<String> {
    tag_ids
      .iter()
      .filter_map(|id| self.get(id).and_then(|t| t.name.clone()))
      .collect()
  }
}

/// A note from Bear's database.
///
/// # Nullable Fields
///
/// Only one field in Bear notes can be NULL in the database:
///
/// - **`content`**: Empty notes may have `None` for content.
///
/// # Always-Present Fields
///
/// The following fields are **always present** (never NULL):
///
/// - **`title`**: All notes have titles (may be empty string, but never NULL)
/// - **`unique_id`**: Bear's UUID identifier (always present)
/// - **`id`**: Primary key (always present)
/// - **`modified`**, **`created`**: Timestamps (always present)
/// - **`is_pinned`**: Boolean flag (always present)
///
/// # Identifiers
///
/// Bear notes use UUIDs as their primary identifier:
///
/// ## UUID (`id()`)
/// - Type: `&NoteId` (Bear's UUID like 'ABC123-DEF456-...')
/// - **Always present** (never NULL)
/// - Stable across the lifetime of the note and across devices
/// - Use this for all programmatic references and API calls
/// - Bear uses this for syncing and x-callback-url schemes
/// - Used in Bear's x-callback-url API (e.g., `bear://x-callback-url/open-note?id=UUID`)
///
/// The internal Core Data primary key is not exposed in the public API.
///
/// # Example
///
/// ```no_run
/// # use bear_query::{BearDb, NotesQuery};
/// # fn main() -> Result<(), bear_query::BearError> {
/// let db = BearDb::new()?;
/// let notes = db.notes(NotesQuery::default())?;
///
/// for note in notes {
///     let note_id = note.id();
///     let title = note.title();
///
///     // Only content may be None
///     let content = note.content().unwrap_or("");
///
///     println!("Note {}: {} ({} bytes)", note_id.as_str(), title, content.len());
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct Note {
  _core_db_id: CoreDbNoteId,
  id: NoteId,
  title: String,
  content: Option<String>,
  modified: OffsetDateTime,
  created: OffsetDateTime,
  is_pinned: bool,
}

impl Note {
  /// Returns the note's internal Core Data ID (for internal use only).
  ///
  /// This is used internally for database queries and joins.
  /// External users should use `id()` which returns Bear's UUID.
  pub(crate) fn _core_db_id(&self) -> CoreDbNoteId {
    self._core_db_id
  }

  /// Returns the note's Bear identifier.
  ///
  /// This is Bear's UUID identifier, which is:
  /// - Always present (never NULL)
  /// - Stable across the note's lifetime
  /// - Works across devices (syncing)
  /// - Used in Bear's x-callback-url API
  ///
  /// Use this for:
  /// - Opening notes in Bear
  /// - Storing references to notes
  /// - Matching notes across devices
  ///
  /// # Example
  ///
  /// ```no_run
  /// # use bear_query::{BearDb, NotesQuery};
  /// # fn main() -> Result<(), bear_query::BearError> {
  /// # let db = BearDb::new()?;
  /// # let notes = db.notes(NotesQuery::default())?;
  /// # let note = &notes[0];
  /// let note_id = note.id();
  /// println!("Open in Bear: bear://x-callback-url/open-note?id={}", note_id.as_str());
  /// # Ok(())
  /// # }
  /// ```
  pub fn id(&self) -> &NoteId {
    &self.id
  }

  /// Returns the note's title.
  ///
  /// All notes have titles (this is never NULL), though the title may be an empty string.
  ///
  /// # Example
  ///
  /// ```no_run
  /// # use bear_query::{BearDb, NotesQuery};
  /// # fn main() -> Result<(), bear_query::BearError> {
  /// # let db = BearDb::new()?;
  /// # let notes = db.notes(NotesQuery::default())?;
  /// # let note = &notes[0];
  /// let title = note.title();
  /// if title.is_empty() {
  ///     println!("[Untitled]");
  /// } else {
  ///     println!("Title: {}", title);
  /// }
  /// # Ok(())
  /// # }
  /// ```
  pub fn title(&self) -> &str {
    &self.title
  }

  /// Returns the note's content (Markdown), if present.
  ///
  /// Returns `None` for empty notes or notes with NULL content.
  ///
  /// # Example
  ///
  /// ```no_run
  /// # use bear_query::{BearDb, NotesQuery};
  /// # fn main() -> Result<(), bear_query::BearError> {
  /// # let db = BearDb::new()?;
  /// # let notes = db.notes(NotesQuery::default())?;
  /// # let note = &notes[0];
  /// let content = note.content().unwrap_or("");
  /// println!("Content length: {} bytes", content.len());
  /// # Ok(())
  /// # }
  /// ```
  pub fn content(&self) -> Option<&str> {
    self.content.as_deref()
  }

  /// Returns the timestamp of the note's last modification.
  ///
  /// This is always present (never NULL).
  pub fn modified(&self) -> OffsetDateTime {
    self.modified
  }

  /// Returns the timestamp when the note was created.
  ///
  /// This is always present (never NULL).
  pub fn created(&self) -> OffsetDateTime {
    self.created
  }

  /// Returns whether the note is pinned.
  ///
  /// This is always present (never NULL).
  pub fn is_pinned(&self) -> bool {
    self.is_pinned
  }
}

/// Helper to construct Note from a database row
pub(crate) fn note_from_row(row: &Row) -> rusqlite::Result<Note> {
  Ok(Note {
    _core_db_id: row.get("core_db_id")?,
    id: NoteId::new(row.get("id")?),
    title: row.get("title")?,
    content: row.get("content")?,
    created: row.get("created")?,
    modified: row.get("modified")?,
    is_pinned: row.get("is_pinned")?,
  })
}

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

  /// Test NoteId::new and as_str
  #[test]
  fn test_note_id_construction() {
    let uuid = "ABC123-DEF456-GHI789".to_string();
    let note_id = NoteId::new(uuid.clone());
    assert_eq!(note_id.as_str(), &uuid);

    // Test round-trip
    let uuid2 = "note-uuid-12345".to_string();
    let note_id2 = NoteId::new(uuid2.clone());
    assert_eq!(note_id2.into_string(), uuid2);
  }

  /// Test TagId::new and as_i64
  #[test]
  fn test_bear_tag_id_construction() {
    let tag_id = TagId::new(42);
    assert_eq!(tag_id.as_i64(), 42);

    // Test round-trip
    let id_value = 12345;
    let tag_id = TagId::new(id_value);
    assert_eq!(tag_id.as_i64(), id_value);
  }

  /// Test NoteId equality and hashing
  #[test]
  fn test_note_id_equality() {
    let id1 = NoteId::new("ABC123".to_string());
    let id2 = NoteId::new("ABC123".to_string());
    let id3 = NoteId::new("DEF456".to_string());

    assert_eq!(id1, id2);
    assert_ne!(id1, id3);

    // Test that they can be used in HashSet
    let mut set = std::collections::HashSet::new();
    set.insert(id1);
    assert!(set.contains(&id2));
    assert!(!set.contains(&id3));
  }

  /// Test TagId equality and hashing
  #[test]
  fn test_bear_tag_id_equality() {
    let id1 = TagId::new(42);
    let id2 = TagId::new(42);
    let id3 = TagId::new(43);

    assert_eq!(id1, id2);
    assert_ne!(id1, id3);

    // Test that they can be used in HashSet
    let mut set = std::collections::HashSet::new();
    set.insert(id1);
    assert!(set.contains(&id2));
    assert!(!set.contains(&id3));
  }
}