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

corrected find_enum and find_message for keyword identifiers #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions codegen/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ pub fn sanitize_keyword(ident: &mut String) {
}).collect::<Vec<_>>().join(".");
}
}

// get the correct identifier (sanitize if needed, otherwise do nothing)
// (disclaimer) I'm new to rust, so this can probably be better
pub fn get_ident(ident: &String) -> String {
if !ident.contains('.') {
if RUST_KEYWORDS.contains(&&ident[..]) {
ident.clone() + &"_pb".to_string()
}
else {
ident.clone()
}
}
else {
ident.split('.').map(|s| get_ident(&s.to_string())).collect::<Vec<_>>().join(".")
}
}
20 changes: 10 additions & 10 deletions codegen/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::File;

use errors::{Result, Error, ErrorKind};
use parser::file_descriptor;
use keywords::sanitize_keyword;
use keywords::{get_ident, sanitize_keyword};

fn sizeof_varint(v: u32) -> usize {
match v {
Expand Down Expand Up @@ -161,11 +161,11 @@ impl FieldType {
FieldType::Message(ref m) => {
let mut found = match m.rfind('.') {
Some(p) => {
let package = &m[..p];
let name = &m[(p + 1)..];
msgs.iter().find(|m| m.package == package && m.name == name)
let package = get_ident(&String::from(&m[..p]));
let name = get_ident(&String::from(&m[(p + 1)..]));
msgs.iter().find(|m| get_ident(&m.package) == package && get_ident(&m.name) == name)
},
None => msgs.iter().find(|m2| m2.name == &m[..]),
None => msgs.iter().find(|m2| get_ident(&m2.name) == get_ident(&m)),
};

if found.is_none() {
Expand All @@ -188,11 +188,11 @@ impl FieldType {
FieldType::Enum(ref m) => {
let mut found = match m.rfind('.') {
Some(p) => {
let package = &m[..p];
let name = &m[(p + 1)..];
enums.iter().find(|m| m.package == package && m.name == name)
let package = get_ident(&String::from(&m[..p]));
let name = get_ident(&String::from(&m[(p + 1)..]));
enums.iter().find(|m| get_ident(&m.package) == package && get_ident(&m.name) == name)
},
None => enums.iter().find(|m2| m2.name == &m[..]),
None => enums.iter().find(|m2| get_ident(&m2.name) == get_ident(&m)),
};

if found.is_none() {
Expand Down Expand Up @@ -1433,7 +1433,7 @@ fn update_mod_file(path: &Path) -> Result<()> {
let mut file = path.to_path_buf();
use std::fs::OpenOptions;
use std::io::prelude::*;

let name = file.file_stem().unwrap().to_string_lossy().to_string();
file.pop();
file.push("mod.rs");
Expand Down