-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from sgrif/sg-table-name
Allow structs to be annotated with `#[table_name="foo"]`
- Loading branch information
Showing
6 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use syntax::ast; | ||
use syntax::attr::AttrMetaMethods; | ||
use syntax::ext::base::ExtCtxt; | ||
use syntax::parse::token::str_to_ident; | ||
|
||
fn str_value_of_attr( | ||
cx: &mut ExtCtxt, | ||
attr: &ast::Attribute, | ||
name: &str, | ||
) -> Option<ast::Ident> { | ||
attr.value_str().map(|value| { | ||
str_to_ident(&value) | ||
}).or_else(|| { | ||
cx.span_err(attr.span(), | ||
&format!(r#"`{}` must be in the form `#[{}="something"]`"#, name, name)); | ||
None | ||
}) | ||
} | ||
|
||
pub fn str_value_of_attr_with_name( | ||
cx: &mut ExtCtxt, | ||
attrs: &[ast::Attribute], | ||
name: &str, | ||
) -> Option<ast::Ident> { | ||
attrs.iter() | ||
.find(|a| a.check_name(name)) | ||
.and_then(|a| str_value_of_attr(cx, &a, name)) | ||
} | ||
|
||
#[cfg(feature = "with-syntex")] | ||
pub fn strip_attributes(krate: ast::Crate) -> ast::Crate { | ||
use syntax::fold; | ||
|
||
struct StripAttributeFolder; | ||
|
||
impl fold::Folder for StripAttributeFolder { | ||
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> { | ||
if attr.check_name("table_name") { | ||
None | ||
} else { | ||
Some(attr) | ||
} | ||
} | ||
|
||
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { | ||
fold::noop_fold_mac(mac, self) | ||
} | ||
} | ||
|
||
fold::Folder::fold_crate(&mut StripAttributeFolder, krate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters