apollo_smith/
description.rs

1use crate::DocumentBuilder;
2use arbitrary::Arbitrary;
3use arbitrary::Result as ArbitraryResult;
4use arbitrary::Unstructured;
5use std::fmt::Write as _;
6
7const CHARSET: &[u8] =
8    b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\n\r\t/$#!.-+='";
9
10/// The `__Description` type represents a description
11///
12/// *Description*:
13///     "string"
14///
15/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#sec-Descriptions).
16///
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub struct Description(String);
19
20impl From<Description> for String {
21    fn from(desc: Description) -> Self {
22        desc.0
23    }
24}
25
26impl From<Description> for apollo_compiler::Node<str> {
27    fn from(desc: Description) -> Self {
28        desc.0.into()
29    }
30}
31
32impl From<apollo_parser::cst::Description> for Description {
33    fn from(desc: apollo_parser::cst::Description) -> Self {
34        Description(desc.string_value().map(|s| s.into()).unwrap_or_default())
35    }
36}
37
38impl From<String> for Description {
39    fn from(desc: String) -> Self {
40        Description(desc)
41    }
42}
43
44impl Arbitrary<'_> for Description {
45    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> ArbitraryResult<Self> {
46        let mut arbitrary_str = limited_string_desc(u, 100)?;
47        if arbitrary_str.trim_matches('"').is_empty() {
48            let _ = write!(arbitrary_str, "{}", u.arbitrary::<usize>()?);
49        }
50        Ok(Self(arbitrary_str))
51    }
52}
53
54impl DocumentBuilder<'_> {
55    /// Create an arbitrary `Description`
56    pub fn description(&mut self) -> ArbitraryResult<Description> {
57        self.u.arbitrary()
58    }
59}
60
61fn limited_string_desc(u: &mut Unstructured<'_>, max_size: usize) -> ArbitraryResult<String> {
62    let size = u.int_in_range(0..=max_size)?;
63
64    let gen_str = String::from_utf8(
65        (0..size)
66            .map(|_curr_idx| {
67                let idx = u.arbitrary::<usize>()?;
68
69                let idx = idx % CHARSET.len();
70
71                Ok(CHARSET[idx])
72            })
73            .collect::<ArbitraryResult<Vec<u8>>>()?,
74    )
75    .unwrap();
76
77    Ok(gen_str)
78}
79
80#[cfg(test)]
81mod tests {
82
83    #[test]
84    fn convert_description_from_parser() {
85        use crate::description::Description;
86        use apollo_parser::cst::Definition;
87        use apollo_parser::Parser;
88
89        let schema = r#"
90"Description for the schema"
91schema {}
92        "#;
93        let parser = Parser::new(schema);
94        let cst = parser.parse();
95        let document = cst.document();
96        if let Definition::SchemaDefinition(def) = document.definitions().next().unwrap() {
97            let parser_description = def.description().unwrap();
98            let smith_description = Description::from(parser_description);
99            assert_eq!(
100                smith_description,
101                Description::from("Description for the schema".to_string())
102            );
103        } else {
104            unreachable!();
105        }
106    }
107}