1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{
json::ConcreteJsonLoader,
loader::{error::LoaderError, trait_::LoaderTrait},
};
use json_trait_rs::{RustType, ToRustType};
#[allow(clippy::module_name_repetitions)]
pub type RustTypeLoader = ConcreteJsonLoader<RustType>;
impl LoaderTrait<RustType> for RustTypeLoader {
fn load_from_bytes(&self, content: &[u8]) -> Result<RustType, LoaderError>
where
Self: Sized,
{
serde_json::from_slice::<serde_json::Value>(content)
.map(|value| value.to_rust_type())
.or_else(|ref serde_error| Err(LoaderError::from(serde_error)))
}
}
#[cfg(test)]
mod tests {
use super::RustTypeLoader;
use crate::{loader::error::LoaderError, testing_helpers::MockLoaderRequestBuilder, traits::check_loader};
use json_trait_rs::RustType;
use test_case::test_case;
#[test]
fn test_is_loader() {
check_loader::<_, RustTypeLoader>()
}
#[test_case("Boolean.json", &RustType::from(false))]
#[test_case("Integer.json", &RustType::from(1))]
#[test_case("Null.json", &RustType::from(()))]
#[test_case("String.json", &RustType::from("Some Text"))]
fn test_load_valid_content(file_name: &'static str, expected_loaded_object: &RustType) {
assert_eq!(
&*MockLoaderRequestBuilder::default()
.resp_body_file_path(vec![file_name])
.build()
.unwrap()
.send_request(&RustTypeLoader::default())
.unwrap(),
expected_loaded_object,
);
}
#[test]
fn test_load_invalid_content() {
assert!(matches!(
MockLoaderRequestBuilder::default()
.resp_body_file_path(vec!["Invalid.json"])
.build()
.unwrap()
.send_request(&RustTypeLoader::default())
.unwrap_err(),
LoaderError::FormatError(value) if "EOF while parsing an object at line 2 column 0" == &value
));
}
}