cynic_codegen/
lib.rs

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
#![deny(rust_2018_idioms)]
pub mod enum_derive;
pub mod fragment_derive;
pub mod generics_for_serde;
pub mod inline_fragments_derive;
pub mod input_object_derive;
pub mod query_variable_literals_derive;
pub mod query_variables_derive;
pub mod registration;
pub mod scalar_derive;
pub mod schema_for_derives;
pub mod schema_module_attr;
pub mod use_schema;

mod error;
mod idents;
mod schema;
mod suggestions;
mod types;

pub use self::{idents::RenameAll, registration::register_schema};

use error::Errors;

#[deprecated(
    since = "3.0.0",
    note = "output_schema_module is deprecated.  See `register_schema` instead"
)]
pub fn output_schema_module(
    schema: impl AsRef<std::path::Path>,
    output_path: impl AsRef<std::path::Path>,
) -> Result<(), Errors> {
    use {std::io::Write, use_schema::UseSchemaParams};

    let tokens = use_schema::use_schema(UseSchemaParams {
        schema_filename: schema.as_ref().to_str().unwrap().to_string(),
    })?;

    {
        let mut out = std::fs::File::create(output_path.as_ref()).unwrap();
        write!(&mut out, "{}", tokens).unwrap();
    }

    format_code(output_path.as_ref()).ok();

    Ok(())
}

#[allow(unused_variables)]
fn format_code(filename: &std::path::Path) -> std::io::Result<()> {
    #[cfg(feature = "rustfmt")]
    {
        std::process::Command::new("cargo")
            .args(["fmt", "--", filename.to_str().unwrap()])
            .spawn()?;
    }
    Ok(())
}

fn variables_fields_path(variables: Option<&syn::Path>) -> Option<syn::Path> {
    variables.cloned().map(|mut variables| {
        // Find VariablesField struct name from Variables struct name
        if let Some(last) = variables.segments.last_mut() {
            last.ident = variables_fields_ident(&last.ident);
        }
        variables
    })
}

fn variables_fields_ident(ident: &syn::Ident) -> syn::Ident {
    proc_macro2::Ident::new(&format!("{}Fields", ident), ident.span())
}