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
|
//! 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::<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))
}
/// 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 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"));
}
}
|