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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::fmt;
use crate::StringValue;
#[derive(Debug, PartialEq, Clone)]
pub struct ScalarDef {
name: String,
description: StringValue,
}
impl ScalarDef {
pub fn new(name: String) -> Self {
Self {
name,
description: StringValue::Top { source: None },
}
}
pub fn description(&mut self, description: Option<String>) {
self.description = StringValue::Top {
source: description,
};
}
}
impl fmt::Display for ScalarDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description)?;
writeln!(f, "scalar {}", self.name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn it_encodes_scalar() {
let scalar = ScalarDef::new("NumberOfTreatsPerDay".to_string());
assert_eq!(
scalar.to_string(),
r#"scalar NumberOfTreatsPerDay
"#
);
}
#[test]
fn it_encodes_scalar_with_description() {
let mut scalar = ScalarDef::new("NumberOfTreatsPerDay".to_string());
scalar.description(Some(
"Int representing number of treats received.".to_string(),
));
assert_eq!(
scalar.to_string(),
r#""Int representing number of treats received."
scalar NumberOfTreatsPerDay
"#
);
}
}