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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
extern crate proc_macro;

#[macro_use] mod error;
mod desc;

use crate::desc::{UserDefinedTypeDesc};
use crate::error::{DeriveError, DeriveResult};
use proc_macro::TokenStream;
use proc_macro2::{TokenStream as TokenStream2};
#[cfg(feature = "dump-expansions--unstable")]
use proc_macro2::{Ident as Ident2};
use quote::{quote};
#[cfg(feature = "dump-expansions--unstable")]
use std::fs::{remove_file, File, OpenOptions};
#[cfg(feature = "dump-expansions--unstable")]
use std::io::Write;
#[cfg(feature = "dump-expansions--unstable")]
use std::path::{Path, PathBuf};
use syn::{parse_macro_input, DeriveInput};


#[proc_macro_derive(Delta, attributes(delta))]
pub fn derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let output: TokenStream2 = derive_internal(input).unwrap(
        // This is a HACK that allows more ergonomic code for the meat of
        // the macro while still conforming to the required macro signature.
    );
    TokenStream::from(output)
}

#[allow(non_snake_case)]
fn derive_internal(input: DeriveInput) -> DeriveResult<TokenStream2> {
    let type_desc = UserDefinedTypeDesc::parse(&input)?;
    let delta_type_definition         = type_desc.define_delta_type()?;
    let impl_Deltoid_for_input_type   = type_desc.define_Deltoid_impl()?;
    let impl_IntoDelta_for_input_type = type_desc.define_IntoDelta_impl()?;
    let impl_FromDelta_for_input_type = type_desc.define_FromDelta_impl()?;
    let output: TokenStream2 = quote! {
        #delta_type_definition
        #impl_Deltoid_for_input_type
        #impl_IntoDelta_for_input_type
        #impl_FromDelta_for_input_type
    };

    #[cfg(feature = "print-expansions--unstable")]
    print_generated_code(
        &delta_type_definition,
        &impl_Deltoid_for_input_type,
        &impl_FromDelta_for_input_type,
        &impl_IntoDelta_for_input_type
    );

    #[cfg(feature = "dump-expansions--unstable")]
    write_generated_code_to_file(
        type_desc.type_name(),
        &delta_type_definition,
        &impl_Deltoid_for_input_type,
        &impl_FromDelta_for_input_type,
        &impl_IntoDelta_for_input_type,
    );

    Ok(output)
}


#[cfg(feature = "print-expansions--unstable")]
#[allow(unused, non_snake_case)]
fn print_generated_code(
    delta_type_definition: &TokenStream2,
    impl_Deltoid_for_input_type: &TokenStream2,
    impl_FromDelta_for_input_type: &TokenStream2,
    impl_IntoDelta_for_input_type: &TokenStream2,
) {
    println!("{}\n", delta_type_definition);
    println!("{}\n", impl_Deltoid_for_input_type);
    println!("{}\n", impl_FromDelta_for_input_type);
    println!("{}\n", impl_IntoDelta_for_input_type);
    println!("\n\n\n\n");
}


#[cfg(feature = "dump-expansions--unstable")]
#[allow(unused, non_snake_case)]
fn write_generated_code_to_file(
    type_name: &Ident2,
    delta_type_definition: &TokenStream2,
    impl_Deltoid_for_input_type: &TokenStream2,
    impl_FromDelta_for_input_type: &TokenStream2,
    impl_IntoDelta_for_input_type: &TokenStream2,
) {
    let manifest_dir: &Path = Path::new(env!("CARGO_MANIFEST_DIR"));
    let expanded_dir: PathBuf = manifest_dir.join("expanded");
    let filename: PathBuf = expanded_dir.join(&format!("{}.rs", type_name));

    create_dir(&expanded_dir);
    let _ = remove_file(&filename);
    let mut file: File = open_file(&filename);
    println!("wrote {}", filename.display());

    file.write_all(format!("{}", delta_type_definition).as_bytes())
        .expect("Failed to write delta_type_definition");
    file.write_all("\n\n".as_bytes()).expect("Failed to write newlines");

    file.write_all(format!("{}", impl_Deltoid_for_input_type).as_bytes())
        .expect("Failed to write impl_Deltoid_for_input_type");
    file.write_all("\n\n".as_bytes()).expect("Failed to write newlines");

    file.write_all(format!("{}", impl_FromDelta_for_input_type).as_bytes())
        .expect("Failed to write impl_FromDelta_for_input_type");
    file.write_all("\n\n".as_bytes()).expect("Failed to write newlines");

    file.write_all(format!("{}", impl_IntoDelta_for_input_type).as_bytes())
        .expect("Failed to write impl_IntoDelta_for_input_type");
    file.write_all("\n\n".as_bytes()).expect("Failed to write newlines");

    file.flush().expect(&format!("Failed to flush {}", filename.display()));
    std::process::Command::new("rustfmt")
        .args(&[
            "--emit", "files", "--edition", "2018",
            &format!("{}", filename.display())
        ])
        .output()
        .expect("failed to execute rustfmt;");
}

#[cfg(feature = "dump-expansions--unstable")]
fn create_dir<P: AsRef<Path>>(path: P) {
    let path = path.as_ref();
    std::fs::DirBuilder::new()
        .recursive(true)
        .create(path)
        .expect(&format!("Failed to create dir {}", path.display()));
}

#[cfg(feature = "dump-expansions--unstable")]
fn open_file<P: AsRef<Path>>(path: P) -> File {
    let path = path.as_ref();
    OpenOptions::new()
        .create_new(true)
        .write(true)
        .truncate(true)
        .open(path)
        .expect(&format!("Failed to open {}", path.display()))
}