Skip to content

Commit 4197b9b

Browse files
authored
fix(provider): handle UTF-8 BOM in JSON and CSV parsing (#24)
1 parent f504f05 commit 4197b9b

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/provider.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn load_signatures(input: &str) -> Result<Vec<Signature>> {
2525
}
2626

2727
pub fn parse_signatures(content: &str) -> Result<Vec<Signature>> {
28+
let content = content.strip_prefix(BOM).unwrap_or(content);
2829
let format = detect_format(content)?;
2930
let inputs = match format {
3031
Format::Json => parse_json(content)?,
@@ -135,6 +136,18 @@ mod tests {
135136
assert_eq!(detect_format(csv).unwrap(), Format::Csv);
136137
}
137138

139+
#[test]
140+
fn test_auto_detect_json_with_bom() {
141+
let json = "\u{FEFF}[{\"r\":\"1\",\"s\":\"2\",\"z\":\"3\"}]";
142+
assert_eq!(detect_format(json).unwrap(), Format::Json);
143+
}
144+
145+
#[test]
146+
fn test_auto_detect_csv_with_bom() {
147+
let csv = "\u{FEFF}r,s,z\n1,2,3";
148+
assert_eq!(detect_format(csv).unwrap(), Format::Csv);
149+
}
150+
138151
#[test]
139152
fn test_invalid_json_error() {
140153
let result = parse_signatures("not json");
@@ -212,4 +225,18 @@ mod tests {
212225
let sigs = parse_signatures(json).unwrap();
213226
assert_eq!(sigs.len(), 1);
214227
}
228+
229+
#[test]
230+
fn test_parse_json_with_bom() {
231+
let json = "\u{FEFF}[{\"r\":\"123\",\"s\":\"456\",\"z\":\"789\"}]";
232+
let sigs = parse_signatures(json).unwrap();
233+
assert_eq!(sigs.len(), 1);
234+
}
235+
236+
#[test]
237+
fn test_parse_csv_with_bom() {
238+
let csv = "\u{FEFF}r,s,z,pubkey\n123,456,789,\n";
239+
let sigs = parse_signatures(csv).unwrap();
240+
assert_eq!(sigs.len(), 1);
241+
}
215242
}

0 commit comments

Comments
 (0)