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;
/// The `Directive` type represents a Directive, it provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
///
/// *Directive*:
/// @ Name Arguments?
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#sec-Language.Directives).
///
/// ### Example
/// ```rust
/// use apollo_encoder::{Argument, Directive, Value};
///
/// 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)");
/// ```
#[derive(Debug, PartialEq, Clone)]
pub struct Directive {
name: String,
args: Vec<Argument>,
}
impl Directive {
/// Create an instance of Directive
pub fn new(name: String) -> Self {
Self {
name,
args: Vec::new(),
}
}
/// Add an argument to the directive
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)");
}
}