default method name to type name

This commit is contained in:
Aleksey Kladov 2018-08-31 15:10:37 +03:00
parent 05a9d42f54
commit 78d60a549d
4 changed files with 51 additions and 84 deletions

View file

@ -78,8 +78,7 @@ impl<'a> AstNode<'a> for Attr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> Attr<'a> { impl<'a> Attr<'a> {pub fn value(self) -> Option<TokenTree<'a>> {
pub fn value(self) -> Option<TokenTree<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -141,7 +140,6 @@ impl<'a> Block<'a> {
pub fn statements(self) -> impl Iterator<Item = Stmt<'a>> + 'a { pub fn statements(self) -> impl Iterator<Item = Stmt<'a>> + 'a {
super::children(self) super::children(self)
} }
pub fn expr(self) -> Option<Expr<'a>> { pub fn expr(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -163,8 +161,7 @@ impl<'a> AstNode<'a> for BlockExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> BlockExpr<'a> { impl<'a> BlockExpr<'a> {pub fn block(self) -> Option<Block<'a>> {
pub fn block(self) -> Option<Block<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -203,11 +200,9 @@ impl<'a> AstNode<'a> for CallExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> CallExpr<'a> { impl<'a> CallExpr<'a> {pub fn expr(self) -> Option<Expr<'a>> {
pub fn expr(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn arg_list(self) -> Option<ArgList<'a>> { pub fn arg_list(self) -> Option<ArgList<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -247,11 +242,9 @@ impl<'a> AstNode<'a> for Condition<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> Condition<'a> { impl<'a> Condition<'a> {pub fn pat(self) -> Option<Pat<'a>> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn expr(self) -> Option<Expr<'a>> { pub fn expr(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -466,8 +459,7 @@ impl<'a> AstNode<'a> for ExprStmt<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> ExprStmt<'a> { impl<'a> ExprStmt<'a> {pub fn expr(self) -> Option<Expr<'a>> {
pub fn expr(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -545,15 +537,12 @@ impl<'a> AstNode<'a> for FnDef<'a> {
impl<'a> ast::NameOwner<'a> for FnDef<'a> {} impl<'a> ast::NameOwner<'a> for FnDef<'a> {}
impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {}
impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {} impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {}
impl<'a> FnDef<'a> { impl<'a> FnDef<'a> {pub fn param_list(self) -> Option<ParamList<'a>> {
pub fn param_list(self) -> Option<ParamList<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn body(self) -> Option<Block<'a>> { pub fn body(self) -> Option<Block<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn ret_type(self) -> Option<RetType<'a>> { pub fn ret_type(self) -> Option<RetType<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -594,11 +583,9 @@ impl<'a> AstNode<'a> for ForExpr<'a> {
} }
impl<'a> ast::LoopBodyOwner<'a> for ForExpr<'a> {} impl<'a> ast::LoopBodyOwner<'a> for ForExpr<'a> {}
impl<'a> ForExpr<'a> { impl<'a> ForExpr<'a> {pub fn pat(self) -> Option<Pat<'a>> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn iterable(self) -> Option<Expr<'a>> { pub fn iterable(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -638,8 +625,7 @@ impl<'a> AstNode<'a> for IfExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> IfExpr<'a> { impl<'a> IfExpr<'a> {pub fn condition(self) -> Option<Condition<'a>> {
pub fn condition(self) -> Option<Condition<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -732,11 +718,9 @@ impl<'a> AstNode<'a> for LambdaExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> LambdaExpr<'a> { impl<'a> LambdaExpr<'a> {pub fn param_list(self) -> Option<ParamList<'a>> {
pub fn param_list(self) -> Option<ParamList<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn body(self) -> Option<Expr<'a>> { pub fn body(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -758,11 +742,9 @@ impl<'a> AstNode<'a> for LetStmt<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> LetStmt<'a> { impl<'a> LetStmt<'a> {pub fn pat(self) -> Option<Pat<'a>> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn initializer(self) -> Option<Expr<'a>> { pub fn initializer(self) -> Option<Expr<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -802,8 +784,7 @@ impl<'a> AstNode<'a> for LifetimeParam<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> LifetimeParam<'a> { impl<'a> LifetimeParam<'a> {pub fn lifetime(self) -> Option<Lifetime<'a>> {
pub fn lifetime(self) -> Option<Lifetime<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -1162,8 +1143,7 @@ impl<'a> AstNode<'a> for Param<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> Param<'a> { impl<'a> Param<'a> {pub fn pat(self) -> Option<Pat<'a>> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -1291,8 +1271,7 @@ impl<'a> AstNode<'a> for Path<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> Path<'a> { impl<'a> Path<'a> {pub fn segment(self) -> Option<PathSegment<'a>> {
pub fn segment(self) -> Option<PathSegment<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -1349,8 +1328,7 @@ impl<'a> AstNode<'a> for PathSegment<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> PathSegment<'a> { impl<'a> PathSegment<'a> {pub fn name_ref(self) -> Option<NameRef<'a>> {
pub fn name_ref(self) -> Option<NameRef<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -2012,8 +1990,7 @@ impl<'a> AstNode<'a> for UseItem<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> UseItem<'a> { impl<'a> UseItem<'a> {pub fn use_tree(self) -> Option<UseTree<'a>> {
pub fn use_tree(self) -> Option<UseTree<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }
@ -2034,11 +2011,9 @@ impl<'a> AstNode<'a> for UseTree<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<'a> UseTree<'a> { impl<'a> UseTree<'a> {pub fn path(self) -> Option<Path<'a>> {
pub fn path(self) -> Option<Path<'a>> {
super::child_opt(self) super::child_opt(self)
} }
pub fn use_tree_list(self) -> Option<UseTreeList<'a>> { pub fn use_tree_list(self) -> Option<UseTreeList<'a>> {
super::child_opt(self) super::child_opt(self)
} }
@ -2101,8 +2076,7 @@ impl<'a> AstNode<'a> for WhileExpr<'a> {
} }
impl<'a> ast::LoopBodyOwner<'a> for WhileExpr<'a> {} impl<'a> ast::LoopBodyOwner<'a> for WhileExpr<'a> {}
impl<'a> WhileExpr<'a> { impl<'a> WhileExpr<'a> {pub fn condition(self) -> Option<Condition<'a>> {
pub fn condition(self) -> Option<Condition<'a>> {
super::child_opt(self) super::child_opt(self)
} }
} }

View file

@ -65,8 +65,15 @@ impl<'a> {{ node }}<'a> {
{%- if methods.options -%} {%- if methods.options -%}
{%- for m in methods.options -%} {%- for m in methods.options -%}
{%- if m is string -%}
{%- set method_name = m | snake -%}
{%- set ChildName = m %}
{%- else -%}
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 %} {%- set ChildName = m.1 %}
{%- endif -%}
pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> {
super::child_opt(self) super::child_opt(self)
} }

View file

@ -250,11 +250,7 @@ Grammar(
"TypeParamsOwner", "TypeParamsOwner",
"AttrsOwner", "AttrsOwner",
], ],
options: [ options: [ "ParamList", ["body", "Block"], "RetType" ],
["param_list", "ParamList"],
["body", "Block"],
["ret_type", "RetType"]
],
), ),
"RetType": (), "RetType": (),
"StructDef": ( "StructDef": (
@ -346,12 +342,12 @@ Grammar(
"PathExpr": (), "PathExpr": (),
"LambdaExpr": ( "LambdaExpr": (
options: [ options: [
["param_list", "ParamList"], "ParamList",
["body", "Expr"], ["body", "Expr"],
] ]
), ),
"IfExpr": ( "IfExpr": (
options: [ ["condition", "Condition"] ] options: [ "Condition" ]
), ),
"LoopExpr": ( "LoopExpr": (
traits: ["LoopBodyOwner"], traits: ["LoopBodyOwner"],
@ -359,21 +355,19 @@ Grammar(
"ForExpr": ( "ForExpr": (
traits: ["LoopBodyOwner"], traits: ["LoopBodyOwner"],
options: [ options: [
["pat", "Pat"], "Pat",
["iterable", "Expr"], ["iterable", "Expr"],
] ]
), ),
"WhileExpr": ( "WhileExpr": (
traits: ["LoopBodyOwner"], traits: ["LoopBodyOwner"],
options: [ options: [ "Condition" ]
["condition", "Condition"],
]
), ),
"ContinueExpr": (), "ContinueExpr": (),
"BreakExpr": (), "BreakExpr": (),
"Label": (), "Label": (),
"BlockExpr": ( "BlockExpr": (
options: [ ["block", "Block"] ] options: [ "Block" ]
), ),
"ReturnExpr": (), "ReturnExpr": (),
"MatchExpr": (), "MatchExpr": (),
@ -384,10 +378,7 @@ Grammar(
"NamedFieldList": (), "NamedFieldList": (),
"NamedField": (), "NamedField": (),
"CallExpr": ( "CallExpr": (
options: [ options: [ "Expr", "ArgList" ]
[ "expr", "Expr" ],
[ "arg_list", "ArgList" ],
]
), ),
"IndexExpr": (), "IndexExpr": (),
"MethodCallExpr": (), "MethodCallExpr": (),
@ -474,7 +465,7 @@ Grammar(
] ]
), ),
"TypeParam": ( traits: ["NameOwner"] ), "TypeParam": ( traits: ["NameOwner"] ),
"LifetimeParam": ( options: [ ["lifetime", "Lifetime"] ] ), "LifetimeParam": ( options: [ "Lifetime" ] ),
"Lifetime": (), "Lifetime": (),
"WhereClause": (), "WhereClause": (),
"ExprStmt": ( "ExprStmt": (
@ -485,16 +476,13 @@ Grammar(
["initializer", "Expr"], ["initializer", "Expr"],
]), ]),
"Condition": ( "Condition": (
options: [ options: [ "Pat", "Expr" ]
[ "pat", "Pat" ],
[ "expr", "Expr" ],
]
), ),
"Stmt": ( "Stmt": (
enum: ["ExprStmt", "LetStmt"], enum: ["ExprStmt", "LetStmt"],
), ),
"Block": ( "Block": (
options: [["expr", "Expr"]], options: [ "Expr" ],
collections: [ collections: [
["statements", "Stmt"], ["statements", "Stmt"],
] ]
@ -505,16 +493,13 @@ Grammar(
] ]
), ),
"Param": ( "Param": (
options: [["pat", "Pat"]], options: [ "Pat" ],
), ),
"UseItem": ( "UseItem": (
options: [["use_tree", "UseTree"]] options: [ "UseTree" ]
), ),
"UseTree": ( "UseTree": (
options: [ options: [ "Path", "UseTreeList" ]
["path", "Path"],
["use_tree_list", "UseTreeList"],
]
), ),
"UseTreeList": ( "UseTreeList": (
collections: [["use_trees", "UseTree"]] collections: [["use_trees", "UseTree"]]
@ -531,9 +516,7 @@ Grammar(
] ]
), ),
"PathSegment": ( "PathSegment": (
options: [ options: [ "NameRef" ]
["name_ref", "NameRef"]
]
), ),
}, },
) )

View file

@ -10,7 +10,7 @@ extern crate commandspec;
extern crate heck; extern crate heck;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, SubCommand};
use heck::{CamelCase, ShoutySnakeCase}; use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs, fs,
@ -91,6 +91,9 @@ fn render_template(template: &str) -> Result<String> {
tera.register_filter("camel", |arg, _| { tera.register_filter("camel", |arg, _| {
Ok(arg.as_str().unwrap().to_camel_case().into()) Ok(arg.as_str().unwrap().to_camel_case().into())
}); });
tera.register_filter("snake", |arg, _| {
Ok(arg.as_str().unwrap().to_snake_case().into())
});
tera.register_filter("SCREAM", |arg, _| { tera.register_filter("SCREAM", |arg, _| {
Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
}); });