Skip to content

Commit 700cd0c

Browse files
committed
Expose IncludeKind to cxx_gen library
1 parent 36b1dba commit 700cd0c

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

gen/cmd/src/app.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
mod test;
44

55
use super::{Opt, Output};
6+
use crate::gen::include::Include;
7+
use crate::syntax::IncludeKind;
68
use clap::AppSettings;
79
use std::ffi::{OsStr, OsString};
810
use std::path::PathBuf;
@@ -63,7 +65,19 @@ pub(super) fn from_args() -> Opt {
6365
let include = matches
6466
.values_of(INCLUDE)
6567
.unwrap_or_default()
66-
.map(str::to_owned)
68+
.map(|include| {
69+
if include.starts_with('<') && include.ends_with('>') {
70+
Include {
71+
path: include[1..include.len() - 1].to_owned(),
72+
kind: IncludeKind::Bracketed,
73+
}
74+
} else {
75+
Include {
76+
path: include.to_owned(),
77+
kind: IncludeKind::Quoted,
78+
}
79+
}
80+
})
6781
.collect();
6882

6983
let mut outputs = Vec::new();

gen/cmd/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ mod output;
1313
mod syntax;
1414

1515
use crate::gen::error::{report, Result};
16-
use crate::gen::{fs, include};
16+
use crate::gen::fs;
17+
use crate::gen::include::{self, Include};
1718
use crate::output::Output;
1819
use std::io::{self, Write};
1920
use std::path::PathBuf;
@@ -24,7 +25,7 @@ struct Opt {
2425
input: Option<PathBuf>,
2526
header: bool,
2627
cxx_impl_annotations: Option<String>,
27-
include: Vec<String>,
28+
include: Vec<Include>,
2829
outputs: Vec<Output>,
2930
}
3031

gen/lib/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ mod gen;
2020
mod syntax;
2121

2222
pub use crate::error::Error;
23-
pub use crate::gen::include::HEADER;
23+
pub use crate::gen::include::{Include, HEADER};
2424
pub use crate::gen::{GeneratedCode, Opt};
25+
pub use crate::syntax::IncludeKind;
2526
use proc_macro2::TokenStream;
2627

2728
/// Generate C++ bindings code from a Rust token stream. This should be a Rust

gen/src/include.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ fn find_line(mut offset: usize, line: &str) -> Option<usize> {
4949
}
5050
}
5151

52-
#[derive(PartialEq)]
52+
/// A header to #include.
53+
///
54+
/// The cxxbridge tool does not parse or even require the given paths to exist;
55+
/// they simply go into the generated C++ code as #include lines.
56+
#[derive(Clone, PartialEq, Debug)]
5357
pub struct Include {
58+
/// The header's path, not including the enclosing quotation marks or angle
59+
/// brackets.
5460
pub path: String,
61+
/// Whether to emit `#include "path"` or `#include <path>`.
5562
pub kind: IncludeKind,
5663
}
5764

@@ -82,12 +89,9 @@ impl Includes {
8289
}
8390
}
8491

85-
impl<'a> Extend<&'a String> for Includes {
86-
fn extend<I: IntoIterator<Item = &'a String>>(&mut self, iter: I) {
87-
self.custom.extend(iter.into_iter().map(|path| Include {
88-
path: path.clone(),
89-
kind: IncludeKind::Quoted,
90-
}));
92+
impl<'a> Extend<&'a Include> for Includes {
93+
fn extend<I: IntoIterator<Item = &'a Include>>(&mut self, iter: I) {
94+
self.custom.extend(iter.into_iter().cloned());
9195
}
9296
}
9397

gen/src/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod write;
1111
pub(super) use self::error::Error;
1212
use self::error::{format_err, Result};
1313
use self::file::File;
14+
use self::include::Include;
1415
use crate::syntax::report::Errors;
1516
use crate::syntax::{self, check, Types};
1617
use std::path::Path;
@@ -34,7 +35,7 @@ pub struct Opt {
3435
/// Any additional headers to #include. The cxxbridge tool does not parse or
3536
/// even require the given paths to exist; they simply go into the generated
3637
/// C++ code as #include lines.
37-
pub include: Vec<String>,
38+
pub include: Vec<Include>,
3839
/// Optional annotation for implementations of C++ function wrappers that
3940
/// may be exposed to Rust. You may for example need to provide
4041
/// `__declspec(dllexport)` or `__attribute__((visibility("default")))` if

syntax/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ pub struct Include {
5252
pub end_span: Span,
5353
}
5454

55-
#[derive(Copy, Clone, PartialEq)]
55+
/// Whether to emit `#include "path"` or `#include <path>`.
56+
#[derive(Copy, Clone, PartialEq, Debug)]
5657
pub enum IncludeKind {
57-
Quoted, // #include "quoted/path/to"
58-
Bracketed, // #include <bracketed/path/to>
58+
/// `#include "quoted/path/to"`
59+
Quoted,
60+
/// `#include <bracketed/path/to>`
61+
Bracketed,
5962
}
6063

6164
pub struct ExternType {

0 commit comments

Comments
 (0)