From c687ee60ffb6abe43e812c19aa8503648fab3fdb Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 28 Nov 2015 20:09:03 -0800 Subject: [PATCH] feat(example): Add an example --- examples/serde-syntex-example/.gitignore | 2 ++ examples/serde-syntex-example/Cargo.toml | 18 ++++++++++++ examples/serde-syntex-example/README.md | 20 ++++++++++++++ examples/serde-syntex-example/build.rs | 29 ++++++++++++++++++++ examples/serde-syntex-example/src/main.rs | 11 ++++++++ examples/serde-syntex-example/src/main.rs.in | 16 +++++++++++ 6 files changed, 96 insertions(+) create mode 100644 examples/serde-syntex-example/.gitignore create mode 100644 examples/serde-syntex-example/Cargo.toml create mode 100644 examples/serde-syntex-example/README.md create mode 100644 examples/serde-syntex-example/build.rs create mode 100644 examples/serde-syntex-example/src/main.rs create mode 100644 examples/serde-syntex-example/src/main.rs.in diff --git a/examples/serde-syntex-example/.gitignore b/examples/serde-syntex-example/.gitignore new file mode 100644 index 00000000..a9d37c56 --- /dev/null +++ b/examples/serde-syntex-example/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/examples/serde-syntex-example/Cargo.toml b/examples/serde-syntex-example/Cargo.toml new file mode 100644 index 00000000..81fdbad3 --- /dev/null +++ b/examples/serde-syntex-example/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "serde-syntex-example" +version = "0.1.0" +authors = ["Erick Tryzelaar "] +build = "build.rs" + +[features] +default = ["serde_codegen"] +nightly = ["serde_macros"] + +[build-dependencies] +serde_codegen = { version = "^0.6.4", optional = true } +syntex = "^0.22.0" + +[dependencies] +serde = "^0.6.1" +serde_json = "^0.6.0" +serde_macros = { version = "^0.6.1", optional = true } diff --git a/examples/serde-syntex-example/README.md b/examples/serde-syntex-example/README.md new file mode 100644 index 00000000..ea919313 --- /dev/null +++ b/examples/serde-syntex-example/README.md @@ -0,0 +1,20 @@ +This example demonstrates how to use Serde with Syntex. On stable or nightly +with Syntex, it can be built with: + +``` +% multirust run stable cargo run + Running `target/debug/serde-syntex-example` +{"x":1,"y":2} +Point { x: 1, y: 2 } + +% multirust run nightly cargo run + Running `target/debug/serde-syntex-example` +{"x":1,"y":2} +Point { x: 1, y: 2 } +``` + +On nightly, it can use a plugin with: + +``` +% multirust run nightly cargo run --features nightly --no-default-features +``` diff --git a/examples/serde-syntex-example/build.rs b/examples/serde-syntex-example/build.rs new file mode 100644 index 00000000..b1aadd78 --- /dev/null +++ b/examples/serde-syntex-example/build.rs @@ -0,0 +1,29 @@ +#[cfg(not(feature = "serde_macros"))] +mod inner { + extern crate syntex; + extern crate serde_codegen; + + use std::env; + use std::path::Path; + + pub fn main() { + let out_dir = env::var_os("OUT_DIR").unwrap(); + + let src = Path::new("src/main.rs.in"); + let dst = Path::new(&out_dir).join("main.rs"); + + let mut registry = syntex::Registry::new(); + + serde_codegen::register(&mut registry); + registry.expand("", &src, &dst).unwrap(); + } +} + +#[cfg(feature = "serde_macros")] +mod inner { + pub fn main() {} +} + +fn main() { + inner::main(); +} diff --git a/examples/serde-syntex-example/src/main.rs b/examples/serde-syntex-example/src/main.rs new file mode 100644 index 00000000..9c2333fb --- /dev/null +++ b/examples/serde-syntex-example/src/main.rs @@ -0,0 +1,11 @@ +#![cfg_attr(nightly, feature(custom_derive, plugin))] +#![cfg_attr(nightly, plugin(serde_macros))] + +extern crate serde; +extern crate serde_json; + +#[cfg(feature = "serde_macros")] +include!("main.rs.in"); + +#[cfg(not(feature = "serde_macros"))] +include!(concat!(env!("OUT_DIR"), "/main.rs")); diff --git a/examples/serde-syntex-example/src/main.rs.in b/examples/serde-syntex-example/src/main.rs.in new file mode 100644 index 00000000..4cf4841c --- /dev/null +++ b/examples/serde-syntex-example/src/main.rs.in @@ -0,0 +1,16 @@ +#[derive(Serialize, Deserialize, Debug)] +struct Point { + x: i32, + y: i32, +} + +fn main() { + let point = Point { x: 1, y: 2 }; + let serialized = serde_json::to_string(&point).unwrap(); + + println!("{}", serialized); + + let deserialized: Point = serde_json::from_str(&serialized).unwrap(); + + println!("{:?}", deserialized); +}