chiark / gitweb /
Process raw stream data into UTF-8 lines.
authorSimon Tatham <anakin@pobox.com>
Sat, 23 Dec 2023 12:51:03 +0000 (12:51 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 23 Dec 2023 12:51:03 +0000 (12:51 +0000)
src/main.rs

index 550e9da232ae0746039a16ca5a01d94ce1d2c4b5..9ea485a74bb42c3ca006e373e0bacc4c321d7339 100644 (file)
@@ -16,11 +16,30 @@ fn main() -> Result<(), mastodonochrome::OurError> {
         Ok(d) => Ok(d),
     }?;
 
+    let mut vec: Vec<u8> = Vec::new();
+
     const BUFSIZE: usize = 4096;
     let mut buf: [u8; BUFSIZE] = [0; BUFSIZE];
     while let Ok(sz) = rsp.read(&mut buf) {
         let read = &buf[..sz];
-        dbg!(read);
+        vec.extend_from_slice(read);
+        vec = 'outer: loop {
+            for line in vec.split_inclusive(|c| *c == 10) {
+                if !line.ends_with(&[10]) {
+                    let mut newvec = Vec::new();
+                    newvec.extend_from_slice(line);
+                    break 'outer newvec;
+                } else {
+                    match std::str::from_utf8(&line) {
+                        Err(e) => { dbg!(e); () },
+                        Ok(d) => { dbg!(d); () },
+                    };
+                }
+            }
+            // If we didn't get a partial line inside the loop, then
+            // we must have an empty buffer here
+            break Vec::new();
+        };
     }
 
     Ok(())