Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support *not* generating protobuf structs #2

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! These functions panic liberally, they are designed to be used from build
//! scripts, not in production.

pub use crate::wrapper::GenOpt;
use regex::Regex;
use std::fs::read_dir;
use std::fs::File;
Expand Down Expand Up @@ -93,9 +94,9 @@ fn rustfmt(file_path: &Path) {
}
}

pub fn generate_wrappers<T: AsRef<str>>(file_names: &[T], out_dir: &str) {
pub fn generate_wrappers<T: AsRef<str>>(file_names: &[T], out_dir: &str, gen_opt: GenOpt) {
for file in file_names {
let gen = wrapper::WrapperGen::new(file.as_ref());
let gen = wrapper::WrapperGen::new(file.as_ref(), gen_opt);
gen.write(out_dir);
}
}
Expand Down
52 changes: 39 additions & 13 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ use syn::{
Type,
};

#[derive(Eq, PartialEq, Clone, Copy)]
pub enum GenOpt {
All,
NoImplMessage,
}

pub struct WrapperGen {
input: String,
name: String,
gen_opt: GenOpt,
}

impl WrapperGen {
pub fn new(file_name: &str) -> WrapperGen {
pub fn new(file_name: &str, gen_opt: GenOpt) -> WrapperGen {
let input = String::from_utf8(
fs::read(file_name).unwrap_or_else(|_| panic!("Could not read {}", file_name)),
)
.expect("File not utf8");
WrapperGen {
input,
gen_opt,
name: format!(
"wrapper_{}",
&file_name[file_name.rfind('/').map(|i| i + 1).unwrap_or(0)..]
Expand All @@ -44,18 +52,23 @@ impl WrapperGen {
W: Write,
{
let file = ::syn::parse_file(&self.input).expect("Could not parse file");
generate_from_items(&file.items, "", buf)
generate_from_items(&file.items, self.gen_opt, "", buf)
}
}

fn generate_from_items<W>(items: &[Item], prefix: &str, buf: &mut W) -> Result<(), io::Error>
fn generate_from_items<W>(
items: &[Item],
gen_opt: GenOpt,
prefix: &str,
buf: &mut W,
) -> Result<(), io::Error>
where
W: Write,
{
for item in items {
if let Item::Struct(item) = item {
if is_message(&item.attrs) {
generate_struct(item, prefix, buf)?;
generate_struct(item, gen_opt, prefix, buf)?;
}
} else if let Item::Enum(item) = item {
if is_enum(&item.attrs) {
Expand All @@ -64,14 +77,19 @@ where
} else if let Item::Mod(m) = item {
if let Some(ref content) = m.content {
let prefix = format!("{}{}::", prefix, m.ident);
generate_from_items(&content.1, &prefix, buf)?;
generate_from_items(&content.1, gen_opt, &prefix, buf)?;
}
}
}
Ok(())
}

fn generate_struct<W>(item: &ItemStruct, prefix: &str, buf: &mut W) -> Result<(), io::Error>
fn generate_struct<W>(
item: &ItemStruct,
gen_opt: GenOpt,
prefix: &str,
buf: &mut W,
) -> Result<(), io::Error>
where
W: Write,
{
Expand All @@ -88,7 +106,7 @@ where
.map(|m| m.write_methods(buf))
.collect::<Result<Vec<_>, _>>()?;
writeln!(buf, "}}")?;
generate_message_trait(&item.ident, prefix, buf)
generate_message_trait(&item.ident, prefix, buf, gen_opt)
}

fn generate_enum<W>(item: &ItemEnum, prefix: &str, buf: &mut W) -> Result<(), io::Error>
Expand Down Expand Up @@ -120,10 +138,21 @@ where
)
}

fn generate_message_trait<W>(name: &Ident, prefix: &str, buf: &mut W) -> Result<(), io::Error>
fn generate_message_trait<W>(
name: &Ident,
prefix: &str,
buf: &mut W,
gen_opt: GenOpt,
) -> Result<(), io::Error>
where
W: Write,
{
if gen_opt == GenOpt::NoImplMessage {
return writeln!(
buf,
"// impls for `::protobuf::*` stuffs are not generated."
);
}
write!(buf, "impl ::protobuf::Clear for {}{} {{", prefix, name)?;
writeln!(
buf,
Expand Down Expand Up @@ -286,7 +315,7 @@ impl FieldKind {
let unwrapped_type = match ty {
Type::Path(p) => {
let seg = p.path.segments.iter().last().unwrap();
assert!(seg.ident == "Option");
assert_eq!(seg.ident, "Option");
match &seg.arguments {
PathArguments::AngleBracketed(args) => match &args.args[0] {
GenericArgument::Type(ty) => ty.clone(),
Expand Down Expand Up @@ -337,10 +366,7 @@ impl FieldKind {
"self.{}.take().unwrap_or_else({}::default)",
result.name, unwrapped_type,
));
format!(
"<{} as ::protobuf::Message>::default_instance()",
unwrapped_type,
)
format!("{}::new_()", unwrapped_type)
}
FieldKind::Bytes => {
result.take = Some(format!(
Expand Down