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
|
//! Pure helpers (no Chromium).
use crate::format::{format_apple_note, build_paragraphs, Paragraph};
/// Format title + body for Apple Notes (Title / headings / lists / body spacing).
pub fn format_note_text(title: &str, body: &str) -> String {
format_apple_note(title, body)
}
/// Structured paragraphs (for browser styling when available).
pub fn note_paragraphs(title: &str, body: &str) -> Vec<Paragraph> {
build_paragraphs(title, 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::<String>();
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!(format_note_text("Hi", "").starts_with("Hi"));
let body_only = format_note_text("", "Just a body paragraph.");
assert!(body_only.contains("Just a body paragraph."));
}
#[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"));
}
}
|