refactor: extract Store methods, clean up shell-outs
- Add Store::upsert() — generic create-or-update, used by cmd_write - Add Store::insert_node() — for pre-constructed nodes (journal entries) - Add Store::delete_node() — soft-delete with version bump - Simplify cmd_write (20 → 8 lines), cmd_node_delete (16 → 7 lines), cmd_journal_write (removes manual append/insert/save boilerplate) - Replace generate_cookie shell-out to head/urandom with direct /dev/urandom read + const alphabet table main.rs: 1137 → 1109 lines.
This commit is contained in:
parent
29d5ed47a1
commit
0ea86b8d54
3 changed files with 62 additions and 55 deletions
|
|
@ -150,19 +150,12 @@ fn load_or_create_cookie(dir: &Path, session_id: &str) -> String {
|
|||
}
|
||||
|
||||
fn generate_cookie() -> String {
|
||||
let out = Command::new("head")
|
||||
.args(["-c", "12", "/dev/urandom"])
|
||||
.output()
|
||||
const ALPHA: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
let mut buf = [0u8; 16];
|
||||
fs::File::open("/dev/urandom")
|
||||
.and_then(|mut f| io::Read::read_exact(&mut f, &mut buf))
|
||||
.expect("failed to read urandom");
|
||||
out.stdout.iter()
|
||||
.map(|b| {
|
||||
let idx = (*b as usize) % 62;
|
||||
if idx < 10 { (b'0' + idx as u8) as char }
|
||||
else if idx < 36 { (b'a' + (idx - 10) as u8) as char }
|
||||
else { (b'A' + (idx - 36) as u8) as char }
|
||||
})
|
||||
.take(16)
|
||||
.collect()
|
||||
buf.iter().map(|b| ALPHA[(*b as usize) % ALPHA.len()] as char).collect()
|
||||
}
|
||||
|
||||
fn load_seen(dir: &Path, session_id: &str) -> HashSet<String> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue