Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From blather-0.9.0 To blather-0.10.0
2024-05-01
| ||
05:25 | Add a utility function for receiving an expected telegram over a Frame'd stream. check-in: b4762b2871 user: jan tags: trunk | |
2024-02-23
| ||
14:32 | Changlog fixup. check-in: a73f934f4c user: jan tags: blather-0.10.0, trunk | |
14:31 | Release maintenance. check-in: 8d3690239e user: jan tags: trunk | |
10:06 | Happy clippy. check-in: f5c317f97c user: jan tags: trunk | |
2022-03-19
| ||
13:19 | Update exclude list and repo url. check-in: 40afa6b119 user: jan tags: blather-0.9.0, trunk | |
13:15 | Edition 2021. Update dependencies. Fix renamed attributes. check-in: 47d98bfea4 user: jan tags: trunk | |
Changes to .efiles.
1 | Cargo.toml | | > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Cargo.toml README.md www/index.md www/changelog.md src/err.rs src/lib.rs src/types.rs src/types/telegram.rs src/types/params.rs src/types/kvlines.rs src/types/validators.rs src/codec.rs tests/*.rs |
Changes to Cargo.toml.
1 2 | [package] name = "blather" | | < < | | > | | | | | > > > | 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 | [package] name = "blather" version = "0.10.0" edition = "2021" license = "0BSD" keywords = [ "line-based", "protocol", "tokio", "codec" ] repository = "https://repos.qrnch.tech/pub/blather" description = "A talkative line-based protocol" exclude = [ ".fossil-settings", ".efiles", ".fslckout", "rustfmt.toml", "www" ] [dependencies] bytes = { version = "1.5.0" } futures = { version = "0.3.30" } # Tempoarily add "net", because there's a rustdoc bug in tokio 1.36.0. tokio = { version = "1.36.0", features = ["net"] } tokio-util = { version= "0.7.10", features = ["codec"] } [dev-dependencies] tokio = { version = "1.36.0", features = ["macros", "net"] } tokio-stream = { version = "0.1.14" } tokio-test = { version = "0.4.3" } [package.metadata.docs.rs] rustdoc-args = ["--generate-link-to-definition"] # vim: set ft=toml et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : |
Added README.md.
> > > > > | 1 2 3 4 5 | # blather A talkative, somwhat reminiscent to HTTP, line-based protocol, implemented as a tokio-util Codec. |
Changes to src/codec.rs.
1 2 3 | //! A [`tokio_util::codec`] Codec that is used to encode and decode the //! blather protocol. | | | < < | > | < | > | > | 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 | //! A [`tokio_util::codec`] Codec that is used to encode and decode the //! blather protocol. use std::{ fmt, {cmp, collections::HashMap, mem} }; use bytes::{BufMut, Bytes, BytesMut}; use tokio::io; use tokio_util::codec::{Decoder, Encoder}; use crate::{ err::Error, {KVLines, Params, Telegram} }; /// Current state of decoder. /// /// Controls what, if anything, will be returned to the application. #[derive(Clone, Debug, PartialEq)] enum CodecState { |
︙ | ︙ | |||
36 37 38 39 40 41 42 | /// arrive. Chunks, /// Read a specified amount of raw bytes, and return the entire immutable /// buffer when it has arrived. Bytes, | < < < < < < < < < < < < | | | < < < < < < < < < < | < < < | 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 | /// arrive. Chunks, /// Read a specified amount of raw bytes, and return the entire immutable /// buffer when it has arrived. Bytes, /// Ignore a specified amount of raw bytes. Skip } /// Data returned to the application when the Codec's Decode iterator is /// called and the decoder has a complete entity to return. pub enum Input { /// A complete [`Telegram`] has been received. Telegram(Telegram), /// A complete key/value lines buffer ([`KVLines`]) has been received. KVLines(KVLines), /// A complete [`Params`] has been received. Params(Params), /// 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), /// 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, remain: u64 } impl fmt::Debug for Codec { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Codec").field("state", &self.state).finish() } } |
︙ | ︙ | |||
134 135 136 137 138 139 140 | /// async fn do_something() { /// let socket = TcpStream::connect("127.0.0.1:8080").await.unwrap(); /// let mut conn = Framed::new(socket, Codec::new()); /// /// // .. do stuff .. /// /// let len = 8192; | | | < < < | 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 | /// async fn do_something() { /// let socket = TcpStream::connect("127.0.0.1:8080").await.unwrap(); /// let mut conn = Framed::new(socket, Codec::new()); /// /// // .. do stuff .. /// /// let len = 8192; /// conn.codec_mut().expect_bytes(len); /// } /// ``` impl Codec { /// Create a new `Codec`. It will default to having not practical limit to /// the maximum line length and it will expect a [`Telegram`] buffer to /// arrive as the first frame. pub fn new() -> Codec { Codec { next_line_index: 0, max_line_length: usize::MAX, tg: Telegram::new(), params: Params::new(), kvlines: KVLines::new(), state: CodecState::Telegram, remain: 0 } } /// Create a new `Codec` with a specific maximum line length. The default /// state will be to expect a [`Telegram`]. pub fn new_with_max_length(max_line_length: usize) -> Self { Codec { |
︙ | ︙ | |||
192 193 194 195 196 197 198 | /// passed the line to this function to process it. /// /// The first line received is a telegram topic. This is a required line. /// Following lines are parameter lines, which are a single space character /// separated key/value pairs. fn decode_telegram_line(&mut self, line: &str) -> Result<(), Error> { if self.tg.get_topic().is_none() { | > > | > | 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | /// passed the line to this function to process it. /// /// The first line received is a telegram topic. This is a required line. /// Following lines are parameter lines, which are a single space character /// separated key/value pairs. fn decode_telegram_line(&mut self, line: &str) -> Result<(), Error> { if self.tg.get_topic().is_none() { self .tg .set_topic(line) .map_err(|e| Error::Protocol(e.to_string()))?; } else { let idx = line.find(' '); if let Some(idx) = idx { let (k, v) = line.split_at(idx); let v = &v[1..v.len()]; self.tg.add_param(k, v)?; } |
︙ | ︙ | |||
239 240 241 242 243 244 245 | } } } */ /// Get index of the next end of line in `buf`. fn get_eol_idx(&mut self, buf: &BytesMut) -> Result<Option<usize>, Error> { | | | | < > | 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | } } } */ /// Get index of the next end of line in `buf`. fn get_eol_idx(&mut self, buf: &BytesMut) -> Result<Option<usize>, Error> { let (read_to, newline_offset) = self.find_newline(buf); match newline_offset { Some(offset) => { // Found an eol let newline_index = offset + self.next_line_index; self.next_line_index = 0; Ok(Some(newline_index + 1)) } None if buf.len() > self.max_line_length => { Err(Error::Protocol("Exceeded maximum line length.".to_string())) } None => { // Didn't find a line or reach the length limit, so the next // call will resume searching at the current offset. self.next_line_index = read_to; // Returning Ok(None) instructs the FramedRead that more data is // needed. |
︙ | ︙ | |||
287 288 289 290 291 292 293 | // Empty line marks end of Telegram if line.is_empty() { // mem::take() can replace a member of a struct. // (This requires Default to be implemented for the object being // taken). return Ok(Some(mem::take(&mut self.tg))); } else { | | | 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | // Empty line marks end of Telegram if line.is_empty() { // mem::take() can replace a member of a struct. // (This requires Default to be implemented for the object being // taken). return Ok(Some(mem::take(&mut self.tg))); } else { self.decode_telegram_line(line)?; } } else { // Returning Ok(None) instructs the FramedRead that more data is // needed. return Ok(None); } } |
︙ | ︙ | |||
391 392 393 394 395 396 397 | /// the actual chunk the number of bytes remaining will be returned. The /// remaining bytes value is adjusted to subtract the currently returned /// chunk, which means that the application can detect the end of the /// buffer by checking if the remaining value is zero. /// /// Once the entire buffer has been received by the `Decoder` it will revert /// to expect an [`Input::Telegram`]. | | > > > > | > > < < < | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < | 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 | /// the actual chunk the number of bytes remaining will be returned. The /// remaining bytes value is adjusted to subtract the currently returned /// chunk, which means that the application can detect the end of the /// buffer by checking if the remaining value is zero. /// /// Once the entire buffer has been received by the `Decoder` it will revert /// to expect an [`Input::Telegram`]. pub fn expect_chunks(&mut self, size: u64) -> Result<(), Error> { if size == 0 { return Err(Error::InvalidSize("The size must not be zero".to_string())); } //println!("Expecting bin {}", size); self.state = CodecState::Chunks; self.remain = size; Ok(()) } /// Expect a immutable buffer of a certain size to be received. /// /// The returned buffer will be stored in process memory. /// /// # Decoder behavior /// Once a complete buffer has been successfully reaceived the `Decoder` will /// return an [`Input::Bytes(b)`](Input::Bytes) where `b` is a /// [`bytes::Bytes`] containing the entire buffer. /// /// Once the entire buffer has been received by the `Decoder` it will revert /// to expect an [`Input::Telegram`]. pub fn expect_bytes(&mut self, size: usize) -> Result<(), Error> { if size == 0 { return Err(Error::InvalidSize("The size must not be zero".to_string())); } self.state = CodecState::Bytes; // unwrap() should be safe, unless running on a platform where // size_of::<usize>() > size_of::<u64>() and the buffer is larger than // usize::MAX. self.remain = size.try_into().unwrap(); Ok(()) } /// Tell the Decoder to expect lines of key/value pairs. /// /// # Decoder behavior /// On successful completion the the decoder will next return an /// [`Input::Params(params)`](Input::Params) once a complete `Params` buffer /// has been received. |
︙ | ︙ | |||
530 531 532 533 534 535 536 | /// Skip a requested number of bytes. /// /// # Decoder behavior /// On successful completion the decoder will have ignored the specified /// number of byes, reverts back to waiting for a [`Input::Telegram`] and /// returns [`Input::SkipDone`]. | < < < | | | 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | /// Skip a requested number of bytes. /// /// # Decoder behavior /// On successful completion the decoder will have ignored the specified /// number of byes, reverts back to waiting for a [`Input::Telegram`] and /// returns [`Input::SkipDone`]. pub fn skip(&mut self, size: u64) -> Result<(), Error> { if size == 0 { return Err(Error::InvalidSize("The size must not be zero".to_string())); } self.state = CodecState::Skip; self.remain = size; Ok(()) } } fn utf8(buf: &[u8]) -> Result<&str, io::Error> { std::str::from_utf8(buf).map_err(|_| { io::Error::new( |
︙ | ︙ | |||
617 618 619 620 621 622 623 | } CodecState::Chunks => { if buf.is_empty() { // Need more data return Ok(None); } | | | | > > > | > > > < < < < < | < < | < < < < < | < < < | < < < < < < < < < < | < < | < < < < < < < < < < | | < < < | | < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | } CodecState::Chunks => { if buf.is_empty() { // Need more data return Ok(None); } let read_to = cmp::min(self.remain, buf.len() as u64); self.remain -= read_to; if self.remain == 0 { // When no more data is expected for this binary part, revert to // expecting Telegram lines self.state = CodecState::Telegram; } // 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. // // The `as usize` cast is safe to do, because read_to is guaranteed to // be within the bounds of an usize due to the `cmp::min()` above. Ok(Some(Input::Chunk( buf.split_to(read_to as usize).freeze(), self.remain ))) } CodecState::Bytes => { // This is guaranteed to work, because expect_bytes() takes in an // usize. let remain: usize = self.remain.try_into().unwrap(); if buf.len() < remain { Ok(None) } else { // Revert to expecting Telegram lines self.state = CodecState::Telegram; Ok(Some(Input::Bytes(buf.split_to(remain).freeze()))) } } 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 // output. let read_to = cmp::min(self.remain, buf.len() as u64); let _ = buf.split_to(read_to as usize); self.remain -= read_to; if self.remain != 0 { return Ok(None); // Need more data } // Revert to the default of expecting a telegram. self.state = CodecState::Telegram; Ok(Some(Input::SkipDone)) |
︙ | ︙ |
Changes to src/err.rs.
1 2 3 4 5 6 7 8 9 | //! Error types and error management functions. use std::fmt; use tokio::io; /// Error that `blather` can emit. #[derive(Debug, PartialEq)] pub enum Error { | < < < < < < < < < | > > > > > > > > > > > > > | < < < | > > > > | 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 | //! Error types and error management functions. use std::fmt; use tokio::io; /// Error that `blather` can emit. #[derive(Debug, PartialEq)] pub enum Error { /// The input format of a buffer was incorrect. BadFormat(String), /// Something occurred which was unexpected in the current state. BadState(String), /// The specified size is invalid, or invalid in a specific context. InvalidSize(String), /// A `std::io` or `tokio::io` error has occurred. IO(String), /// The requiested key was not found. KeyNotFound(String), /// Unable to serialize a buffer. SerializeError(String), /// A "protcol error" implies that the Framed decoder detected an error /// while parsing incoming data. Protocol(String) } impl std::error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::BadFormat(s) => write!(f, "Bad format; {}", s), Error::BadState(s) => { write!(f, "Encountred an unexpected/bad state: {}", s) } Error::InvalidSize(s) => write!(f, "Invalid size; {}", s), Error::IO(s) => write!(f, "I/O error; {}", s), Error::KeyNotFound(s) => write!(f, "Parameter '{}' not found", s), Error::Protocol(s) => write!(f, "Protocol error; {}", s), Error::SerializeError(s) => write!(f, "Unable to serialize; {}", s) } } } impl From<io::Error> for Error { fn from(err: io::Error) -> Self { Error::IO(err.to_string()) |
︙ | ︙ |
Changes to src/lib.rs.
︙ | ︙ | |||
44 45 46 47 48 49 50 | //! A set of "parameters", represented by the Params struct, is a set of //! key/value pairs. They look similar to `Telegrams` because the `Telegram`'s //! implement their key/value paris using a `Params` buffer. //! //! # Communication //! blather handles transmission using tokio-util's //! [`Framed`](tokio_util::codec::Framed) framework, by | | < | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | //! A set of "parameters", represented by the Params struct, is a set of //! key/value pairs. They look similar to `Telegrams` because the `Telegram`'s //! implement their key/value paris using a `Params` buffer. //! //! # Communication //! blather handles transmission using tokio-util's //! [`Framed`](tokio_util::codec::Framed) framework, by //! implementing its own [`Codec`]. It can be used to send and //! receive the various communication buffers supported by the crate. #![deny(missing_docs)] #![deny(rustdoc::missing_crate_level_docs)] pub mod codec; mod err; pub mod types; pub use codec::Codec; pub use err::Error; |
︙ | ︙ |
Changes to src/types/kvlines.rs.
1 2 | //! A key/value pair list with stable ordering and non-unique keys. | < | 1 2 3 4 5 6 7 8 9 | //! A key/value pair list with stable ordering and non-unique keys. use std::fmt; use bytes::{BufMut, BytesMut}; use crate::err::Error; /// Representation of a key/value pair in `KVLines`. |
︙ | ︙ |
Changes to src/types/params.rs.
1 2 3 4 5 | //! The `Params` buffer is a set of unorderded key/value pairs, with unique //! keys. It's similar to a `HashMap`, but has constraints on key names and //! offers conventions for value layouts, such as comma-separated values for //! lists. | > | < | | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //! The `Params` buffer is a set of unorderded key/value pairs, with unique //! keys. It's similar to a `HashMap`, but has constraints on key names and //! offers conventions for value layouts, such as comma-separated values for //! lists. use std::{ collections::{HashMap, HashSet}, fmt, str::FromStr }; use bytes::{BufMut, BytesMut}; use super::validators::validate_param_key; use crate::err::Error; |
︙ | ︙ | |||
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | /// Return the number of key/value pairs in the parameter buffer. pub fn len(&self) -> usize { self.hm.len() } /// Return reference to inner HashMap. pub fn get_inner(&self) -> &HashMap<String, String> { &self.hm } /// Add a parameter to the parameter. /// /// The `key` and `value` parameters are generic over the trait `ToString`, /// allowing a polymorphic behavior. /// /// # Examples /// ``` /// use blather::Params; | > > > > > > > | | | | < | 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 | /// Return the number of key/value pairs in the parameter buffer. pub fn len(&self) -> usize { self.hm.len() } /// Returns `true` if the `Params` collection does not contain any key/value /// pairs. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Return reference to inner HashMap. pub fn get_inner(&self) -> &HashMap<String, String> { &self.hm } /// Add a parameter to the parameter. /// /// The `key` and `value` parameters are generic over the trait `ToString`, /// allowing a polymorphic behavior. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_param("integer", 42).unwrap(); /// params.add_param("string", "hello").unwrap(); /// ``` pub fn add_param<T: ToString, U: ToString>( &mut self, key: T, value: U ) -> Result<(), Error> { let key = key.to_string(); |
︙ | ︙ | |||
92 93 94 95 96 97 98 | /// Add parameter where the value is generated from an iterator over /// strings, where entries are comma-separated. /// /// # Examples /// ``` /// use std::collections::HashSet; /// use blather::Params; | | | | | | | | | | | | < | 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 | /// Add parameter where the value is generated from an iterator over /// strings, where entries are comma-separated. /// /// # Examples /// ``` /// use std::collections::HashSet; /// use blather::Params; /// /// let mut params = Params::new(); /// /// params.add_strit("Cat", &["meow", "paws", "tail"]).unwrap(); /// assert_eq!(params.get_str("Cat"), Some("meow,paws,tail")); /// /// let v = vec!["meow", "paws", "tail"]; /// params.add_strit("CatToo", v.into_iter()).unwrap(); /// assert_eq!(params.get_str("CatToo"), Some("meow,paws,tail")); /// /// let mut hs = HashSet::new(); /// hs.insert("Elena"); /// hs.insert("Drake"); /// params.add_strit("Uncharted", hs.into_iter()).unwrap(); /// ``` pub fn add_strit<I, S>(&mut self, key: &str, c: I) -> Result<(), Error> where I: IntoIterator<Item = S>, S: AsRef<str> { let mut sv = Vec::new(); |
︙ | ︙ | |||
128 129 130 131 132 133 134 | /// Add a boolean parameter. /// /// # Examples /// ``` /// use blather::Params; | | | | | | | < | 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | /// Add a boolean parameter. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_bool("should_be_true", true).unwrap(); /// params.add_bool("should_be_false", false).unwrap(); /// assert_eq!(params.get_bool("should_be_true"), Ok(true)); /// assert_eq!(params.get_bool("should_be_false"), Ok(false)); /// ``` /// /// # Notes /// - Applications should not make assumptions about the specific string /// value added by this function. Do not treat boolean values as strings; /// use the [`get_bool()`](Self::get_bool) method instead. pub fn add_bool<K: ToString>( |
︙ | ︙ | |||
167 168 169 170 171 172 173 | /// Get a parameter and convert it to a requested type, fail if key isn't /// found. /// /// # Examples /// ``` /// use blather::{Params, Error}; | | | | | | | | < | 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /// Get a parameter and convert it to a requested type, fail if key isn't /// found. /// /// # Examples /// ``` /// use blather::{Params, Error}; /// /// let mut params = Params::new(); /// params.add_param("arthur", 42); /// let fourtytwo = params.get_param::<u32>("arthur").unwrap(); /// assert_eq!(fourtytwo, 42); /// let nonexist = params.get_param::<u32>("ford"); /// assert_eq!(nonexist, Err(Error::KeyNotFound("ford".to_string()))); /// ``` pub fn get_param<T: FromStr>(&self, key: &str) -> Result<T, Error> { if let Some(val) = self.get_str(key) { if let Ok(v) = T::from_str(val) { return Ok(v); } return Err(Error::BadFormat(format!( |
︙ | ︙ | |||
196 197 198 199 200 201 202 | /// Get a parameter and convert it to a requested type, return a default /// value if key isn't found. /// /// # Examples /// ``` /// use blather::Params; | | | | | < | 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | /// Get a parameter and convert it to a requested type, return a default /// value if key isn't found. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// let val = params.get_param_def::<u32>("nonexist", 11); /// assert_eq!(val, Ok(11)); /// ``` pub fn get_param_def<T: FromStr>( &self, key: &str, def: T ) -> Result<T, Error> { if let Some(val) = self.get_str(key) { |
︙ | ︙ | |||
238 239 240 241 242 243 244 | /// Get string representation of a value for a requested key. Returns a /// default value if key does not exist in parameter buffer. /// /// # Examples /// ``` /// use blather::Params; | | | | | < | | | | < | 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /// Get string representation of a value for a requested key. Returns a /// default value if key does not exist in parameter buffer. /// /// # Examples /// ``` /// use blather::Params; /// /// let params = Params::new(); /// let e = params.get_str_def("nonexist", "elena"); /// assert_eq!(e, "elena"); /// ``` // Lifetimes of self and def don't really go hand-in-hand, but we bound them // together for the sake of the return value's lifetime. pub fn get_str_def<'a>(&'a self, key: &str, def: &'a str) -> &'a str { let kv = self.hm.get_key_value(key); if let Some((_k, v)) = kv { v } else { def } } /// Get a parameter and convert it to an integer type. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_param("Num", 7); /// assert_eq!(params.get_int::<usize>("Num").unwrap(), 7); /// ``` /// /// # Notes /// - This method exists primarily to achive some sort of parity with a /// corresponding C++ library. It is recommended that applications use /// [`Params::get_param()`](Self::get_param) instead. // This method should really have some integer trait bound, but it doesn't |
︙ | ︙ | |||
294 295 296 297 298 299 300 | /// Try to get the value of a key and interpret it as an integer. If the key /// does not exist then return a default value supplied by the caller. /// /// # Examples /// ``` /// use blather::Params; | | | | | | < | 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /// Try to get the value of a key and interpret it as an integer. If the key /// does not exist then return a default value supplied by the caller. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_param("num", 11); /// assert_eq!(params.get_int_def::<u32>("num", 5).unwrap(), 11); /// assert_eq!(params.get_int_def::<u32>("nonexistent", 17).unwrap(), 17); /// ``` /// /// # Notes /// - It is recommended that application use /// [`Params::get_param_def()`](Self::get_param_def) instead. pub fn get_int_def<T: FromStr>( &self, |
︙ | ︙ | |||
361 362 363 364 365 366 367 | /// Parse the value of a key as a comma-separated list of strings and return /// it. Only non-empty entries are returned. /// /// # Examples /// ``` /// use blather::Params; | | | | | | < | | | | | | | | | < | | 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 | /// Parse the value of a key as a comma-separated list of strings and return /// it. Only non-empty entries are returned. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_param("csv", "elena,chloe,drake"); /// let sv = params.get_strvec("csv").unwrap(); /// assert_eq!(sv, vec!["elena", "chloe", "drake"]); /// ``` pub fn get_strvec(&self, key: &str) -> Result<Vec<String>, Error> { let mut ret = Vec::new(); if let Some(v) = self.get_str(key) { let split = v.split(','); for s in split { if !s.is_empty() { ret.push(s.to_string()); } } } Ok(ret) } /// Parse the value of a key as a comma-separated list of uniqie strings and /// return them in a HashSet. Only non-empty entries are returned. /// /// # Examples /// ``` /// use blather::Params; /// /// let mut params = Params::new(); /// params.add_param("set", "elena,chloe"); /// let set = params.get_hashset("set").unwrap(); /// assert_eq!(set.len(), 2); /// assert_eq!(set.contains("elena"), true); /// assert_eq!(set.contains("chloe"), true); /// assert_eq!(set.contains("drake"), false); /// ``` pub fn get_hashset(&self, key: &str) -> Result<HashSet<String>, Error> { let mut ret = HashSet::new(); if let Some(v) = self.get_str(key) { let split = v.split(','); for s in split { if !s.is_empty() { ret.insert(s.to_string()); } } } Ok(ret) } |
︙ | ︙ |
Changes to src/types/telegram.rs.
1 2 3 4 | //! Telegrams are objects that contain a _topic_ and a set of zero or more //! parameters. They can be serialized into a line-based format for //! transmission over a network link. | > | | | > < | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //! Telegrams are objects that contain a _topic_ and a set of zero or more //! parameters. They can be serialized into a line-based format for //! transmission over a network link. use std::{ collections::{HashMap, HashSet}, fmt, str::FromStr }; use bytes::{BufMut, BytesMut}; use crate::err::Error; use super::{params::Params, validators::validate_topic}; /// Representation of a Telegram; a buffer which contains a _topic_ and a set /// of key/value parameters. /// /// Internally the key/value parameters are represented by a [`Params`] /// structure. #[derive(Debug, Clone, Default)] |
︙ | ︙ | |||
38 39 40 41 42 43 44 | } /// Create a new telegram object with a topic. /// /// ``` /// use blather::Telegram; | | | | < | | | | | | < | | | | | < | | | | | | | < | | | | | | < | | | | | < | | | | < | 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 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 179 180 181 182 183 184 185 186 | } /// Create a new telegram object with a topic. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new_topic("Hello").unwrap(); /// assert_eq!(tg.get_topic(), Some("Hello")); /// ``` pub fn new_topic(topic: &str) -> Result<Self, Error> { validate_topic(topic)?; Ok(Telegram { topic: Some(topic.to_string()), ..Default::default() }) } /// Clear topic and internal parameters buffer. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_param("cat", "meow"); /// assert_eq!(tg.num_params(), 1); /// tg.clear(); /// assert_eq!(tg.num_params(), 0); /// ``` pub fn clear(&mut self) { self.topic = None; self.params.clear(); } /// Return the number of key/value parameters in the Telegram object. /// /// # Examples /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// assert_eq!(tg.num_params(), 0); /// tg.add_param("cat", "meow"); /// assert_eq!(tg.num_params(), 1); /// ``` /// /// # Notes /// This is a wrapper around [`Params::len()`](crate::Params::len). pub fn num_params(&self) -> usize { self.params.len() } /// Get a reference to the internal parameters object. pub fn get_params(&self) -> &Params { &self.params } /// Get a mutable reference to the inner [`Params`] object. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_param("cat", "meow"); /// assert_eq!(tg.num_params(), 1); /// tg.get_params_mut().clear(); /// assert_eq!(tg.num_params(), 0); /// ``` pub fn get_params_mut(&mut self) -> &mut Params { &mut self.params } /// Get a reference the the parameter's internal HashMap. /// /// Note: The inner representation of the Params object may change in the /// future. pub fn get_params_inner(&self) -> &HashMap<String, String> { self.params.get_inner() } /// Set topic for telegram. /// /// Overwrites current topic is one has already been set. /// /// # Examples /// ``` /// use blather::{Telegram, Error}; /// /// let mut tg = Telegram::new(); /// assert_eq!(tg.set_topic("Hello"), Ok(())); /// /// let e = Error::BadFormat("Invalid topic character".to_string()); /// assert_eq!(tg.set_topic("Hell o"), Err(e)); /// ``` pub fn set_topic(&mut self, topic: &str) -> Result<(), Error> { validate_topic(topic)?; self.topic = Some(topic.to_string()); Ok(()) } /// Get a reference to the topic string, or None if topic is not been set. /// /// # Examples /// ``` /// use blather::{Telegram, Error}; /// /// let tg = Telegram::new_topic("shoe0nhead").unwrap(); /// assert_eq!(tg.get_topic(), Some("shoe0nhead")); /// /// let tg = Telegram::new(); /// assert_eq!(tg.get_topic(), None); /// ``` pub fn get_topic(&self) -> Option<&str> { if let Some(t) = &self.topic { Some(t) } else { None } } /// Add a parameter to the telegram. /// /// The `key` and `value` parameters are generic over the trait `ToString`, /// allowing a polymorphic behavior. /// /// # Examples /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_param("integer", 42).unwrap(); /// tg.add_param("string", "hello").unwrap(); /// ``` /// /// # Notes /// - This is a thin wrapper around /// [`Params::add_param()`](crate::Params::add_param). pub fn add_param<T: ToString, U: ToString>( &mut self, |
︙ | ︙ | |||
209 210 211 212 213 214 215 | /// Add parameter where the value is generated from an iterator over a /// string container, where entries will be comma-separated. /// /// ``` /// use blather::Telegram; | | | | | < | 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | /// Add parameter where the value is generated from an iterator over a /// string container, where entries will be comma-separated. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_strit("Cat", &["meow", "paws", "tail"]).unwrap(); /// assert_eq!(tg.get_str("Cat"), Some("meow,paws,tail")); /// ``` /// /// # Notes /// - This is a thin wrapper for /// [`Params::add_strit()`](crate::Params::add_strit). pub fn add_strit<I, S>(&mut self, key: &str, c: I) -> Result<(), Error> where |
︙ | ︙ | |||
301 302 303 304 305 306 307 | } /// Get an integer representation of a parameter. /// /// ``` /// use blather::Telegram; | | | | | < | | | | | < | 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | } /// Get an integer representation of a parameter. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_param("Num", 7); /// assert_eq!(tg.get_int::<usize>("Num").unwrap(), 7); /// ``` /// /// # Notes /// - This function uses the `FromStr` trait on the return-type so it /// technically isn't limited to integers. /// - The method exists to mimic a C++ library. It is recommeded that /// applications use [`Telegram::get_param()`](Self::get_param) instead. pub fn get_int<T: FromStr>(&self, key: &str) -> Result<T, Error> { self.params.get_int(key) } /// Try to get the parameter value of a key and interpret it as an integer. /// If the key does not exist then return a default value supplied by the /// caller. /// /// ``` /// use blather::Telegram; /// /// let mut tg = Telegram::new(); /// tg.add_param("num", 11); /// assert_eq!(tg.get_int_def::<u32>("num", 5).unwrap(), 11); /// assert_eq!(tg.get_int_def::<u32>("nonexistent", 17).unwrap(), 17); /// ``` pub fn get_int_def<T: FromStr>( &self, key: &str, def: T ) -> Result<T, Error> { self.params.get_int_def(key, def) |
︙ | ︙ | |||
505 506 507 508 509 510 511 | } } impl fmt::Display for Telegram { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let topic: &str = match &self.topic { Some(s) => s.as_ref(), | | | 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | } } impl fmt::Display for Telegram { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let topic: &str = match &self.topic { Some(s) => s.as_ref(), None => "<None>" }; write!(f, "{}:{}", topic, self.params) } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : |
Changes to tests/conn_telegram.rs.
︙ | ︙ | |||
63 64 65 66 67 68 69 | // space isn't allowed in topic mock.read(b"hel lo\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); if let Some(e) = frm.next().await { if let Err(e) = e { match e { | | | | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | // space isn't allowed in topic mock.read(b"hel lo\n\n"); let mut frm = Framed::new(mock.build(), Codec::new()); if let Some(e) = frm.next().await { if let Err(e) = e { match e { Error::Protocol(s) => { assert_eq!(s, "Bad format; Invalid topic character"); } _ => { panic!("Wrong error"); } } } else { panic!("Unexpected success"); |
︙ | ︙ |
Changes to tests/params.rs.
︙ | ︙ | |||
13 14 15 16 17 18 19 | #[test] fn exists() { let mut params = Params::new(); params.add_str("foo", "bar").unwrap(); | | | | | 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 | #[test] fn exists() { let mut params = Params::new(); params.add_str("foo", "bar").unwrap(); assert!(params.have("foo")); assert!(!params.have("nonexistent")); } #[test] fn integer() { let mut msg = Params::new(); msg.add_str("Num", "64").unwrap(); assert_eq!(msg.get_int::<u16>("Num").unwrap(), 64); } #[test] fn size() { let mut msg = Params::new(); msg.add_param("Num", 7_usize).unwrap(); assert_eq!(msg.get_int::<usize>("Num").unwrap(), 7); } #[test] fn intoparams() { let mut msg = Params::new(); |
︙ | ︙ |
Changes to tests/params_strvec.rs.
︙ | ︙ | |||
35 36 37 38 39 40 41 | } #[test] fn strvec_single_add() { let mut params = Params::new(); | | < | < < | 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 | } #[test] fn strvec_single_add() { let mut params = Params::new(); let sv = vec!["foo"]; params.add_strit("hello", &sv).unwrap(); //let v = params.get_str("hello").unwrap(); assert_eq!(params.get_str("hello"), Some("foo")); let sv = params.get_strvec("hello").unwrap(); assert_eq!(sv.len(), 1); assert_eq!(sv[0], "foo"); } #[test] fn strvec_two_add() { let mut params = Params::new(); let sv = vec!["foo", "bar"]; params.add_strit("hello", &sv).unwrap(); assert_eq!(params.get_str("hello"), Some("foo,bar")); let sv = params.get_strvec("hello").unwrap(); assert_eq!(sv.len(), 2); assert_eq!(sv[0], "foo"); |
︙ | ︙ |
Changes to tests/telegram.rs.
︙ | ︙ | |||
15 16 17 18 19 20 21 | #[test] fn exist() { let mut tg = Telegram::new(); tg.add_str("foo", "bar").unwrap(); | | | | | 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 | #[test] fn exist() { let mut tg = Telegram::new(); tg.add_str("foo", "bar").unwrap(); assert!(tg.have_param("foo")); assert!(!tg.have_param("nonexistent")); } #[test] fn integer() { let mut msg = Telegram::new(); msg.set_topic("SomeTopic").unwrap(); assert_eq!(msg.get_topic().unwrap(), "SomeTopic"); msg.add_str("Num", "64").unwrap(); assert_eq!(msg.get_int::<u16>("Num").unwrap(), 64); } #[test] fn size() { let mut msg = Telegram::new(); msg.add_param("Num", 7_usize).unwrap(); assert_eq!(msg.get_int::<usize>("Num").unwrap(), 7); } #[test] fn intoparams() { let mut msg = Telegram::new(); |
︙ | ︙ |
Changes to tests/tg_to_expect.rs.
︙ | ︙ | |||
10 11 12 13 14 15 16 | 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()); | | > > | > | | | | < | | < | | 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 | 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 codec::Input::Telegram(tg) = o { assert_eq!(tg.get_topic(), Some("hello")); assert_eq!(tg.get_int::<usize>("len").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 codec::Input::Bytes(_bm) = o { } else { panic!("Not a Buf"); } } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : |
Added www/changelog.md.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | # Change Log ⚠️ indicates a breaking change. ## [Unreleased] [Details](/vdiff?from=blather-0.10.0&to=trunk) ### Added ### Changed ### Removed --- ## [0.10.0] - 2024-02-23 [Details](/vdiff?from=blather-0.9.0&to=blather-0.10.0) ### Changed - Dependency maintenance. - ⚠️ Changed `expect_chunks()` to accept length as `u64` rather than `usize` to support blobs on 32-bit platforms. - ⚠️ Make `skip()` take in `u64` rather than `usize` to be able to skip very large blobs. - ⚠️ Use `Bytes` rather than `BytesMut` for chunk buffer. - ⚠️ The decoder will wrap any errors relating to parsing incoming data in `Error::Protocol()` to signal that the incoming protocol fomrat was invalid. - ⚠️ Make `expect_chunks()` fallible and check for zero-length. ### Removed - ⚠️ Removed three codec states: - `BytesMut` has turned out not to be particularly useful. - `File` was always a bad idea. - `Writer` can have valid use-cases, but risk being misused as a substitute for `File`. Use `Bytes` instead of `BytesMut`. Use `Chunks` instead of `File` or `Writer`, and let the application write the chunks as appropriate. |
Added www/index.md.
> > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 | # blather A talkative, somwhat reminiscent to HTTP, line-based protocol, implemented as a tokio-util Codec. ## Change log The details of changes can always be found in the timeline, but for a high-level view of changes between released versions there's a manually maintained [Change Log](./changelog.md). |