Add a Namespace type in gen

This commit is contained in:
David Tolnay 2020-03-29 20:58:46 -07:00
parent 3c19e53ad1
commit 754e21c678
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 55 additions and 8 deletions

View File

@ -3,10 +3,12 @@
mod error;
pub(super) mod include;
mod namespace;
pub(super) mod out;
mod write;
use self::error::format_err;
use self::namespace::Namespace;
use self::out::OutFile;
use crate::syntax::{self, check, ident, Types};
use quote::quote;
@ -32,7 +34,7 @@ pub(super) enum Error {
}
struct Input {
namespace: Vec<String>,
namespace: Namespace,
module: Vec<Item>,
}
@ -86,10 +88,9 @@ fn find_bridge_mod(syntax: File) -> Result<Input> {
)));
}
};
return Ok(Input {
namespace: parse_args(attr)?,
module,
});
let namespace_segments = parse_args(attr)?;
let namespace = Namespace::new(namespace_segments);
return Ok(Input { namespace, module });
}
}
}

44
gen/namespace.rs Normal file
View File

@ -0,0 +1,44 @@
use std::fmt::{self, Display};
use std::slice::Iter;
use std::vec::IntoIter;
#[derive(Clone)]
pub struct Namespace {
segments: Vec<String>,
}
impl Namespace {
pub fn new(segments: Vec<String>) -> Self {
Namespace { segments }
}
pub fn iter(&self) -> Iter<String> {
self.segments.iter()
}
}
impl Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for segment in self {
f.write_str(segment)?;
f.write_str("$")?;
}
Ok(())
}
}
impl<'a> IntoIterator for &'a Namespace {
type Item = &'a String;
type IntoIter = Iter<'a, String>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl IntoIterator for Namespace {
type Item = String;
type IntoIter = IntoIter<String>;
fn into_iter(self) -> Self::IntoIter {
self.segments.into_iter()
}
}

View File

@ -1,8 +1,9 @@
use crate::gen::include::Includes;
use crate::gen::namespace::Namespace;
use std::fmt::{self, Arguments, Write};
pub(crate) struct OutFile {
pub namespace: Vec<String>,
pub namespace: Namespace,
pub header: bool,
pub include: Includes,
content: Vec<u8>,
@ -11,7 +12,7 @@ pub(crate) struct OutFile {
}
impl OutFile {
pub fn new(namespace: Vec<String>, header: bool) -> Self {
pub fn new(namespace: Namespace, header: bool) -> Self {
OutFile {
namespace,
header,

View File

@ -1,3 +1,4 @@
use crate::gen::namespace::Namespace;
use crate::gen::out::OutFile;
use crate::gen::{include, Opt};
use crate::syntax::atom::Atom::{self, *};
@ -5,7 +6,7 @@ use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
use proc_macro2::Ident;
pub(super) fn gen(
namespace: Vec<String>,
namespace: Namespace,
apis: &[Api],
types: &Types,
opt: Opt,