Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From blather-0.11.0 To blather-0.12.0
2025-06-10
| ||
22:53 | Dependencies. Leaf check-in: 57cb959ef2 user: jan tags: trunk | |
2025-05-18
| ||
21:58 | Experiment with CbErr instead of From<Error> for vec encoding/decoding. Leaf check-in: b4da21ab85 user: jan tags: cberr | |
2025-05-13
| ||
15:55 | Release maintenance. check-in: e2b1017969 user: jan tags: blather-0.12.0, trunk | |
15:48 | Document document generation. check-in: 48942b13bc user: jan tags: trunk | |
2025-05-01
| ||
11:20 | Merge. check-in: 4984cb9c5e user: jan tags: trunk | |
2025-04-30
| ||
20:36 | Redesign. check-in: 9f97f187cc user: jan tags: redesign | |
2024-09-22
| ||
14:26 | Release maintenance. check-in: f9ffae0254 user: jan tags: blather-0.11.0, trunk | |
12:02 | Docs. check-in: 69ffa6c624 user: jan tags: trunk | |
Changes to .efiles.
1 2 3 4 5 6 | 1 2 3 4 5 6 7 8 9 10 11 | - - + + - - - + + - - | Cargo.toml README.md www/index.md www/changelog.md src/err.rs src/lib.rs |
Changes to Cargo.toml.
1 2 | 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 | - - + + + + + + + + + - - - + + + - - + + - + + + - + - + | [package] name = "blather" |
Changes to rustfmt.toml.
1 2 | 1 2 3 4 5 6 7 8 9 10 | - + | blank_lines_upper_bound = 2 comment_width = 79 |
︙ |
Changes to src/codec.rs.
1 2 3 | 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 | - - - - - + + + + - + + + + | //! A [`tokio_util::codec`] Codec that is used to encode and decode the //! blather protocol. |
︙ | |||
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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 | + + + + - + + - | /// A chunk of raw data has arrived. The second argument is the amount of /// data remains, which has been adjusted for the current [`Bytes`]. If /// the `u64` parameter is 0 it means this is the final chunk. Chunk(Bytes, u64), /// A complete raw immutable buffer has been received. Bytes(Bytes), /// Sentinel value used to signal that transfer of buffers to a channel is /// done. BytesChDone, /// The requested number of bytes have been ignored. SkipDone } /// The Codec is used to keep track of the state of the inbound and outbound /// communication. pub struct Codec { next_line_index: usize, max_line_length: usize, tg: Telegram, params: Params, kvlines: KVLines, state: CodecState, |
︙ | |||
128 129 130 131 132 133 134 | 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 178 | - + + - - | Self { next_line_index: 0, max_line_length: usize::MAX, tg: Telegram::new_uninit(), params: Params::new(), kvlines: KVLines::new(), state: CodecState::Telegram, |
︙ | |||
280 281 282 283 284 285 286 | 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | - | // Returning Ok(None) instructs the FramedRead that more data is // needed. return Ok(None); } } } |
︙ | |||
349 350 351 352 353 354 355 | 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | + - + - - + - - + + + + + + + + + + + + + + + + + + + - + - + - + | // taken). return Ok(Some(mem::take(&mut self.kvlines))); } let idx = line.find(' '); if let Some(idx) = idx { let (k, v) = line.split_at(idx); let v = &v[1..v.len()]; |
︙ | |||
566 567 568 569 570 571 572 573 574 575 576 577 578 579 | 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | Ok(None) } else { // Revert to expecting Telegram lines self.state = CodecState::Telegram; Ok(Some(Input::Bytes(buf.split_to(remain).freeze()))) } } CodecState::BytesCh => { // Calculate how much data to take off read buffer, capping at // `remain`. let read_to = cmp::min(self.remain, buf.len() as u64); self.remain -= read_to; // Return a buffer and the amount of data remaining, this buffer // included. The application can check if remain is 0 to determine // if it has received all the expected binary data. // // .unwrap() is safe here, because read_to is guaranteed to // be within the bounds of an usize due to the `cmp::min()` above. let len = usize::try_from(read_to).unwrap(); let buf = buf.split_to(len).freeze(); if let Some(ref tx) = self.bytes_tx { // Ignore errors -- we'll assume this means the application dropped // the reader end-point to signal it is no longer interested in // receiving the data. let _ = tx.send(buf); } // If we have all the expected data, then report that we're done with a // senitnel value. Otherwise report back to caller that more data is // needed. if self.remain == 0 { // When no more data is expected for this binary part, revert to // expecting a Telegram self.state = CodecState::Telegram; // Send an empty buffer to signal eof, then drop the end-point if let Some(tx) = self.bytes_tx.take() { let _ = tx.send(Bytes::new()); } // Return sentinel value just to signal that we're done Ok(Some(Input::BytesChDone)) } else { Ok(None) } } CodecState::Skip => { if buf.is_empty() { return Ok(None); // Need more data } // Read as much data as available or requested and write it to our |
︙ | |||
700 701 702 703 704 705 706 707 | 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | ) -> Result<(), crate::err::Error> { buf.reserve(data.len()); buf.put(data); Ok(()) } } #[cfg(test)] mod tests { use { futures::sink::SinkExt, tokio::sync::mpsc::unbounded_channel, tokio_stream::StreamExt, tokio_test::io::Builder, tokio_util::codec::Framed }; use bytes::BytesMut; use super::{Bytes, Codec, Input, Telegram}; #[tokio::test] async fn valid_no_params() { let mut mock = Builder::new(); mock.read(b"hello\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); while let Some(o) = frm.next().await { let o = o.unwrap(); if let Input::Telegram(tg) = o { assert_eq!(tg.get_topic(), "hello"); let params = tg.into_params(); let map = params.into_inner(); assert_eq!(map.len(), 0); } else { panic!("Not a Telegram"); } } } #[tokio::test] async fn valid_with_params() { let mut mock = Builder::new(); mock.read(b"hello\nmurky_waters off\nwrong_impression cows\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); while let Some(o) = frm.next().await { let o = o.unwrap(); match o { Input::Telegram(tg) => { assert_eq!(tg.get_topic(), "hello"); let params = tg.into_params(); let map = params.into_inner(); assert_eq!(map.len(), 2); assert_eq!(map.get("murky_waters").unwrap(), "off"); assert_eq!(map.get("wrong_impression").unwrap(), "cows"); } _ => { panic!("Not a Telegram"); } } } } #[tokio::test] #[should_panic( expected = "Protocol(\"Bad format; Invalid topic character\")" )] async fn bad_topic() { let mut mock = Builder::new(); // space isn't allowed in topic mock.read(b"hel lo\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); let e = frm.next().await.unwrap(); e.unwrap(); } #[tokio::test] async fn multiple() { let mut mock = Builder::new(); mock.read(b"hello\nfoo bar\n\nworld\nholy cows\n\nfinal\nthe thing\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); let o = frm.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectely not Input::Telegram"); }; assert_eq!(tg.get_topic(), "hello"); assert_eq!(tg.get_str("foo"), Some("bar")); let o = frm.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectely not Input::Telegram"); }; assert_eq!(tg.get_topic(), "world"); assert_eq!(tg.get_str("holy"), Some("cows")); let o = frm.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectely not Input::Telegram"); }; assert_eq!(tg.get_topic(), "final"); assert_eq!(tg.get_str("the"), Some("thing")); } #[tokio::test] async fn tg_followed_by_buf() { let mut mock = Builder::new(); mock.read(b"hello\nlen 4\n\n1234"); let mut frm = Framed::new(mock.build(), Codec::new()); let Some(o) = frm.next().await else { panic!("No frame"); }; let o = o.unwrap(); if let Input::Telegram(tg) = o { assert_eq!(tg.get_topic(), "hello"); assert_eq!(tg.get_fromstr::<usize, _>("len").unwrap().unwrap(), 4); frm.codec_mut().expect_bytes(4).unwrap(); } else { panic!("Not a Telegram"); } while let Some(o) = frm.next().await { let o = o.unwrap(); if let Input::Bytes(_bm) = o { } else { panic!("Not a Buf"); } } } #[tokio::test] async fn tg_buf_tg() { let mut mock = Builder::new(); mock.read(b"hello\nlen 4\n\n1234world\nfoo bar\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); // Expect Telegram, which sets up for getting Bytes let o = frm.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectedly not Input::Telegram(_)"); }; assert_eq!(tg.get_topic(), "hello"); let len = tg.get_fromstr::<usize, _>("len").unwrap().unwrap(); assert_eq!(len, 4); frm.codec_mut().expect_bytes(len).unwrap(); // Expect Bytes let o = frm.next().await.unwrap().unwrap(); let Input::Bytes(buf) = o else { panic!("Unexpectedly not Input::Bytes(_)"); }; assert_eq!(buf, "1234"); // Expect Telegram let o = frm.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectedly not Input::Telegram(_)"); }; assert_eq!(tg.get_topic(), "world"); assert_eq!(tg.get_str("foo"), Some("bar")); } #[tokio::test] async fn expect_bytes_ch() { let (client, server) = tokio::io::duplex(64); let mut frmin = Framed::new(server, Codec::new()); let mut frmout = Framed::new(client, Codec::new()); // Spawn server task let jh = tokio::task::spawn(async move { let o = frmin.next().await.unwrap().unwrap(); let Input::Telegram(tg) = o else { panic!("Unexpectedly not Input::Telegram(_)"); }; assert_eq!(tg.as_ref(), "ReqToSend"); let len = tg.get_fromstr::<u64, _>("Len").unwrap().unwrap(); // Create a channel for receiving stream of Bytes let (tx, mut rx) = unbounded_channel(); // Expect requested number of bytes frmin.codec_mut().expect_bytes_channel(len, tx).unwrap(); // Spawn task for receiving expected data through channel let jh = tokio::task::spawn(async move { let mut inbuf = BytesMut::new(); inbuf.reserve(16); // Receive the expected amount of bytes over channel loop { let buf = rx.recv().await.unwrap(); if buf.is_empty() { break; } // ToDo: Verify buffer contents inbuf.extend_from_slice(&buf); } let buf = inbuf.freeze(); assert_eq!(buf, "0123456789abcdef"); }); // Wait for codec to report that it is done feeding data to the channel let o = frmin.next().await.unwrap().unwrap(); let Input::BytesChDone = o else { panic!("Unexpectedly not Input::BytesChDone"); }; jh.await.unwrap(); }); let len = 16; let mut tg = Telegram::new("ReqToSend"); tg.add_param("Len", len).unwrap(); frmout.send(&tg).await.unwrap(); // Send `len` amount of binary data frmout.send(Bytes::from("0123")).await.unwrap(); frmout.send(Bytes::from("4567")).await.unwrap(); frmout.send(Bytes::from("89ab")).await.unwrap(); frmout.send(Bytes::from("cdef")).await.unwrap(); jh.await.unwrap(); } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : |
Deleted src/codec/utils.rs.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
Changes to src/err.rs.
1 2 3 4 5 6 7 8 9 10 11 12 | 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 | + + + + + + + + + + + - - - - - + + + + + + + + + + + + + - - - + + + - - + + + + + + + + + + | //! Error types and error management functions. use std::fmt; use tokio::io; /// Specifies whether a parameter key or value is invalid. #[derive(Debug)] pub enum ParamError { /// The key is invalid. Key(String), /// The value is invalid. Value(String) } /// Error that `blather` can emit. #[derive(Debug)] pub enum Error { /// The input format of a buffer was incorrect. BadFormat(String), |
︙ |
Added src/kvlines.rs.