Skip to content

Commit 032957f

Browse files
committed
docs(echo): added docs
Signed-off-by: Jad K. Haddad <jadkhaddad@gmail.com>
1 parent 22c7e89 commit 032957f

4 files changed

Lines changed: 54 additions & 4 deletions

File tree

framez/src/framed.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,50 @@ impl<'buf, C, RW> Framed<'buf, C, RW> {
125125
self.core.maybe_next().await
126126
}
127127

128-
/// TODO
128+
/// Tries to read a frame from the underlying reader and echoes it back to the writer if the `echo` function returns `Echo::Echo`.
129+
///
130+
/// # Return value
131+
///
132+
/// - `Some(Ok(None))` if the buffer is not framable or the frame was echoed. Call `maybe_next_echoed` again to read more bytes.
133+
/// - `Some(Ok(Some(frame)))` if a frame was successfully decoded and not echoed. Call `maybe_next_echoed` again to read more bytes.
134+
/// - `Some(Err(error))` if an error occurred. The caller should stop reading.
135+
/// - `None` if eof was reached. The caller should stop reading.
136+
///
137+
/// # Usage
138+
///
139+
/// See [`next!`](crate::next_echoed!).
140+
///
141+
/// # Example
142+
///
143+
/// Echo back [`str`] frames starting with a dot (`.`).
144+
///
145+
/// ```rust
146+
/// use core::{error::Error};
147+
///
148+
/// use framez::{Echo, Framed, codec::lines::StrLines, mock::Noop, next_echoed};
149+
///
150+
/// async fn read() -> Result<(), Box<dyn Error>> {
151+
/// let r_buf = &mut [0u8; 1024];
152+
/// let w_buf = &mut [0u8; 1024];
153+
///
154+
/// let mut framed = Framed::new(StrLines::new(), Noop, r_buf, w_buf);
155+
///
156+
/// while let Some(item) = next_echoed!(framed, |item| {
157+
/// if item.starts_with('.') {
158+
/// return Echo::Echo(item);
159+
/// }
160+
///
161+
/// Echo::NoEcho(item)
162+
/// })
163+
/// .transpose()? {
164+
/// assert!(!item.starts_with('.'));
165+
///
166+
/// println!("Frame: {}", item);
167+
/// }
168+
///
169+
/// Ok(())
170+
/// }
171+
/// ```
129172
pub async fn maybe_next_echoed<'this, F>(
130173
&'this mut self,
131174
echo: F,

framez/src/framed_core.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ impl<'buf, C, RW> FramedCore<'buf, C, RW> {
211211
}
212212
}
213213

214-
/// TODO
214+
/// Wether to echo the item back to the writer or not.
215+
///
216+
/// See [`Framed::maybe_next_echoed`](crate::Framed::maybe_next_echoed).
215217
#[derive(Debug)]
216218
pub enum Echo<T> {
217219
/// Echo the item back to the writer.

framez/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ mod framed;
3030
pub use framed::{Framed, FramedRead, FramedWrite};
3131

3232
mod framed_core;
33-
// TODO
3433
pub use framed_core::Echo;
3534
use framed_core::FramedCore;
3635

framez/src/next.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ macro_rules! next {
1919
}};
2020
}
2121

22-
/// TODO
22+
/// Calls [`Framed::maybe_next_echoed`](crate::Framed::maybe_next_echoed) in a loop until a frame is returned or an error occurs.
23+
///
24+
/// # Return value
25+
///
26+
/// - `Some(Ok(frame))` if a frame was successfully decoded or echoed. Call `next_echoed` again to read more frames.
27+
/// - `Some(Err(error))` if an error occurred. The caller should stop reading
28+
/// - `None` if eof was reached. The caller should stop reading.
2329
#[macro_export]
2430
macro_rules! next_echoed {
2531
($framed:ident, $f:expr) => {{

0 commit comments

Comments
 (0)