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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
// Copyright 2018 Palantir Technologies, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Code generation for Conjure definitions. //! //! # Examples //! //! Code generation via a build script, assuming we have a `service-api.conjure.json` file in the crate root: //! //! build.rs: //! //! ```no_run //! use std::env; //! use std::path::Path; //! //! fn main() { //! let input = "service-api.conjure.json"; //! let output = Path::new(&env::var_os("OUT_DIR").unwrap()).join("service_api"); //! //! println!("cargo:rerun-if-changed={}", input); //! conjure_codegen::Config::new() //! .run_rustfmt(false) //! .generate_files(input, output) //! .unwrap(); //! } //! ``` //! //! src/lib.rs: //! //! ```ignore //! mod service_api { //! include!(concat!(env!("OUT_DIR"), "/service_api/mod.rs")); //! } //! ``` //! //! # Types //! //! ## Builtin //! //! Builtin types map directly to existing Rust types: //! //! | Conjure | Rust | //! | ------------- | ----------------------------------- | //! | `string` | `String` | //! | `datetime` | `chrono::DateTime<Utc>` | //! | `integer` | `i32` | //! | `double` | `f64` | //! | `safelong` | `conjure_object::SafeLong` | //! | `binary` | `serde_bytes::ByteBuf` | //! | `any` | `serde_value::Value` | //! | `boolean` | `bool` | //! | `uuid` | `uuid::Uuid` | //! | `rid` | `conjure_object::ResourceIdentifier` | //! | `bearertoken` | `conjure_object::BearerToken` | //! | `optional<T>` | `Option<T>` | //! | `list<T>` | `Vec<T>` | //! | `set<T>` | `BTreeSet<T>` | //! | `map<K, V>` | `BTreeMap<K, V>` | //! //! Many of these are exposed by the `conjure-types` crate, which is a required dependency of crates containing the //! generated code. //! //! ## Objects //! //! Conjure objects turn into Rust structs along with builders used to construct them: //! //! ```rust //! # use conjure_codegen::example_types::{ManyFieldExample, StringAliasExample}; //! let object = ManyFieldExample::builder() //! .string("foo") //! .integer(123) //! .double_value(3.14) //! .optional_item("bar".to_string()) //! .items(vec!["hello".to_string(), "world".to_string()]) //! .alias(StringAliasExample("foobar".to_string())) //! .build(); //! //! assert_eq!(object.string(), "foo"); //! assert_eq!(object.optional_item(), Some("bar")); //! ``` //! //! The generated structs implement `Debug`, `Clone`, `PartialEq`, `PartialOrd`, `Serialize`, and `Deserialize`. They //! also implement `Eq`, `Ord`, and `Hash` if they do not contain a `double` value, and `Copy` if they consist entirely //! of copyable primitive types. //! //! ## Unions //! //! Conjure unions turn into Rust enums. By default, unions are *extensible* through an additional `Unknown` variant. //! This allows unions to be forward-compatible by allowing clients to deserialize variants they don't yet know about //! and reserialize them properly: //! //! ```rust //! # use conjure_codegen::example_types::UnionTypeExample; //! # let union_value = UnionTypeExample::If(0); //! match union_value { //! UnionTypeExample::StringExample(string) => { //! // ... //! } //! UnionTypeExample::Set(set) => { //! // ... //! } //! // ... //! UnionTypeExample::Unknown(unknown) => { //! println!("got unknown variant: {}", unknown.type_()); //! } //! # _ => {} //! } //! ``` //! //! The generated enums implement `Debug`, `Clone`, `PartialEq`, `PartialOrd`, `Serialize`, and `Deserialize`. They //! also implement `Eq`, `Ord`, and `Hash` if they do not contain a `double` value. Union variants which are themselves //! unions are boxed in the generated enum to avoid self-referential type definitions. //! //! ## Enums //! //! Conjure enums turn into Rust enums. By default, enums are *extensible*. This allows enums to be forward-compatible //! by allowing clients to deserialize variants they don't yet know about and reserialize them properly: //! //! ```rust //! # use conjure_codegen::example_types::EnumExample; //! # let enum_value = EnumExample::One; //! match enum_value { //! EnumExample::One => println!("found one"), //! EnumExample::Two => println!("found two"), //! EnumExample::Unknown(unknown) => println!("got unknown variant: {}", unknown), //! } //! ``` //! //! The generated enums implement `Debug`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Display`, //! `Serialize`, and `Deserialize`. //! //! ## Aliases //! //! Conjure aliases turn into Rust newtype structs that act like their inner value: //! //! ```rust //! # use conjure_codegen::example_types::StringAliasExample; //! let alias_value = StringAliasExample("hello world".to_string()); //! assert!(alias_value.starts_with("hello")); //! ``` //! //! The generated structs implement `Deref`, `DerefMut`, `Debug`, `Clone`, `PartialEq`, `PartialOrd`, `Serialize`, and //! `Deserialize`. They also implement `Eq`, `Ord`, and `Hash` if they do not contain a `double` value, `Copy` if they //! wrap a copyable primitive type, `Default` if they wrap a type implementing `Default`, and `Display` if they wrap a //! type implementing `Display`. #![warn(clippy::all, missing_docs)] #![doc(html_root_url = "https://docs.rs/conjure-codegen/0.1")] #![recursion_limit = "256"] use failure::{bail, Error, ResultExt}; use proc_macro2::TokenStream; use quote::quote; use std::env; use std::ffi::{OsStr, OsString}; use std::fs; use std::path::Path; use std::process::Command; use crate::context::Context; use crate::types::{ConjureDefinition, TypeDefinition}; mod aliases; mod context; mod enums; mod objects; #[allow(dead_code, clippy::all)] mod types; mod unions; /// Examples of generated Conjure code. /// /// This module is only intended to be present in documentation; it shouldn't be relied on by any library code. #[cfg(feature = "example-types")] #[allow(warnings)] pub mod example_types; /// Codegen configuration. pub struct Config { rustfmt: OsString, run_rustfmt: bool, exhaustive: bool, } impl Default for Config { fn default() -> Config { Config::new() } } impl Config { /// Creates a new `Config` with default settings. pub fn new() -> Config { Config { rustfmt: env::var_os("RUSTFMT").unwrap_or_else(|| OsString::from("rustfmt")), run_rustfmt: true, exhaustive: false, } } /// Controls exhaustive matchability of unions and enums. /// /// Non-exhaustive unions and enums have the ability to deserialize and reserialize unknown variants. This enables /// clients to be more forward-compatible with changes made by newer servers. /// /// Defaults to `false`. pub fn exhaustive(&mut self, exhaustive: bool) -> &mut Config { self.exhaustive = exhaustive; self } /// Controls the use of rustfmt to format generated source code. /// /// Defaults to `true`. pub fn run_rustfmt(&mut self, run_rustfmt: bool) -> &mut Config { self.run_rustfmt = run_rustfmt; self } /// Sets the name of the binary used to format source code. /// /// Defaults to the value of the `RUSTFMT` environment variable, or `rustfmt` if not set. pub fn rustfmt<T>(&mut self, rustfmt: T) -> &mut Config where T: AsRef<OsStr>, { self.rustfmt = rustfmt.as_ref().to_owned(); self } /// Generates Rust source files from a JSON-encoded Conjure IR file. pub fn generate_files<P, Q>(&self, ir_file: P, out_dir: Q) -> Result<(), Error> where P: AsRef<Path>, Q: AsRef<Path>, { self.generate_files_inner(ir_file.as_ref(), out_dir.as_ref()) } fn generate_files_inner(&self, ir_file: &Path, out_dir: &Path) -> Result<(), Error> { let defs = self.parse_ir(ir_file)?; if defs.version() != 1 { bail!("unsupported IR version {}", defs.version()); } let modules = self.create_modules(&defs); fs::create_dir_all(out_dir) .with_context(|_| format!("error creating directory {}", out_dir.display()))?; for module in &modules { self.write_module( &out_dir.join(format!("{}.rs", module.module_name)), &module.contents, )?; } let root_module = self.create_root_module(&modules); self.write_module(&out_dir.join("mod.rs"), &root_module)?; Ok(()) } fn parse_ir(&self, ir_file: &Path) -> Result<ConjureDefinition, Error> { let ir = fs::read_to_string(ir_file) .with_context(|_| format!("error reading file {}", ir_file.display()))?; let defs = serde_json::from_str(&ir) .with_context(|_| format!("error parsing Conjure IR file {}", ir_file.display()))?; Ok(defs) } fn write_module(&self, path: &Path, contents: &TokenStream) -> Result<(), Error> { fs::write(path, contents.to_string()) .with_context(|_| format!("error writing module {}", path.display()))?; if self.run_rustfmt { let _ = Command::new(&self.rustfmt).arg(&path).status(); } Ok(()) } fn create_modules(&self, defs: &ConjureDefinition) -> Vec<Module> { let context = Context::new(&defs, self.exhaustive); let mut modules = vec![]; for def in defs.types() { let (type_name, contents) = match def { TypeDefinition::Enum(def) => (def.type_name(), enums::generate(&context, def)), TypeDefinition::Alias(def) => (def.type_name(), aliases::generate(&context, def)), TypeDefinition::Union(def) => (def.type_name(), unions::generate(&context, def)), TypeDefinition::Object(def) => (def.type_name(), objects::generate(&context, def)), }; let module = Module { module_name: context.module_name(type_name), type_name: context.type_name(type_name.name()).to_string(), contents, }; modules.push(module); } modules } fn create_root_module(&self, modules: &[Module]) -> TokenStream { let uses = modules.iter().map(|m| { let module_name = m.module_name.parse::<TokenStream>().unwrap(); let type_name = m.type_name.parse::<TokenStream>().unwrap(); quote! { #[doc(inline)] pub use self::#module_name::#type_name; } }); let mods = modules.iter().map(|m| { let module_name = m.module_name.parse::<TokenStream>().unwrap(); quote! { pub mod #module_name; } }); quote! { #(#uses)* #(#mods)* } } } struct Module { module_name: String, type_name: String, contents: TokenStream, }