#seed #routing #cli #generation #debugging #file-read

app proto_seeder

使用 seed_routing 从路由创建文件的命令行工具

8 个版本

0.0.8 2021年2月26日
0.0.7 2021年2月26日

#23 in #file-read

MIT 许可证

110KB
2.5K SLoC

proto-seeder

A Cli prototype to integrate to seeder later. It generates files and methods for a Seed app from a Route enum.

描述

This proto cli should be able to parse a rust file and read the Route enum


 #[derive(Debug, PartialEq, Clone, RoutingModules)]
    #[modules_path = "pages"]
     pub enum Route {
         Other {
             id: String,
             children: Settings,
         },
         #[guard = "logged_user => admin_guard => not_authorized_view"]
         Admin { // will load module "admin.rs"
          // will load model.admin and as well
          // will check init has correct arguments
          // will check view has correct arguments
             query: IndexMap<String, String>,
         },
         #[guard = "logged_user => user_guard => not_logged_user_view"]
         Dashboard(DashboardRoutes), // will load module "dashboard"
         Profile { // will load module "profile"
             id: String,
         },
         #[guard = "logged_user => admin_guard => not_authorized_view"]
         #[view = " => my_stuff"]
         MyStuff,
         #[view = " => not_found"]
         #[default_route]
         NotFound,
         #[view = " => home"]
         #[as_path = ""]
         Root,
     }

示例

请参阅以下 lib.rs

您应该从以下命令在项目根目录下得到以下输出

proto_seeder ./src/lib.rs

如果转到 /src 并运行

proto_seeder lib.rs

将不会工作,因为 https://github.com/arn-the-long-beard/proto-seeder/issues/1

以下是示例的输出。

-> found 3 locals view to create
-> found 2 guards to create
-> found 3 modules to create
[+] finished parsing the file
[+] created folder ./examples/backbone_app/src/pages
[+] created file at ./examples/backbone_app/src/pages/mod.rs 
[+] updated ./examples/backbone_app/src/pages/mod.rs for import parent module => pub mod login;
pub mod dashboard;
pub mod admin;
[+] created file at ./examples/backbone_app/src/pages/login.rs 
[+] updated ./examples/backbone_app/src/pages/login.rs 
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub fn init()
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub struct Model{}
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub enum Routes{} 
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub enum Msg{}
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub fn update()
[+] updated ./examples/backbone_app/src/pages/login.rs for adding pub fn view()
[+] created file at ./examples/backbone_app/src/pages/dashboard.rs 
[+] updated ./examples/backbone_app/src/pages/dashboard.rs 
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub fn init()
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub struct Model{}
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub enum Routes{} 
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub enum Msg{}
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub fn update()
[+] updated ./examples/backbone_app/src/pages/dashboard.rs for adding pub fn view()
[+] created file at ./examples/backbone_app/src/pages/admin.rs 
[+] updated ./examples/backbone_app/src/pages/admin.rs 
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub fn init()
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub struct Model{}
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub enum Routes{} 
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub enum Msg{}
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub fn update()
[+] updated ./examples/backbone_app/src/pages/admin.rs for adding pub fn view()
[+] found file to update at ./examples/backbone_app/src/lib.rs 
[=>] No need to create view for route NotFound [ => ] as fn not_found ()
[+] found file to update at ./examples/backbone_app/src/lib.rs 
[+] updated ./examples/backbone_app/src/lib.rs for writing local view forbidden for route Forbidden
[+] updated ./examples/backbone_app/src/lib.rs for Added indentation
[+] found file to update at ./examples/backbone_app/src/lib.rs 
[=>] No need to create view for route Home [ => ] as fn home ()
[+] found file to update at ./examples/backbone_app/src/lib.rs 
[+] updated ./examples/backbone_app/src/lib.rs for writing local guard as guard
[+] updated ./examples/backbone_app/src/lib.rs for Added indentation
[=>] No need to create redirect forbidden for [ => ] guard ()
[+] found file to update at ./examples/backbone_app/src/lib.rs 
[+] updated ./examples/backbone_app/src/lib.rs for writing local guard as admin_guard
[+] updated ./examples/backbone_app/src/lib.rs for Added indentation
[+] updated ./examples/backbone_app/src/lib.rs for writing redirect for guard as forbidden_user
[+] updated ./examples/backbone_app/src/lib.rs for Added indentation
[=>] Created 4 new files
[=>] Updated 2 files
[=>] Ignored 0 files
▪▪▪▪▪ Done

您应该在 lib.rs 中也有以下新代码

fn not_found(model : &Model) -> Node<Msg>{div!["not_found"]}
fn forbidden(model : &Model) -> Node<Msg>{div!["forbidden"]}
fn guard(model : &Model) -> Option<bool> {log!("Write condition")}
fn admin_guard(model : &Model) -> Option<bool> {log!("Write condition")}
fn forbidden_user(model : &Model) -> Node<Msg>{div!["forbidden_user"]}

以及 4 个包含 TEA 代码的新文件和一个新的示例文件夹。

待办事项

  • 如果找不到 Routes,则发出抱怨(然后它将不执行任何操作)
  • 如果找不到 Model,则发出抱怨(然后它将不执行任何操作)
  • 从路由中提取 URL 有效负载(id_param、查询和子路由)或提取嵌套路由(如果有)作为单个变体元组
  • 提取路由并显示统计信息(将创建多少视图和文件)
  • 创建正确的子目录(?)并使用正确的名称 -> #[modules_path]
  • 当路由是模块时,创建正确的文件并使用步骤 2 中提取的正确有效负载创建视图和初始化 -> 完成
  • 如果路由不是模块,则通过调用适当的模型/属性创建本地视图 -> #[view = "Model/prop => local_view"]
  • 使用调用适当的模型/属性创建本地守卫 -> #[guard = "Model/prop => guard => callback_view"]
  • 添加构建命令以测试生成的代码是否可以编译
  • 在根文件中添加模块,其中解析初始 Routes

以后

  • 检测未来的文件是否已存在

  • 如果未来的文件已存在,尝试将其 Routes enum 递归地应用于命令行?

  • 检查内容是否已存在,如果存在则不会添加

  • 检查本地内容(本地视图和守卫)是否已存在,如果存在则不会添加

  • 检查是否已进行更新,并显示消息而不是现在的文件更新数量。需要改进。

  • 在lib.rs中生成路由器的实现

依赖关系

~7–16MB
~180K SLoC