Skip to content

Commit d60c07b

Browse files
committed
Merge pull request 370 from adetaylor/allow-namespace-override
2 parents 7b14585 + c871343 commit d60c07b

27 files changed

+1167
-448
lines changed

gen/src/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(super) mod error;
66
mod file;
77
pub(super) mod fs;
88
pub(super) mod include;
9+
mod namespace_organizer;
910
pub(super) mod out;
1011
mod write;
1112

@@ -113,23 +114,23 @@ pub(super) fn generate(syntax: File, opt: &Opt) -> Result<GeneratedCode> {
113114
.ok_or(Error::NoBridgeMod)?;
114115
let ref namespace = bridge.namespace;
115116
let trusted = bridge.unsafety.is_some();
116-
let ref apis = syntax::parse_items(errors, bridge.content, trusted);
117+
let ref apis = syntax::parse_items(errors, bridge.content, trusted, namespace);
117118
let ref types = Types::collect(errors, apis);
118119
check::precheck(errors, apis, opt);
119120
errors.propagate()?;
120-
check::typecheck(errors, namespace, apis, types);
121+
check::typecheck(errors, apis, types);
121122
errors.propagate()?;
122123
// Some callers may wish to generate both header and C++
123124
// from the same token stream to avoid parsing twice. But others
124125
// only need to generate one or the other.
125126
Ok(GeneratedCode {
126127
header: if opt.gen_header {
127-
write::gen(namespace, apis, types, opt, true).content()
128+
write::gen(apis, types, opt, true).content()
128129
} else {
129130
Vec::new()
130131
},
131132
implementation: if opt.gen_implementation {
132-
write::gen(namespace, apis, types, opt, false).content()
133+
write::gen(apis, types, opt, false).content()
133134
} else {
134135
Vec::new()
135136
},

gen/src/namespace_organizer.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::syntax::Api;
2+
use proc_macro2::Ident;
3+
use std::collections::BTreeMap;
4+
5+
pub(crate) struct NamespaceEntries<'a> {
6+
pub(crate) entries: Vec<&'a Api>,
7+
pub(crate) children: BTreeMap<&'a Ident, NamespaceEntries<'a>>,
8+
}
9+
10+
pub(crate) fn sort_by_namespace(apis: &[Api]) -> NamespaceEntries {
11+
let api_refs = apis.iter().collect::<Vec<_>>();
12+
sort_by_inner_namespace(api_refs, 0)
13+
}
14+
15+
fn sort_by_inner_namespace(apis: Vec<&Api>, depth: usize) -> NamespaceEntries {
16+
let mut root = NamespaceEntries {
17+
entries: Vec::new(),
18+
children: BTreeMap::new(),
19+
};
20+
21+
let mut kids_by_child_ns = BTreeMap::new();
22+
for api in apis {
23+
if let Some(ns) = api.get_namespace() {
24+
let first_ns_elem = ns.iter().nth(depth);
25+
if let Some(first_ns_elem) = first_ns_elem {
26+
let list = kids_by_child_ns.entry(first_ns_elem).or_insert(Vec::new());
27+
list.push(api);
28+
continue;
29+
}
30+
}
31+
root.entries.push(api);
32+
}
33+
34+
for (k, v) in kids_by_child_ns.into_iter() {
35+
root.children
36+
.insert(k, sort_by_inner_namespace(v, depth + 1));
37+
}
38+
39+
root
40+
}

gen/src/out.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use crate::gen::include::Includes;
2-
use crate::syntax::namespace::Namespace;
32
use std::cell::RefCell;
43
use std::fmt::{self, Arguments, Write};
54

65
pub(crate) struct OutFile {
7-
pub namespace: Namespace,
86
pub header: bool,
97
pub include: Includes,
108
pub front: Content,
@@ -18,9 +16,8 @@ pub struct Content {
1816
}
1917

2018
impl OutFile {
21-
pub fn new(namespace: Namespace, header: bool) -> Self {
19+
pub fn new(header: bool) -> Self {
2220
OutFile {
23-
namespace,
2421
header,
2522
include: Includes::new(),
2623
front: Content::new(),

0 commit comments

Comments
 (0)