Added ressponse errors.
All checks were successful
MoreThanText/morethantext/pipeline/head This commit looks good

This commit is contained in:
Jeff Baskin 2025-02-14 22:14:48 -05:00
parent 7d2f79d736
commit c511b623fd

View File

@ -135,6 +135,12 @@ mod records {
}
}
#[derive(Debug)]
enum ResponseError {
ColumnNumberMisMatch,
MissionColumn(String),
}
struct Response {
data: HashMap<String, Vec<Field>>,
counter: usize,
@ -148,7 +154,7 @@ impl Response {
}
}
fn add(&mut self, rec: Record) -> Result<(), String> {
fn add(&mut self, rec: Record) -> Result<(), ResponseError> {
if self.data.is_empty() {
for (key, value) in rec.iter() {
let mut store = Vec::new();
@ -157,12 +163,12 @@ impl Response {
}
} else {
if rec.len() != self.data.len() {
return Err("incorrect number of columns".to_string());
return Err(ResponseError::ColumnNumberMisMatch);
}
for (key, value) in rec.iter() {
match self.data.get_mut(key) {
Some(data) => data.push(value.clone()),
None => return Err("bad".to_string()),
None => return Err(ResponseError::MissionColumn(key.to_string())),
}
}
}
@ -244,7 +250,10 @@ mod responses {
res.add(rec1).unwrap();
match res.add(rec2) {
Ok(_) => unreachable!("Should not accept additional value"),
Err(_) => {}
Err(err) => match err {
ResponseError::ColumnNumberMisMatch => {}
_ => unreachable!("should havee been a mismatch error"),
},
}
}
@ -259,7 +268,27 @@ mod responses {
res.add(rec2).unwrap();
match res.add(rec1) {
Ok(_) => unreachable!("Should not accept additional value"),
Err(_) => {}
Err(err) => match err {
ResponseError::ColumnNumberMisMatch => {}
_ => unreachable!("should havee been a mismatch error"),
},
}
}
#[test]
fn wrong_column_name() {
let mut res = Response::new();
let mut rec1 = Record::new();
let mut rec2 = Record::new();
rec1.add("one", "one");
rec2.add("two", "two");
res.add(rec1).unwrap();
match res.add(rec2) {
Ok(_) => unreachable!("Should not accept additional value"),
Err(err) => match err {
ResponseError::MissionColumn(txt) => assert_eq!(txt, "two"),
_ => unreachable!("should have been missing cloumn"),
},
}
}
}