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.143")]
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::elidable_lifetime_names,
18    clippy::enum_glob_use,
19    clippy::inherent_to_string,
20    clippy::items_after_statements,
21    clippy::match_bool,
22    clippy::match_on_vec_items,
23    clippy::match_same_arms,
24    clippy::missing_errors_doc,
25    clippy::must_use_candidate,
26    clippy::needless_lifetimes,
27    clippy::needless_pass_by_value,
28    clippy::nonminimal_bool,
29    clippy::redundant_else,
30    clippy::ref_option,
31    clippy::similar_names,
32    clippy::single_match_else,
33    clippy::struct_excessive_bools,
34    clippy::struct_field_names,
35    clippy::too_many_arguments,
36    clippy::too_many_lines,
37    clippy::toplevel_ref_arg,
38    clippy::uninlined_format_args
39)]
40
41mod error;
42mod gen;
43mod syntax;
44
45pub use crate::error::Error;
46pub use crate::gen::include::{Include, HEADER};
47pub use crate::gen::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
48pub use crate::syntax::IncludeKind;
49use proc_macro2::TokenStream;
50
51/// Generate C++ bindings code from a Rust token stream. This should be a Rust
52/// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
53pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
54    let syntax = syn::parse2(rust_source)
55        .map_err(crate::gen::Error::from)
56        .map_err(Error::from)?;
57    gen::generate(syntax, opt).map_err(Error::from)
58}