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
use std::fmt;
use crate::Argument;
#[derive(Debug, PartialEq, Clone)]
pub struct Directive {
    name: String,
    args: Vec<Argument>,
}
impl Directive {
    
    pub fn new(name: String) -> Self {
        Self {
            name,
            args: Vec::new(),
        }
    }
    
    pub fn arg(&mut self, arg: Argument) {
        self.args.push(arg);
    }
}
impl fmt::Display for Directive {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "@{}", self.name)?;
        if !self.args.is_empty() {
            for (i, arg) in self.args.iter().enumerate() {
                match i {
                    0 => write!(f, "({}", arg)?,
                    _ => write!(f, ", {}", arg)?,
                }
            }
            write!(f, ")")?;
        }
        Ok(())
    }
}
#[cfg(test)]
mod tests {
    use crate::Value;
    use super::*;
    #[test]
    fn it_encodes_directive() {
        let mut directive = Directive::new(String::from("myDirective"));
        directive.arg(Argument::new(String::from("first"), Value::Int(5)));
        assert_eq!(directive.to_string(), "@myDirective(first: 5)");
    }
}