stern4rust/
header_source.rs1use std::fs;
6use std::path::Path;
7
8use anyhow::Context;
9use anyhow::Result;
10
11pub struct HeaderSource;
19
20impl HeaderSource {
21 pub fn read(path: &Path) -> Result<Vec<String>> {
22 let contents = fs::read_to_string(path)
23 .with_context(|| format!("failed to read header file {}", path.display()))?;
24 Ok(Self::parse(&contents))
25 }
26
27 pub fn parse(contents: &str) -> Vec<String> {
28 let contents = contents.strip_prefix('\u{feff}').unwrap_or(contents);
29 let mut lines: Vec<String> = contents
30 .split('\n')
31 .map(|line| line.strip_suffix('\r').unwrap_or(line).to_string())
32 .collect();
33 while lines.last().is_some_and(|line| line.trim().is_empty()) {
34 lines.pop();
35 }
36 lines
37 }
38}