cxx_gen/
lib.rs

1//! The CXX code generator for constructing and compiling C++ code.
2//!
3//! This is intended as a mechanism for embedding the `cxx` crate into
4//! higher-level code generators. See [dtolnay/cxx#235] and
5//! [https://github.com/google/autocxx].
6//!
7//! [dtolnay/cxx#235]: https://github.com/dtolnay/cxx/issues/235
8//! [https://github.com/google/autocxx]: https://github.com/google/autocxx
9
10#![doc(html_root_url = "https://docs.rs/cxx-gen/0.7.141")]
11#![deny(missing_docs)]
12#![allow(dead_code)]
13#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
14#![allow(
15    clippy::cast_sign_loss,
16    clippy::default_trait_access,
17    clippy::enum_glob_use,
18    clippy::inherent_to_string,
19    clippy::items_after_statements,
20    clippy::match_bool,
21    clippy::match_on_vec_items,
22    clippy::match_same_arms,
23    clippy::missing_errors_doc,
24    clippy::must_use_candidate,
25    clippy::needless_lifetimes,
26    clippy::needless_pass_by_value,
27    clippy::nonminimal_bool,
28    clippy::redundant_else,
29    clippy::ref_option,
30    clippy::similar_names,
31    clippy::single_match_else,
32    clippy::struct_excessive_bools,
33    clippy::struct_field_names,
34    clippy::too_many_arguments,
35    clippy::too_many_lines,
36    clippy::toplevel_ref_arg,
37    clippy::uninlined_format_args
38)]
39
40mod error;
41mod gen;
42mod syntax;
43
44pub use crate::error::Error;
45pub use crate::gen::include::{Include, HEADER};
46pub use crate::gen::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
47pub use crate::syntax::IncludeKind;
48use proc_macro2::TokenStream;
49
50/// Generate C++ bindings code from a Rust token stream. This should be a Rust
51/// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
52pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
53    let syntax = syn::parse2(rust_source)
54        .map_err(crate::gen::Error::from)
55        .map_err(Error::from)?;
56    gen::generate(syntax, opt).map_err(Error::from)
57}