fix: don't panic on missing data
This commit is contained in:
1 parent
4f61222f2d
commit
97b8305068
1 file changed
+10
-11
@@ -2,7 +2,8 @@ use super::{
|
|||||||
Comparison, MapListComparison, NestedListComparison, NumberComparison, NumberListComparison,
|
Comparison, MapListComparison, NestedListComparison, NumberComparison, NumberListComparison,
|
||||||
TextComparison, TextListComparison,
|
TextComparison, TextListComparison,
|
||||||
};
|
};
|
||||||
use crate::common::{ExpandableInfo, ValueType};
|
use crate::common::ValueType;
|
||||||
|
use std::collections::HashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
@@ -42,15 +43,13 @@ impl ComparisonData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compare(&self, student_data: &ExpandableInfo) -> Result<bool, String> {
|
pub fn compare(&self, student_data: &HashMap<String, ValueType>) -> Result<bool, String> {
|
||||||
let value = student_data.data.get(&self.member).expect(
|
student_data.get(&self.member)
|
||||||
format!(
|
.ok_or_else(||
|
||||||
"Couldn't find member {:?}, current student data: {:?}",
|
format!("Couldn't find member {:?}, current student data: {:?}", self.member, student_data)
|
||||||
self.member, student_data
|
|
||||||
)
|
)
|
||||||
.as_str(),
|
.and_then(|value| self.comparison.evaluate(value, &self.target_value))
|
||||||
);
|
.or_else(|err| Err(err.to_string()))
|
||||||
self.comparison.evaluate(value, &self.target_value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,11 +105,11 @@ mod tests {
|
|||||||
display_text: "First Name is Jane".to_string(),
|
display_text: "First Name is Jane".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut result = text_comp_success.compare(&student_data);
|
let mut result = text_comp_success.compare(&student_data.data);
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), true);
|
assert_eq!(result.unwrap(), true);
|
||||||
|
|
||||||
result = text_comp_fail.compare(&student_data);
|
result = text_comp_fail.compare(&student_data.data);
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user