Skip to content

Commit

Permalink
proc macro for custom #[derive(Deserialize, FromFile)]
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 30, 2018
1 parent 56beee8 commit bb9158b
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 43 deletions.
51 changes: 31 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ tempdir = "0.3.7"
reqwest = "0.9.2"
rjs = { path = "rjs-parse" }
from_file = { path = "from-file" }
from_file_derive = { path = "from_file_derive" }
33 changes: 21 additions & 12 deletions from-file/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions from-file/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ serde_yaml = "0.8"
serde_derive = "1.0"
serde_json = "1.0"

from_file_derive = { path = "../from_file_derive" }

[lib]
name = "from_file"
path = "src/lib.rs"
3 changes: 1 addition & 2 deletions from-file/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,10 @@ mod tests {

#[test]
fn test_from_file() {
#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, FromFile, Debug, PartialEq)]
struct Person {
name: String,
}
impl FromFile for Person {}

let p1 = Person::from_file("test/fixtures/person.json").expect("file->Person");
assert_eq!(
Expand Down
11 changes: 11 additions & 0 deletions from_file_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "from_file_derive"
version = "0.1.0"
authors = ["Shane Osbourne <[email protected]>"]

[dependencies]
syn = "0.15.22"
quote = "0.6.10"

[lib]
proc-macro = true
24 changes: 24 additions & 0 deletions from_file_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

#[proc_macro_derive(FromFile)]
pub fn from_file_macro_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();

// Build the trait implementation
impl_from_file_macro(&ast)
}

fn impl_from_file_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl FromFile for #name {}
};
gen.into()
}
1 change: 1 addition & 0 deletions rjs-parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ serde_json = "1.0"
serde_yaml = "0.8"
ratel = "0.7.0"
from_file = { path = "../from-file" }
from_file_derive = { path = "../from_file_derive" }

[lib]
name = "rjs"
Expand Down
3 changes: 1 addition & 2 deletions rjs-parse/src/bundle_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate serde_yaml;

use from_file::FromFile;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, FromFile, Debug, PartialEq)]
pub struct BundleConfig {
pub bundles: Vec<ConfigItem>,
pub module_blacklist: Option<Vec<String>>,
Expand Down Expand Up @@ -88,7 +88,6 @@ pub struct ConfigItem {
pub children: Vec<ConfigItem>,
}

impl FromFile for BundleConfig {}
impl Default for BundleConfig {
fn default() -> BundleConfig {
BundleConfig {
Expand Down
2 changes: 2 additions & 0 deletions rjs-parse/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[macro_use]
extern crate from_file_derive;
extern crate from_file;
extern crate ratel;
extern crate serde;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/bs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ extern crate actix_web;
extern crate base64;
extern crate bytes;
extern crate clap;

#[macro_use]
extern crate from_file_derive;
extern crate from_file;

extern crate futures;
extern crate http;
extern crate mime;
Expand Down
4 changes: 1 addition & 3 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use options::ConfigError;
use serde_json::Value;
use std;

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, FromFile, Debug, Clone)]
pub struct ProgramConfig {
pub presets: Vec<PresetConfig>,
}
Expand All @@ -19,8 +19,6 @@ impl Default for ProgramConfig {
}
}

impl FromFile for ProgramConfig {}

impl ProgramConfig {
pub fn get_opts(&self, name: &str) -> Option<serde_json::Value> {
self.presets
Expand Down
6 changes: 2 additions & 4 deletions src/lib/presets/m2/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use from_file::FromFile;
use rjs::modules::ModuleData;
use rjs::RequireJsClientConfig;

#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize, FromFile, Default)]
pub struct SeedData {
pub rjs_client_config: RequireJsClientConfig,
pub req_log: Vec<ModuleData>,
}

impl FromFile for SeedData {}
}

0 comments on commit bb9158b

Please sign in to comment.