use std::io::BufRead;
use crate::format::UTF8_BOM_CHAR;
use crate::import::{ImportError, ImportLimits, ImportOrigin, Importer, TargetSink};
const COMMENT: char = '#';
#[derive(Debug, Clone, Copy)]
pub struct ListImporter {
limits: ImportLimits,
}
impl ListImporter {
pub fn new(limits: ImportLimits) -> Self {
Self { limits }
}
}
impl Default for ListImporter {
fn default() -> Self {
Self::new(ImportLimits::default())
}
}
impl Importer for ListImporter {
fn import(
&self,
input: &mut dyn BufRead,
sink: &mut dyn TargetSink,
) -> Result<(), ImportError> {
let mut buffer = Vec::new();
let mut line_number = 0u64;
let mut first_line = true;
loop {
buffer.clear();
line_number += 1;
let origin = ImportOrigin::line(line_number);
if !read_line(input, &mut buffer, self.limits.max_line_bytes, origin)? {
return Ok(());
}
let text =
std::str::from_utf8(&buffer).map_err(|_| ImportError::InvalidUtf8 { origin })?;
let text = if first_line {
first_line = false;
text.strip_prefix(UTF8_BOM_CHAR).unwrap_or(text)
} else {
text
};
let content = match text.split_once(COMMENT) {
Some((before, _)) => before,
None => text,
};
for token in content.split_whitespace() {
sink.accept(token, origin)?;
}
}
}
}
pub(crate) fn read_line(
input: &mut dyn BufRead,
buffer: &mut Vec<u8>,
max_line_bytes: usize,
origin: ImportOrigin,
) -> Result<bool, ImportError> {
let ceiling = (max_line_bytes as u64).saturating_add(2);
let mut bounded = std::io::Read::take(&mut *input, ceiling);
let read = bounded.read_until(b'\n', buffer)?;
if read == 0 {
return Ok(false);
}
if buffer.last() == Some(&b'\n') {
buffer.pop();
if buffer.last() == Some(&b'\r') {
buffer.pop();
}
} else if read as u64 == ceiling {
return Err(ImportError::LineTooLong {
origin,
limit: max_line_bytes,
});
}
if buffer.len() > max_line_bytes {
return Err(ImportError::LineTooLong {
origin,
limit: max_line_bytes,
});
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::import::{ImportFormat, ImportOptions, Imported};
use crate::model::port::PortSet;
use std::io::Cursor;
fn read(input: &str) -> Imported {
read_with(input, &ImportOptions::new(PortSet::try_from("80").unwrap()))
}
fn read_with(input: &str, options: &ImportOptions<'_>) -> Imported {
ImportFormat::List
.read(&mut Cursor::new(input), options)
.expect("the list imports")
}
#[test]
fn the_shapes_a_hand_written_list_arrives_in_are_all_read_the_same() {
let file = concat!(
"\u{feff}# staging, 2026-02\r\n",
"\r\n",
" 192.0.2.1 \r\n",
"198.51.100.1 198.51.100.2\t198.51.100.3\n",
" # a whole line of comment\n",
"198.51.100.4 # and a trailing one\n",
"\n",
"198.51.100.5",
);
let imported = read(file);
assert_eq!(imported.tokens, 6);
assert_eq!(imported.addresses, 6);
}
#[test]
fn a_byte_order_mark_does_not_cost_the_first_target() {
assert_eq!(read("\u{feff}198.51.100.1\n198.51.100.2\n").addresses, 2);
}
#[test]
fn a_comma_stays_inside_the_expression_it_was_written_in() {
let imported = read("198.51.100.1:80,443\n198.51.100.2,198.51.100.3:22\n");
assert_eq!(imported.tokens, 2);
assert_eq!(imported.addresses, 3);
assert_eq!(imported.map.units.len(), 2, "80 with 443, and 22");
}
#[test]
fn a_last_line_without_a_terminator_is_still_a_target() {
assert_eq!(read("198.51.100.1\n198.51.100.2").addresses, 2);
assert_eq!(read("198.51.100.1").addresses, 1);
}
#[test]
fn an_empty_input_produces_no_targets_rather_than_an_error() {
let imported = read("");
assert_eq!(imported.tokens, 0);
assert!(imported.map.is_empty());
assert_eq!(read("\n\n \n# only a comment\n").tokens, 0);
}
#[test]
fn a_line_past_the_limit_is_refused_without_being_read_whole() {
let options =
ImportOptions::new(PortSet::try_from("80").unwrap()).with_limits(ImportLimits {
max_line_bytes: 32,
..ImportLimits::default()
});
let long = "10.0.0.1 ".repeat(1024);
let err = ImportFormat::List
.read(&mut Cursor::new(long), &options)
.expect_err("a line of nine thousand bytes is not a target list");
assert!(matches!(err, ImportError::LineTooLong { limit: 32, .. }));
}
#[test]
fn the_line_limit_counts_content_and_not_the_terminator() {
let options =
ImportOptions::new(PortSet::try_from("80").unwrap()).with_limits(ImportLimits {
max_line_bytes: 8,
..ImportLimits::default()
});
for file in ["10.0.0.1\r\n", "10.0.0.1\n", "10.0.0.1"] {
assert_eq!(read_with(file, &options).addresses, 1, "{file:?}");
}
for file in ["10.0.0.12\r\n", "10.0.0.12\n", "10.0.0.12"] {
let err = ImportFormat::List
.read(&mut Cursor::new(file), &options)
.expect_err("nine bytes is past an eight byte limit");
assert!(
matches!(err, ImportError::LineTooLong { limit: 8, .. }),
"{file:?} gave {err:?}"
);
}
}
#[test]
fn invalid_utf8_is_refused_rather_than_replaced() {
let mut input = Cursor::new(b"198.51.100.1\n198.51.100.\xff\xfe2\n".to_vec());
let options = ImportOptions::new(PortSet::try_from("80").unwrap());
let err = ImportFormat::List
.read(&mut input, &options)
.expect_err("the second line is not text");
assert!(matches!(
err,
ImportError::InvalidUtf8 {
origin: ImportOrigin { line: Some(2), .. }
}
));
}
#[test]
fn a_comment_runs_to_the_end_of_its_line_and_no_further() {
let imported = read("198.51.100.1 # 198.51.100.99 198.51.100.98\n198.51.100.2\n");
assert_eq!(imported.tokens, 2);
assert_eq!(imported.addresses, 2);
}
}