Skip to main content

libaprs_engine/
transport.rs

1/// Line-oriented packet source for file/stdin style transports.
2#[derive(Clone, Debug, Eq, PartialEq)]
3pub struct LineTransport<'a> {
4    input: &'a [u8],
5}
6
7impl<'a> LineTransport<'a> {
8    /// Creates a transport over newline-separated packet bytes.
9    #[must_use]
10    pub fn new(input: &'a [u8]) -> Self {
11        Self { input }
12    }
13
14    /// Iterates packet lines without trailing CR/LF bytes.
15    #[must_use]
16    pub fn packets(&self) -> Vec<&'a [u8]> {
17        self.input
18            .split(|byte| *byte == b'\n')
19            .map(trim_trailing_carriage_return)
20            .filter(|line| !line.is_empty())
21            .collect()
22    }
23}
24
25fn trim_trailing_carriage_return(line: &[u8]) -> &[u8] {
26    line.strip_suffix(b"\r").unwrap_or(line)
27}