@@ -71,7 +71,10 @@ pub mod codegen;
71
71
pub mod codegen_types;
72
72
mod pointer_constants;
73
73
74
- use std:: path:: { Path , PathBuf } ;
74
+ use std:: {
75
+ collections:: HashMap ,
76
+ path:: { Path , PathBuf } ,
77
+ } ;
75
78
76
79
// Copied from capnp/src/lib.rs, where this conversion lives behind the "std" feature flag,
77
80
// which we don't want to depend on here.
@@ -119,6 +122,7 @@ pub struct CompilerCommand {
119
122
output_path : Option < PathBuf > ,
120
123
default_parent_module : Vec < String > ,
121
124
raw_code_generator_request_path : Option < PathBuf > ,
125
+ crate_provides_map : HashMap < u64 , String > ,
122
126
}
123
127
124
128
impl CompilerCommand {
@@ -156,6 +160,75 @@ impl CompilerCommand {
156
160
self
157
161
}
158
162
163
+ /// Specify that `crate_name` provides generated code for `files`.
164
+ ///
165
+ /// This means that when your schema refers to types defined in `files` we
166
+ /// will generate Rust code that uses identifiers in `crate_name`.
167
+ ///
168
+ /// # Arguments
169
+ ///
170
+ /// - `crate_name`: The Rust identifier of the crate
171
+ /// - `files`: the Capnp file ids the crate provides generated code for
172
+ ///
173
+ /// # When to use
174
+ ///
175
+ /// You only need this when your generated code needs to refer to types in
176
+ /// the external crate. If you just want to use an annotation and the
177
+ /// argument to that annotation is a builtin type (e.g. `$Json.name`) this
178
+ /// isn't necessary.
179
+ ///
180
+ /// # Example
181
+ ///
182
+ /// If you write a schema like so
183
+ ///
184
+ /// ```capnp
185
+ /// // my_schema.capnp
186
+ ///
187
+ /// using Json = import "/capnp/compat/json.capnp";
188
+ ///
189
+ /// struct Foo {
190
+ /// value @0 :Json.Value;
191
+ /// }
192
+ /// ```
193
+ ///
194
+ /// you'd look at [json.capnp][json.capnp] to see its capnp id.
195
+ ///
196
+ /// ```capnp
197
+ /// // json.capnp
198
+ ///
199
+ /// # Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors ...
200
+ /// @0x8ef99297a43a5e34;
201
+ /// ```
202
+ ///
203
+ /// If you want the `foo::Builder::get_value` method generated for your
204
+ /// schema to return a `capnp_json::json_capnp::value::Reader` you'd add a
205
+ /// dependency on `capnp_json` to your `Cargo.toml` and specify it provides
206
+ /// `json.capnp` in your `build.rs`.
207
+ ///
208
+ /// ```rust,no_run
209
+ /// // build.rs
210
+ ///
211
+ /// capnpc::CompilerCommand::new()
212
+ /// .crate_provides("json_capnp", [0x8ef99297a43a5e34])
213
+ /// .file("my_schema.capnp")
214
+ /// .run()
215
+ /// .unwrap();
216
+ /// ```
217
+ ///
218
+ /// [json.capnp]:
219
+ /// https://github.com/capnproto/capnproto/blob/master/c%2B%2B/src/capnp/compat/json.capnp
220
+ pub fn crate_provides (
221
+ & mut self ,
222
+ crate_name : impl Into < String > ,
223
+ files : impl IntoIterator < Item = u64 > ,
224
+ ) -> & mut Self {
225
+ let crate_name = crate_name. into ( ) ;
226
+ for file in files. into_iter ( ) {
227
+ self . crate_provides_map . insert ( file, crate_name. clone ( ) ) ;
228
+ }
229
+ self
230
+ }
231
+
159
232
/// Adds the --no-standard-import flag, indicating that the default import paths of
160
233
/// /usr/include and /usr/local/include should not bet included.
161
234
pub fn no_standard_import ( & mut self ) -> & mut Self {
@@ -307,7 +380,8 @@ impl CompilerCommand {
307
380
let mut code_generation_command = crate :: codegen:: CodeGenerationCommand :: new ( ) ;
308
381
code_generation_command
309
382
. output_directory ( output_path)
310
- . default_parent_module ( self . default_parent_module . clone ( ) ) ;
383
+ . default_parent_module ( self . default_parent_module . clone ( ) )
384
+ . crates_provide_map ( self . crate_provides_map . clone ( ) ) ;
311
385
if let Some ( raw_code_generator_request_path) = & self . raw_code_generator_request_path {
312
386
code_generation_command
313
387
. raw_code_generator_request_path ( raw_code_generator_request_path. clone ( ) ) ;
0 commit comments