-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* impl gen controller * align path separator * improve route uri building * align tests
- Loading branch information
Showing
16 changed files
with
336 additions
and
43 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,53 @@ | ||
use rrgen::RRgen; | ||
use serde_json::json; | ||
|
||
use crate::{app::Hooks, gen}; | ||
|
||
const API_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/api/controller.t"); | ||
const API_CONTROLLER_TEST_T: &str = include_str!("templates/controller/api/test.t"); | ||
|
||
const HTMX_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/htmx/controller.t"); | ||
const HTMX_VIEW_T: &str = include_str!("templates/controller/htmx/view.t"); | ||
|
||
const HTML_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/html/controller.t"); | ||
const HTML_VIEW_T: &str = include_str!("templates/controller/html/view.t"); | ||
|
||
use super::collect_messages; | ||
use crate::Result; | ||
|
||
pub fn generate<H: Hooks>( | ||
rrgen: &RRgen, | ||
name: &str, | ||
actions: &[String], | ||
kind: &gen::ScaffoldKind, | ||
) -> Result<String> { | ||
let vars = json!({"name": name, "actions": actions, "pkg_name": H::app_name()}); | ||
match kind { | ||
gen::ScaffoldKind::Api => { | ||
let res1 = rrgen.generate(API_CONTROLLER_CONTROLLER_T, &vars)?; | ||
let res2 = rrgen.generate(API_CONTROLLER_TEST_T, &vars)?; | ||
let messages = collect_messages(vec![res1, res2]); | ||
Ok(messages) | ||
} | ||
gen::ScaffoldKind::Html => { | ||
let mut messages = Vec::new(); | ||
let res = rrgen.generate(HTML_CONTROLLER_CONTROLLER_T, &vars)?; | ||
messages.push(res); | ||
for action in actions { | ||
let vars = json!({"name": name, "action": action, "pkg_name": H::app_name()}); | ||
messages.push(rrgen.generate(HTML_VIEW_T, &vars)?); | ||
} | ||
Ok(collect_messages(messages)) | ||
} | ||
gen::ScaffoldKind::Htmx => { | ||
let mut messages = Vec::new(); | ||
let res = rrgen.generate(HTMX_CONTROLLER_CONTROLLER_T, &vars)?; | ||
messages.push(res); | ||
for action in actions { | ||
let vars = json!({"name": name, "action": action, "pkg_name": H::app_name()}); | ||
messages.push(rrgen.generate(HTMX_VIEW_T, &vars)?); | ||
} | ||
Ok(collect_messages(messages)) | ||
} | ||
} | ||
} |
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,40 @@ | ||
{% set file_name = name | snake_case -%} | ||
{% set module_name = file_name | pascal_case -%} | ||
to: src/controllers/{{ file_name }}.rs | ||
skip_exists: true | ||
message: "Controller `{{module_name}}` was added successfully." | ||
injections: | ||
- into: src/controllers/mod.rs | ||
append: true | ||
content: "pub mod {{ file_name }};" | ||
- into: src/app.rs | ||
after: "AppRoutes::" | ||
content: " .add_route(controllers::{{ file_name }}::routes())" | ||
--- | ||
#![allow(clippy::missing_errors_doc)] | ||
#![allow(clippy::unnecessary_struct_initialization)] | ||
#![allow(clippy::unused_async)] | ||
use loco_rs::prelude::*; | ||
use axum::debug_handler; | ||
|
||
#[debug_handler] | ||
pub async fn index(State(_ctx): State<AppContext>) -> Result<Response> { | ||
format::empty() | ||
} | ||
|
||
{% for action in actions -%} | ||
#[debug_handler] | ||
pub async fn {{action}}(State(_ctx): State<AppContext>) -> Result<Response> { | ||
format::empty() | ||
} | ||
|
||
{% endfor -%} | ||
|
||
pub fn routes() -> Routes { | ||
Routes::new() | ||
.prefix("{{file_name | plural}}/") | ||
.add("/", get(index)) | ||
{%- for action in actions %} | ||
.add("{{action}}", get({{action}})) | ||
{%- endfor %} | ||
} |
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,26 @@ | ||
{% set file_name = name | snake_case -%} | ||
{% set module_name = file_name | pascal_case -%} | ||
to: tests/requests/{{ file_name }}.rs | ||
skip_exists: true | ||
message: "Tests for controller `{{module_name}}` was added successfully. Run `cargo run test`." | ||
injections: | ||
- into: tests/requests/mod.rs | ||
append: true | ||
content: "pub mod {{ file_name }};" | ||
--- | ||
use {{pkg_name}}::app::App; | ||
use loco_rs::testing; | ||
use serial_test::serial; | ||
|
||
{% for action in actions -%} | ||
#[tokio::test] | ||
#[serial] | ||
async fn can_get_{{action}}() { | ||
testing::request::<App, _, _>(|request, _ctx| async move { | ||
let res = request.get("/{{ name | snake_case }}/{{action}}").await; | ||
assert_eq!(res.status_code(), 200); | ||
}) | ||
.await; | ||
} | ||
|
||
{% endfor -%} |
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,37 @@ | ||
{% set file_name = name | snake_case -%} | ||
{% set module_name = file_name | pascal_case -%} | ||
to: src/controllers/{{ file_name }}.rs | ||
skip_exists: true | ||
message: "Controller `{{module_name}}` was added successfully." | ||
injections: | ||
- into: src/controllers/mod.rs | ||
append: true | ||
content: "pub mod {{ file_name }};" | ||
- into: src/app.rs | ||
after: "AppRoutes::" | ||
content: " .add_route(controllers::{{ file_name }}::routes())" | ||
--- | ||
#![allow(clippy::missing_errors_doc)] | ||
#![allow(clippy::unnecessary_struct_initialization)] | ||
#![allow(clippy::unused_async)] | ||
use loco_rs::prelude::*; | ||
use axum::debug_handler; | ||
|
||
{% for action in actions -%} | ||
#[debug_handler] | ||
pub async fn {{action}}( | ||
ViewEngine(v): ViewEngine<TeraView>, | ||
State(_ctx): State<AppContext> | ||
) -> Result<Response> { | ||
format::render().view(&v, "{{file_name}}/{{action}}.html", serde_json::json!({})) | ||
} | ||
|
||
{% endfor -%} | ||
|
||
pub fn routes() -> Routes { | ||
Routes::new() | ||
.prefix("{{file_name | plural}}/") | ||
{%- for action in actions %} | ||
.add("{{action}}", get({{action}})) | ||
{%- endfor %} | ||
} |
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,19 @@ | ||
{% set file_name = name | snake_case -%} | ||
{% set module_name = file_name | pascal_case -%} | ||
to: assets/views/{{file_name}}/{{action}}.html | ||
skip_exists: true | ||
message: "{{file_name}}/{{action}} view was added successfully." | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script> | ||
</head> | ||
|
||
<body class="prose p-10"> | ||
<h1>View {{action}}</h1> | ||
Find me in <code>{{file_name}}/{{action}}</code> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.