Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
A note says what it says, blank lines included
Yashwanth Kamireddi379564480b00
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rsindex f759c58..d507a34 100644--- a/crates/quince-icloud/src/ckdocument.rs+++ b/crates/quince-icloud/src/ckdocument.rs@@ -176,8 +176,13 @@ fn usable_text(bytes: &[u8]) -> Option<String> {{return None;}- let trimmed = text.trim();- (!trimmed.is_empty()).then(|| trimmed.to_string())+ // Trimmed to decide whether this is text at all, never to decide what the text is.+ // Handing back the trimmed version lost every blank line a note opened or ended with —+ // and worse, an edit then wrote the shortened version back, so opening a note in this+ // window and typing one character deleted the spacing above it for good. It also made a+ // note whose first line is blank look titled here and untitled on the phone, because the+ // phone takes the first line exactly as it finds it.+ (!text.trim().is_empty()).then(|| text.to_string())}/// Write `text` back in the shape Apple reads: protobuf at field path 2 → 3 → 2, gzipped,@@ -1184,7 +1189,7 @@ mod tests {let encoded = text_to_base64(text);assert_eq!(text_from_base64(&encoded).as_deref(),- Some(text.trim()),+ Some(text),"round trip failed for {text:?}");}@@ -1740,3 +1745,59 @@ mod merge_record_tests {assert!(has_merge_records(&swapped));}}++/// Text comes back as it was written, including the parts that look like nothing.+#[cfg(test)]+mod whitespace_tests {+ use super::*;++ /// A note that opens with blank lines keeps them. Handing back a trimmed version was not+ /// only wrong on screen — the next edit wrote the shortened text back, so opening such a+ /// note here and typing one character deleted the spacing above it for good.+ #[test]+ fn blank_lines_a_note_opens_or_ends_with_survive() {+ for text in [+ "\n\n\nhello",+ "hello\n\n\n",+ "\n\n\nhello\n\n",+ " indented",+ "trailing spaces ",+ ] {+ assert_eq!(+ text_from_base64(&text_to_base64(text)).as_deref(),+ Some(text),+ "{text:?} did not survive"+ );+ }+ }++ /// And an edit to such a note only changes what was edited.+ #[test]+ fn editing_a_note_that_opens_with_blank_lines_keeps_them() {+ let original = text_to_base64("\n\n\nhello");+ let edited = replace_text_in_base64(&original, "\n\n\nhello there").unwrap();+ assert_eq!(+ text_from_base64(&edited).as_deref(),+ Some("\n\n\nhello there")+ );+ }++ /// Nothing at all is still nothing — the emptiness test is what the trim was for, and it+ /// still does that job.+ #[test]+ fn whitespace_alone_is_still_not_text() {+ assert_eq!(usable_text(b""), None);+ assert_eq!(usable_text(b" "), None);+ assert_eq!(usable_text(b"\n\n\n"), None);+ assert_eq!(usable_text(b"\t \n"), None);+ }++ /// The run has to measure the whole text, blank lines included, or the phone renders+ /// against a length that is short.+ #[test]+ fn the_run_covers_the_blank_lines_too() {+ let encoded = text_to_base64("\n\n\nhello");+ let raw = B64.decode(encoded).unwrap();+ assert_eq!(debug_attribute_lengths(&raw), vec![8]);+ }+}diff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rsindex 2adf7d0..c02e4a5 100644--- a/crates/quince-icloud/src/notes.rs+++ b/crates/quince-icloud/src/notes.rs@@ -262,9 +262,18 @@ fn now_millis() -> i64 {/// from this app looking, in the list on a phone, exactly like one typed on the phone.#[must_use]pub fn title_and_snippet(body: &str) -> (String, String) {- let mut lines = body.lines().map(str::trim).filter(|l| !l.is_empty());- let title = lines.next().unwrap_or_default().to_string();- let snippet = lines.next().unwrap_or_default().to_string();+ // The first line exactly as written, blank or not, and the next line that says something.+ //+ // Skipping the blank ones meant a note that opens with an empty line was titled here and+ // untitled on the phone, which takes the first line as it finds it. Two apps disagreeing+ // about what a note is called is worse than an untitled note.+ let mut lines = body.lines();+ let title = lines.next().unwrap_or_default().trim().to_string();+ let snippet = lines+ .map(str::trim)+ .find(|l| !l.is_empty())+ .unwrap_or_default()+ .to_string();(title, snippet)}@@ -1117,3 +1126,37 @@ mod template_tests {}}}++/// What a note is called, and what shows under the name.+#[cfg(test)]+mod title_tests {+ use super::*;++ /// A phone takes the first line as it finds it. Skipping blank ones meant a note that+ /// opens with an empty line read as titled here and untitled there — two apps disagreeing+ /// about what the same note is called.+ #[test]+ fn the_title_is_the_first_line_even_when_it_is_empty() {+ assert_eq!(title_and_snippet("\nhello").0, "");+ assert_eq!(title_and_snippet("\n\n\nhello").0, "");+ assert_eq!(title_and_snippet("hello\nworld").0, "hello");+ assert_eq!(title_and_snippet(" hello \nworld").0, "hello");+ }++ /// The line under it is the next one that says something, so a note separated by a blank+ /// line still previews.+ #[test]+ fn the_preview_is_the_next_line_with_something_on_it() {+ assert_eq!(title_and_snippet("Shopping\n\n\nMilk").1, "Milk");+ assert_eq!(title_and_snippet("Shopping\nMilk").1, "Milk");+ assert_eq!(title_and_snippet("Shopping").1, "");+ assert_eq!(title_and_snippet("").1, "");+ }++ /// A note of nothing but blank lines has no name and nothing to preview, which is what it+ /// is rather than an error.+ #[test]+ fn a_note_of_blank_lines_has_neither() {+ assert_eq!(title_and_snippet("\n\n\n"), (String::new(), String::new()));+ }+}