cynic_codegen/
lib.rs

1#![deny(rust_2018_idioms)]
2pub mod enum_derive;
3pub mod fragment_derive;
4pub mod generics_for_serde;
5pub mod inline_fragments_derive;
6pub mod input_object_derive;
7pub mod query_variable_literals_derive;
8pub mod query_variables_derive;
9pub mod registration;
10pub mod scalar_derive;
11pub mod schema_for_derives;
12pub mod schema_module_attr;
13pub mod use_schema;
14
15mod error;
16mod idents;
17mod schema;
18mod suggestions;
19mod types;
20
21pub use self::{idents::RenameAll, registration::register_schema};
22
23use error::Errors;
24
25#[deprecated(
26    since = "3.0.0",
27    note = "output_schema_module is deprecated.  See `register_schema` instead"
28)]
29pub fn output_schema_module(
30    schema: impl AsRef<std::path::Path>,
31    output_path: impl AsRef<std::path::Path>,
32) -> Result<(), Errors> {
33    use {std::io::Write, use_schema::UseSchemaParams};
34
35    let tokens = use_schema::use_schema(UseSchemaParams {
36        schema_filename: schema.as_ref().to_str().unwrap().to_string(),
37    })?;
38
39    {
40        let mut out = std::fs::File::create(output_path.as_ref()).unwrap();
41        write!(&mut out, "{}", tokens).unwrap();
42    }
43
44    format_code(output_path.as_ref()).ok();
45
46    Ok(())
47}
48
49#[allow(unused_variables)]
50fn format_code(filename: &std::path::Path) -> std::io::Result<()> {
51    #[cfg(feature = "rustfmt")]
52    {
53        std::process::Command::new("cargo")
54            .args(["fmt", "--", filename.to_str().unwrap()])
55            .spawn()?;
56    }
57    Ok(())
58}
59
60fn variables_fields_path(variables: Option<&syn::Path>) -> Option<syn::Path> {
61    variables.cloned().map(|mut variables| {
62        // Find VariablesField struct name from Variables struct name
63        if let Some(last) = variables.segments.last_mut() {
64            last.ident = variables_fields_ident(&last.ident);
65        }
66        variables
67    })
68}
69
70fn variables_fields_ident(ident: &syn::Ident) -> syn::Ident {
71    proc_macro2::Ident::new(&format!("{}Fields", ident), ident.span())
72}