#[must_use]
pub fn next_event_boundary(buf: &[u8]) -> Option<(usize, usize)> {
let crlf = buf.windows(4).position(|w| w == b"\r\n\r\n");
let lf = buf.windows(2).position(|w| w == b"\n\n");
match (crlf, lf) {
(Some(c), Some(l)) if c <= l => Some((c, 4)),
(_, Some(l)) => Some((l, 2)),
(Some(c), None) => Some((c, 4)),
(None, None) => None,
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::pedantic, clippy::nursery, missing_docs)]
use super::next_event_boundary;
#[test]
fn finds_lf_boundary() {
assert_eq!(next_event_boundary(b"data: x\n\nmore"), Some((7, 2)));
}
#[test]
fn finds_crlf_boundary() {
assert_eq!(next_event_boundary(b"data: x\r\n\r\nmore"), Some((7, 4)));
}
#[test]
fn returns_none_when_no_separator() {
assert_eq!(next_event_boundary(b"data: partial"), None);
}
#[test]
fn returns_the_earliest_of_mixed_delimiters() {
assert_eq!(next_event_boundary(b"a\n\nb\r\n\r\n"), Some((1, 2)));
}
}