Milestone 1: sync engine and lunesync CLI
Rust workspace with core (watch, settle, save, sync over a plain Git remote, keep-both on same-line edits, offline queueing, tidy) and the lunesync CLI (device, add, run, service, status, checkpoint, tidy, history). Tests cover byte-exact round trips, keep-both, offline catch-up and adopting an existing vault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
83
core/src/names.rs
Normal file
83
core/src/names.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
//! File-name helpers: "kept both" copies, case-clash renames, and path conversions.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Vault-relative path with forward slashes, as Git stores it.
|
||||
pub fn slash_path(p: &Path) -> String {
|
||||
p.to_string_lossy().replace('\\', "/")
|
||||
}
|
||||
|
||||
pub fn bytes_path(b: &[u8]) -> PathBuf {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
PathBuf::from(std::ffi::OsStr::from_bytes(b))
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
PathBuf::from(String::from_utf8_lossy(b).into_owned())
|
||||
}
|
||||
|
||||
/// Splits `dir/name.ext` into (`dir/name`, `.ext`). Dotfiles like `.env` have no extension.
|
||||
fn split_ext(path: &str) -> (&str, &str) {
|
||||
let file_start = path.rfind('/').map_or(0, |i| i + 1);
|
||||
match path[file_start..].rfind('.') {
|
||||
Some(dot) if dot > 0 => path.split_at(file_start + dot),
|
||||
_ => (path, ""),
|
||||
}
|
||||
}
|
||||
|
||||
/// `runbook.md` → `runbook (Codex's version).md`; n > 1 adds a number.
|
||||
pub fn copy_name(path: &str, label: &str, n: usize) -> String {
|
||||
let (stem, ext) = split_ext(path);
|
||||
let num = if n > 1 { format!(" {n}") } else { String::new() };
|
||||
format!("{stem} ({label}'s version{num}){ext}")
|
||||
}
|
||||
|
||||
/// `Notes.md` → `Notes (name clash).md`; n > 1 adds a number.
|
||||
pub fn clash_name(path: &str, n: usize) -> String {
|
||||
let (stem, ext) = split_ext(path);
|
||||
let num = if n > 1 { format!(" {n}") } else { String::new() };
|
||||
format!("{stem} (name clash{num}){ext}")
|
||||
}
|
||||
|
||||
/// `runbook (from Codex).md`: what a copy becomes when both are kept for good.
|
||||
pub fn kept_name(original: &str, label: &str, n: usize) -> String {
|
||||
let (stem, ext) = split_ext(original);
|
||||
let num = if n > 1 { format!(" {n}") } else { String::new() };
|
||||
format!("{stem} (from {label}{num}){ext}")
|
||||
}
|
||||
|
||||
/// If `path` is a kept-both copy, returns (original path, device label).
|
||||
pub fn parse_copy(path: &str) -> Option<(String, String)> {
|
||||
const MARK: &str = "'s version";
|
||||
let file_start = path.rfind('/').map_or(0, |i| i + 1);
|
||||
let name = &path[file_start..];
|
||||
let v = name.rfind(MARK)?;
|
||||
let open = name[..v].rfind(" (")?;
|
||||
let label = &name[open + 2..v];
|
||||
let rest = &name[v + MARK.len()..];
|
||||
let close = rest.find(')')?;
|
||||
let num = &rest[..close];
|
||||
let num_ok = num.is_empty() || (num.len() > 1 && num.starts_with(' ') && num[1..].bytes().all(|b| b.is_ascii_digit()));
|
||||
let ext = &rest[close + 1..];
|
||||
if label.is_empty() || !num_ok || !(ext.is_empty() || ext.starts_with('.')) {
|
||||
return None;
|
||||
}
|
||||
Some((format!("{}{}{}", &path[..file_start], &name[..open], ext), label.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn copy_names_round_trip() {
|
||||
for (p, l, n) in [("runbook.md", "Codex", 1), ("ops/vm list.md", "Hermes", 2), (".env", "VPS", 1), ("a.tar.gz", "x", 1), ("README", "Mac", 3)] {
|
||||
let c = copy_name(p, l, n);
|
||||
assert_eq!(parse_copy(&c), Some((p.to_string(), l.to_string())), "{c}");
|
||||
}
|
||||
assert_eq!(copy_name("ops/runbook.md", "Codex", 1), "ops/runbook (Codex's version).md");
|
||||
assert_eq!(parse_copy("notes/plain.md"), None);
|
||||
assert_eq!(parse_copy("Sen's version.md"), None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user