forked from decentralized-identity/didcomm-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shape.rs
38 lines (30 loc) · 904 Bytes
/
shape.rs
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
mod common;
use common::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct DesiredShape {
num_field: usize,
string_field: String,
}
impl Shape for DesiredShape {
type Err = Error;
fn shape(m: &Message) -> Result<DesiredShape, Error> {
serde_json::from_slice(m.get_body()?.as_ref()).map_err(Error::SerdeError)
}
}
#[test]
fn shape_desired_test() {
// Arrange
let initial_shape = DesiredShape {
num_field: 42,
string_field: "important data".into(),
};
let m = Message::new()
.body(&serde_json::to_string(&initial_shape).expect("failed to serialize shape"))
.expect("failed to add body");
// -- pack, send, receive happens here
// Act
let received_typed_body = DesiredShape::shape(&m).unwrap();
// Assert
assert_eq!(received_typed_body, initial_shape);
}