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
//! # logbook
//!
//! Core library for the [`logbook`](https://github.com/jeffbai996/logbook)
//! CLI. The binary in `src/main.rs` is a thin wrapper around the pure
//! functions and types defined here, which lets the integration suite
//! exercise the parser, error paths, and atomic-write logic directly
//! without shelling out.
//!
//! Most consumers should use the CLI; this library is exposed so that
//! tooling (test suites, downstream integrations, alternative front-ends
//! like a web viewer) can manipulate logbook files without re-implementing
//! the format.
//!
//! ## Example: append an entry to a file
//!
//! ```no_run
//! use logbook::{atomic_append, init_file, render_entry_block, RenderInput, today};
//! use std::path::Path;
//!
//! let path = Path::new("logbook.md");
//! init_file(path)?;
//!
//! let date = today();
//! let block = render_entry_block(&RenderInput {
//! date: &date,
//! title: "switched to websockets",
//! why: "polling was hammering the API",
//! rejected: Some("redis pub/sub (overkill)"),
//! risk: None,
//! tags: &["refactor".to_string(), "perf".to_string()],
//! });
//! atomic_append(path, &block)?;
//! # Ok::<(), logbook::Error>(())
//! ```
//!
//! ## Example: parse an existing file
//!
//! ```
//! use logbook::parse_entries;
//!
//! let text = "# logbook\n\n## 2026-05-16 — t\n**why:** w\n**tags:** refactor, perf\n";
//! let entries = parse_entries(text);
//! assert_eq!(entries.len(), 1);
//! assert_eq!(entries[0].date.as_deref(), Some("2026-05-16"));
//! assert_eq!(entries[0].tags, vec!["refactor", "perf"]);
//! ```
pub use ;
pub use ;
pub use ;
use Local;
use PathBuf;
/// Default filename used when [`ENV_VAR`] is not set: `logbook.md`.
pub const DEFAULT_LOGBOOK_FILE: &str = "logbook.md";
/// Environment variable that overrides the default logbook path.
///
/// Set `LOGBOOK_FILE` to any path (relative or absolute) and the CLI will
/// read and write that file instead of `./logbook.md`. Useful for
/// monorepos (`LOGBOOK_FILE=docs/decisions.md`) or for keeping a personal
/// log in a fixed location across projects.
pub const ENV_VAR: &str = "LOGBOOK_FILE";
/// Markdown header written to a freshly-initialized logbook file.
///
/// Ends with a blank line so subsequent entries don't merge into the header.
pub const HEADER: &str = "# logbook\n\nAppend-only record of architectural decisions for this project.\nNewest entries at the bottom. Generated and maintained by `logbook` — https://github.com/jeffbai996/logbook\n\n";
/// Resolve the logbook file path.
///
/// Returns `$LOGBOOK_FILE` if set, otherwise `./logbook.md`. The path is
/// not canonicalized — callers can do that themselves if they want an
/// absolute path (the CLI's `where` subcommand does).
///
/// # Example
///
/// ```
/// use logbook::{logbook_path, DEFAULT_LOGBOOK_FILE};
/// use std::path::PathBuf;
///
/// std::env::remove_var("LOGBOOK_FILE");
/// assert_eq!(logbook_path(), PathBuf::from(DEFAULT_LOGBOOK_FILE));
/// ```
/// Today's date in `YYYY-MM-DD` format, local time.
///
/// Centralized so that any future change to the date convention (UTC,
/// fixed timezone, ISO 8601 with time component) only needs one edit.
///
/// # Example
///
/// ```
/// let today = logbook::today();
/// assert_eq!(today.len(), 10);
/// assert_eq!(today.chars().filter(|c| *c == '-').count(), 2);
/// ```
/// Cheap shape check for date arguments.
///
/// Returns `true` iff `s` looks like `YYYY-MM-DD` — ten characters,
/// dashes at positions 4 and 7, digits everywhere else. **This is not
/// calendar validation**: `2026-13-99` shapes correctly even though it
/// isn't a real day. That's intentional — wrong real-world dates just
/// return no matches when used as filters, which is the right UX. Full
/// calendar validation would force us to take on a date-parsing dep
/// like `chrono::NaiveDate::parse_from_str` for negligible value.
///
/// # Example
///
/// ```
/// use logbook::is_date_shaped;
///
/// assert!(is_date_shaped("2026-05-16"));
/// assert!(!is_date_shaped("2026-5-16"));
/// assert!(!is_date_shaped("banana1234"));
/// assert!(!is_date_shaped("2O26-05-16")); // letter O, not zero
/// ```