Skip to content

Commit fcd8f46

Browse files
committed
Data structure for source file
1 parent 05ef6ff commit fcd8f46

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

gen/src/file.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use syn::parse::{Parse, ParseStream, Result};
2+
use syn::{Attribute, Item};
3+
4+
pub struct File {
5+
pub attrs: Vec<Attribute>,
6+
pub items: Vec<Item>,
7+
}
8+
9+
impl Parse for File {
10+
fn parse(input: ParseStream) -> Result<Self> {
11+
let attrs = input.call(Attribute::parse_inner)?;
12+
13+
let mut items = Vec::new();
14+
while !input.is_empty() {
15+
items.push(input.parse()?);
16+
}
17+
18+
Ok(File { attrs, items })
19+
}
20+
}

gen/src/find.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::gen::{Error, Input, Result};
1+
use crate::gen::{Error, File, Input, Result};
22
use crate::syntax::namespace::Namespace;
3-
use syn::{Attribute, File, Item};
3+
use syn::{Attribute, Item};
44

55
pub(super) fn find_bridge_mod(syntax: File) -> Result<Input> {
66
match scan(syntax.items)? {

gen/src/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// the cxxbridge CLI command.
33

44
mod error;
5+
mod file;
56
mod find;
67
pub(super) mod include;
78
pub(super) mod out;
@@ -11,6 +12,7 @@ mod write;
1112
mod tests;
1213

1314
use self::error::{format_err, Error, Result};
15+
use self::file::File;
1416
use crate::syntax::namespace::Namespace;
1517
use crate::syntax::report::Errors;
1618
use crate::syntax::{self, check, Types};
@@ -56,7 +58,7 @@ fn generate_from_path(path: &Path, opt: Opt, header: bool) -> Vec<u8> {
5658
fn generate(source: &str, opt: Opt, header: bool) -> Result<Vec<u8>> {
5759
proc_macro2::fallback::force();
5860
let ref mut errors = Errors::new();
59-
let syntax = syn::parse_file(&source)?;
61+
let syntax: File = syn::parse_str(source)?;
6062
let bridge = find::find_bridge_mod(syntax)?;
6163
let ref namespace = bridge.namespace;
6264
let ref apis = syntax::parse_items(errors, bridge.module);

0 commit comments

Comments
 (0)