//! Pure helpers (no Chromium). /// Join title + body the way Apple Notes expects (first line = title). pub fn format_note_text(title: &str, body: &str) -> String { match (title.is_empty(), body.is_empty()) { (true, true) => String::new(), (false, true) => title.to_string(), (true, false) => body.to_string(), (false, false) => format!("{title}\n\n{body}"), } } pub fn is_apple_auth_url(url: &str) -> bool { url.contains("idmsa.apple.com") || url.contains("appleid.apple.com") } #[derive(Debug, Default, Clone)] pub struct LoggedOutSignals { pub url: String, pub body_text: String, pub has_password_field: bool, pub has_sign_in_control: bool, pub has_notes_app: bool, } pub fn looks_logged_out(s: &LoggedOutSignals) -> bool { if is_apple_auth_url(&s.url) { return true; } let t = s.body_text.chars().take(4000).collect::(); if t.to_ascii_lowercase().contains("sign in to icloud") { return true; } if s.has_password_field { return true; } if s.has_sign_in_control && !s.has_notes_app { return true; } false } pub fn require_create_content(title: &str, body: &str) -> Result<(String, String), String> { let title = title.to_string(); let body = body.trim_end().to_string(); if title.is_empty() && body.is_empty() { return Err( "Provide --title and/or --body / --body-file / stdin".to_string(), ); } Ok((title, body)) } /// Update requires a title to locate the existing note. pub fn require_update_content(title: &str, body: &str) -> Result<(String, String), String> { let title = title.trim().to_string(); let body = body.trim_end().to_string(); if title.is_empty() { return Err( "Provide --title (existing note title to update)".to_string(), ); } Ok((title, body)) } /// Case-insensitive title equality for matching Notes.app titles. pub fn titles_match(a: &str, b: &str) -> bool { a.trim().eq_ignore_ascii_case(b.trim()) } /// Conventional Commits 1.0.0 subject line (no regex crate). pub fn is_conventional_commit_subject(subject: &str) -> bool { let line = subject.trim(); let types = [ "feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert", ]; let Some((ty, rest)) = line.split_once(':') else { return false; }; if !rest.starts_with(' ') || rest.len() < 2 { return false; } let ty = ty.trim_end_matches('!'); if let Some((base, scope)) = ty.split_once('(') { if !types.contains(&base) { return false; } scope.ends_with(')') && scope.len() > 1 && scope[..scope.len() - 1] .chars() .all(|c| c.is_ascii_alphanumeric() || "/._-".contains(c)) } else { types.contains(&ty) } } #[cfg(test)] mod tests { use super::*; #[test] fn format_joins_title_body() { assert_eq!(format_note_text("Hi", "body"), "Hi\n\nbody"); assert_eq!(format_note_text("Hi", ""), "Hi"); assert_eq!(format_note_text("", "body"), "body"); } #[test] fn auth_urls() { assert!(is_apple_auth_url("https://idmsa.apple.com/appleauth/auth")); assert!(is_apple_auth_url("https://appleid.apple.com/")); assert!(!is_apple_auth_url("https://www.icloud.com/notes/")); } #[test] fn logged_out_signals() { assert!(looks_logged_out(&LoggedOutSignals { body_text: "Sign in to iCloud to continue".into(), ..Default::default() })); assert!(looks_logged_out(&LoggedOutSignals { has_password_field: true, ..Default::default() })); assert!(looks_logged_out(&LoggedOutSignals { has_sign_in_control: true, has_notes_app: false, ..Default::default() })); assert!(!looks_logged_out(&LoggedOutSignals { has_sign_in_control: true, has_notes_app: true, ..Default::default() })); } #[test] fn create_content_required() { assert!(require_create_content("", "").is_err()); let (t, b) = require_create_content("T", "hi\n\n").unwrap(); assert_eq!(t, "T"); assert_eq!(b, "hi"); } #[test] fn update_content_requires_title() { assert!(require_update_content("", "body").is_err()); let (t, b) = require_update_content(" Hello ", "body\n").unwrap(); assert_eq!(t, "Hello"); assert_eq!(b, "body"); } #[test] fn titles_match_case_insensitive() { assert!(titles_match("Hello", "hello")); assert!(titles_match(" Foo ", "foo")); assert!(!titles_match("a", "b")); } #[test] fn conventional_subjects() { assert!(is_conventional_commit_subject( "feat: add icloud.com/notes helper and agent skill" )); assert!(is_conventional_commit_subject( "fix: run create/status headless without a visible browser" )); assert!(is_conventional_commit_subject("feat(notes): add dry-run")); assert!(!is_conventional_commit_subject("Added tests")); assert!(!is_conventional_commit_subject("FIX: caps type")); } }