Skip to content

Commit 8fbf478

Browse files
committed
Move some utility functions from the webidl crate into the backend crate
1 parent eb04d15 commit 8fbf478

File tree

5 files changed

+80
-84
lines changed

5 files changed

+80
-84
lines changed

crates/backend/src/ast.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
22
use quote::ToTokens;
33
use shared;
4-
use std::iter::FromIterator;
54
use syn;
5+
use util;
66

77
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
88
#[derive(Default)]
@@ -416,20 +416,7 @@ impl Program {
416416
} else if let Some(cls) = wasm.opts.static_method_of() {
417417
let class = cls.to_string();
418418
let kind = MethodKind::Static;
419-
420-
let segments = syn::punctuated::Punctuated::from_iter(Some(syn::PathSegment {
421-
ident: cls.clone(),
422-
arguments: syn::PathArguments::None,
423-
}));
424-
425-
let ty = syn::Type::Path(syn::TypePath {
426-
qself: None,
427-
path: syn::Path {
428-
leading_colon: None,
429-
segments,
430-
},
431-
});
432-
419+
let ty = util::ident_ty(cls.clone());
433420
ImportFunctionKind::Method { class, ty, kind }
434421
} else if wasm.opts.constructor() {
435422
let class = match wasm.ret {

crates/backend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ extern crate wasm_bindgen_shared as shared;
1212

1313
pub mod ast;
1414
mod codegen;
15+
pub mod util;

crates/backend/src/util.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::iter::FromIterator;
2+
3+
use ast;
4+
use proc_macro2::{self, Ident};
5+
use syn;
6+
7+
fn is_rust_keyword(name: &str) -> bool {
8+
match name {
9+
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
10+
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
11+
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
12+
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
13+
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
14+
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
15+
| "yield" | "bool" | "_" => true,
16+
_ => false,
17+
}
18+
}
19+
20+
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
21+
pub fn rust_ident(name: &str) -> Ident {
22+
if is_rust_keyword(name) {
23+
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
24+
} else {
25+
raw_ident(name)
26+
}
27+
}
28+
29+
// Create an `Ident` without checking to see if it conflicts with a Rust
30+
// keyword.
31+
pub fn raw_ident(name: &str) -> Ident {
32+
Ident::new(name, proc_macro2::Span::call_site())
33+
}
34+
35+
/// Create a path type from the given segments. For example an iterator yielding
36+
/// the idents `[foo, bar, baz]` will result in the path type `foo::bar::baz`.
37+
pub fn simple_path_ty<I>(segments: I) -> syn::Type
38+
where
39+
I: IntoIterator<Item = Ident>,
40+
{
41+
let segments: Vec<_> = segments
42+
.into_iter()
43+
.map(|i| syn::PathSegment {
44+
ident: i,
45+
arguments: syn::PathArguments::None,
46+
})
47+
.collect();
48+
49+
syn::TypePath {
50+
qself: None,
51+
path: syn::Path {
52+
leading_colon: None,
53+
segments: syn::punctuated::Punctuated::from_iter(segments),
54+
},
55+
}.into()
56+
}
57+
58+
pub fn ident_ty(ident: Ident) -> syn::Type {
59+
simple_path_ty(Some(ident))
60+
}
61+
62+
pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
63+
ast::Import {
64+
module: None,
65+
version: None,
66+
js_namespace: None,
67+
kind: ast::ImportKind::Function(function),
68+
}
69+
}

crates/webidl/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ extern crate syn;
1818
extern crate wasm_bindgen_backend as backend;
1919
extern crate webidl;
2020

21+
mod util;
22+
2123
use std::fs;
2224
use std::io::{self, Read};
23-
use std::iter;
2425
use std::path::Path;
2526

27+
use backend::util::{ident_ty, rust_ident, wrap_import_function};
2628
use failure::ResultExt;
2729
use quote::ToTokens;
2830

29-
mod util;
30-
3131
use util::{
32-
create_basic_method, create_function, create_getter, create_setter, ident_ty, rust_ident,
33-
webidl_ty_to_syn_ty, wrap_import_function, TypePosition,
32+
create_basic_method, create_function, create_getter, create_setter, webidl_ty_to_syn_ty,
33+
TypePosition,
3434
};
3535

3636
/// Either `Ok(t)` or `Err(failure::Error)`.

crates/webidl/src/util.rs

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,12 @@
1-
use std::iter::{self, FromIterator};
1+
use std::iter;
22

33
use backend;
4+
use backend::util::{ident_ty, raw_ident, rust_ident, simple_path_ty};
45
use heck::SnakeCase;
5-
use proc_macro2::{self, Ident};
6+
use proc_macro2::Ident;
67
use syn;
78
use webidl;
89

9-
fn is_rust_keyword(name: &str) -> bool {
10-
match name {
11-
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
12-
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
13-
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
14-
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
15-
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
16-
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
17-
| "yield" | "bool" | "_" => true,
18-
_ => false,
19-
}
20-
}
21-
22-
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
23-
pub fn rust_ident(name: &str) -> Ident {
24-
if is_rust_keyword(name) {
25-
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
26-
} else {
27-
raw_ident(name)
28-
}
29-
}
30-
31-
// Create an `Ident` without checking to see if it conflicts with a Rust
32-
// keyword.
33-
fn raw_ident(name: &str) -> Ident {
34-
Ident::new(name, proc_macro2::Span::call_site())
35-
}
36-
37-
fn simple_path_ty<I>(segments: I) -> syn::Type
38-
where
39-
I: IntoIterator<Item = Ident>,
40-
{
41-
let segments: Vec<_> = segments
42-
.into_iter()
43-
.map(|i| syn::PathSegment {
44-
ident: i,
45-
arguments: syn::PathArguments::None,
46-
})
47-
.collect();
48-
49-
syn::TypePath {
50-
qself: None,
51-
path: syn::Path {
52-
leading_colon: None,
53-
segments: syn::punctuated::Punctuated::from_iter(segments),
54-
},
55-
}.into()
56-
}
57-
58-
pub fn ident_ty(ident: Ident) -> syn::Type {
59-
simple_path_ty(Some(ident))
60-
}
61-
6210
fn shared_ref(ty: syn::Type) -> syn::Type {
6311
syn::TypeReference {
6412
and_token: Default::default(),
@@ -337,12 +285,3 @@ pub fn create_setter(
337285
vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(name)))],
338286
)
339287
}
340-
341-
pub fn wrap_import_function(function: backend::ast::ImportFunction) -> backend::ast::Import {
342-
backend::ast::Import {
343-
module: None,
344-
version: None,
345-
js_namespace: None,
346-
kind: backend::ast::ImportKind::Function(function),
347-
}
348-
}

0 commit comments

Comments
 (0)