summaryrefslogtreecommitdiff
path: root/src/helpers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers.rs')
-rw-r--r--src/helpers.rs142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/helpers.rs b/src/helpers.rs
new file mode 100644
index 0000000..c6a8163
--- /dev/null
+++ b/src/helpers.rs
@@ -0,0 +1,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"));
+ }
+}