use crate::at::parse::{AtCompletion, ExchangeMatch};
use crate::events::ExchangeCompletionMode;
use crate::exchange::options::{default_terminators, matches_terminators, ResolvedExchangeOptions};
#[derive(Debug)]
pub(crate) enum Completion {
At(AtCompletion),
Substring {
terminators: Vec<Vec<u8>>,
longest: usize,
},
}
impl Completion {
pub(crate) fn new(options: &ResolvedExchangeOptions) -> Self {
match options.completion_mode {
ExchangeCompletionMode::AtFinalLine => {
Completion::At(AtCompletion::new(false, options.result_format))
}
ExchangeCompletionMode::AtIntermediate => {
Completion::At(AtCompletion::new(true, options.result_format))
}
ExchangeCompletionMode::Substring => Completion::Substring {
longest: options.terminators.iter().map(Vec::len).max().unwrap_or(0),
terminators: options.terminators.clone(),
},
}
}
pub(crate) fn push(&mut self, buf: &[u8], new: usize) -> Option<ExchangeMatch> {
let new = new.min(buf.len());
match self {
Completion::At(c) => c.push(&buf[buf.len() - new..]),
Completion::Substring {
terminators,
longest,
} => {
let from = (buf.len() - new).saturating_sub(longest.saturating_sub(1));
matches_terminators(&buf[from..], terminators).then(|| ExchangeMatch::Substring {
term: String::from_utf8_lossy(default_terminators()[0].as_slice()).into_owned(),
})
}
}
}
}
#[cfg(test)]
pub(crate) fn check_exchange_complete(
buf: &[u8],
options: &ResolvedExchangeOptions,
) -> Option<ExchangeMatch> {
Completion::new(options).push(buf, buf.len())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::events::{AtResultFormat, RxPrepareMode};
fn options(mode: ExchangeCompletionMode, terminators: Vec<Vec<u8>>) -> ResolvedExchangeOptions {
ResolvedExchangeOptions {
timeout_ms: 1000,
max_bytes: 4096,
terminators,
idle_ms: None,
rx_prepare: RxPrepareMode::None,
drain_idle_ms: 50,
drain_max_ms: 200,
completion_mode: mode,
result_format: AtResultFormat::Verbose,
command: Some("AT".into()),
solicited_prefixes: vec![],
}
}
fn byte_by_byte(c: &mut Completion, reply: &[u8]) -> Vec<Option<ExchangeMatch>> {
(1..=reply.len()).map(|n| c.push(&reply[..n], 1)).collect()
}
#[test]
fn at_completion_sees_each_new_byte() {
let mut c = Completion::new(&options(ExchangeCompletionMode::AtFinalLine, vec![]));
let seen = byte_by_byte(&mut c, b"AT\r\r\nOK\r\n");
assert!(
seen[..seen.len() - 1].iter().all(Option::is_none),
"{seen:?}"
);
assert!(
matches!(seen.last(), Some(Some(ExchangeMatch::Ok))),
"{seen:?}"
);
}
#[test]
fn substring_window_covers_a_terminator_ending_in_the_new_bytes() {
let opts = options(ExchangeCompletionMode::Substring, vec![b"OK\r\n".to_vec()]);
let mut c = Completion::new(&opts);
let seen = byte_by_byte(&mut c, b"OK\r\n");
assert!(seen[..3].iter().all(Option::is_none), "{seen:?}");
assert!(seen[3].is_some(), "{seen:?}");
let mut c = Completion::new(&opts);
assert!(c.push(b"OK\r\nzz", 2).is_none());
assert!(c.push(b"zzOK\r\n", 6).is_some());
}
}