Skip to main content

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| Ok(*u.choose(CHARSET)?))
67            .collect::<ArbitraryResult<Vec<u8>>>()?,
68    )
69    .unwrap();
70
71    Ok(gen_str)
72}
73
74#[cfg(test)]
75mod tests {
76
77    #[test]
78    fn convert_description_from_parser() {
79        use crate::description::Description;
80        use apollo_parser::cst::Definition;
81        use apollo_parser::Parser;
82
83        let schema = r#"
84"Description for the schema"
85schema {}
86        "#;
87        let parser = Parser::new(schema);
88        let cst = parser.parse();
89        let document = cst.document();
90        if let Definition::SchemaDefinition(def) = document.definitions().next().unwrap() {
91            let parser_description = def.description().unwrap();
92            let smith_description = Description::from(parser_description);
93            assert_eq!(
94                smith_description,
95                Description::from("Description for the schema".to_string())
96            );
97        } else {
98            unreachable!();
99        }
100    }
101}