From 8646a89249420a828eaee5a47858cbb44e8d72d1 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Santillana Date: Mon, 1 Apr 2019 01:23:31 +0200 Subject: [PATCH] Update yarte to 0.2 --- template_yarte/Cargo.toml | 16 ++- template_yarte/README.md | 4 +- template_yarte/src/lib.rs | 1 - template_yarte/src/main.rs | 124 +++++++++++++++--- .../templates/deep/more/card/form.hbs | 20 +++ .../templates/deep/more/card/hi.hbs | 8 ++ .../templates/deep/more/deep/welcome.hbs | 9 +- .../templates/deep/more/doc/head.hbs | 1 + template_yarte/templates/deep/more/doc/t.hbs | 1 + template_yarte/templates/index.hbs | 30 ++--- template_yarte/yarte.toml | 2 + 11 files changed, 167 insertions(+), 49 deletions(-) delete mode 100644 template_yarte/src/lib.rs create mode 100644 template_yarte/templates/deep/more/card/form.hbs create mode 100644 template_yarte/templates/deep/more/card/hi.hbs create mode 100644 template_yarte/templates/deep/more/doc/head.hbs create mode 100644 template_yarte/templates/deep/more/doc/t.hbs diff --git a/template_yarte/Cargo.toml b/template_yarte/Cargo.toml index bcc59083..a734a375 100644 --- a/template_yarte/Cargo.toml +++ b/template_yarte/Cargo.toml @@ -1,15 +1,23 @@ [package] -name = "example" +name = "template_yarte" version = "0.0.1" authors = ["Rust-iendo Barcelona "] publish = false edition = "2018" + workspace = ".." [dependencies] -actix-web = "1.0.0-alpha.2" env_logger = "0.6" -yarte = "0.1" + +yarte = { version = "0.2", features=["with-actix-web"] } + +actix-web = "1.0.0-alpha" [build-dependencies] -yarte = "0.1" +yarte = { version = "0.2", features=["with-actix-web"] } + +[dev-dependencies] +bytes = "0.4" +actix-http-test = "0.1.0-alpha" +actix-http = "0.1.0-alpha" diff --git a/template_yarte/README.md b/template_yarte/README.md index df534a7c..0f3d2756 100644 --- a/template_yarte/README.md +++ b/template_yarte/README.md @@ -2,10 +2,10 @@ Example of composition with partials and `with-actix-web` feature -See the generated code on stdout when run at debug ```bash +cargo test + cargo run ``` > open `localhost:8080` -More at [mdbook](https://yarte.netlify.com/) and [repository](https://gitlab.com/r-iendo/yarte) diff --git a/template_yarte/src/lib.rs b/template_yarte/src/lib.rs deleted file mode 100644 index 8b137891..00000000 --- a/template_yarte/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/template_yarte/src/main.rs b/template_yarte/src/main.rs index cd4434b7..4089a1bc 100644 --- a/template_yarte/src/main.rs +++ b/template_yarte/src/main.rs @@ -3,28 +3,18 @@ extern crate actix_web; use std::collections::HashMap; -use actix_web::{ - error::ErrorInternalServerError, middleware, web::Query, App, HttpResponse, - HttpServer, Result, -}; +use actix_web::{middleware::Logger, web, App, HttpServer, Responder}; use yarte::Template; #[derive(Template)] #[template(path = "index.hbs")] struct IndexTemplate { - query: Query>, + query: web::Query>, } #[get("/")] -pub fn index(query: Query>) -> Result { +pub fn index(query: web::Query>) -> impl Responder { IndexTemplate { query } - .call() - .map(|s| { - HttpResponse::Ok() - .content_type(IndexTemplate::mime()) - .body(s) - }) - .map_err(|_| ErrorInternalServerError("Template parsing error")) } fn main() -> std::io::Result<()> { @@ -32,12 +22,104 @@ fn main() -> std::io::Result<()> { env_logger::init(); // start http server - HttpServer::new(|| { - App::new() - // enable logger - .wrap(middleware::Logger::default()) - .service(index) - }) - .bind("127.0.0.1:8080")? - .run() + HttpServer::new(move || App::new().wrap(Logger::default()).service(index)) + .bind("127.0.0.1:8080")? + .run() +} + +#[cfg(test)] +mod test { + use super::*; + use actix_http::HttpService; + use actix_http_test::TestServer; + use actix_web::{http, App}; + use bytes::Bytes; + + #[test] + fn test() { + let mut srv = TestServer::new(|| HttpService::new(App::new().service(index))); + + let req = srv.get(); + let response = srv.block_on(req.send()).unwrap(); + assert!(response.status().is_success()); + + assert_eq!( + response.headers().get(http::header::CONTENT_TYPE).unwrap(), + "text/html" + ); + + let bytes = srv.block_on(response.body()).unwrap(); + assert_eq!( + bytes, + Bytes::from_static( + "\ + \ + Actix web\ +

Welcome!

\ +

What is your name?

\ +
\ + Name: \ +
Last name: \ +

\ +
\ + " + .as_ref() + ) + ); + + let req = srv.get().uri(srv.url("/?name=foo&lastname=bar")); + let response = srv.block_on(req.send()).unwrap(); + assert!(response.status().is_success()); + + assert_eq!( + response.headers().get(http::header::CONTENT_TYPE).unwrap(), + "text/html" + ); + + let bytes = srv.block_on(response.body()).unwrap(); + assert_eq!( + bytes, + Bytes::from_static( + "\ + \ + Actix web\ + \ +

Hi, foo bar!

Welcome

\ + " + .as_ref() + ) + ); + + let req = srv.get().uri(srv.url("/?name=foo")); + let response = srv.block_on(req.send()).unwrap(); + assert!(response.status().is_server_error()); + + let req = srv.get().uri(srv.url("/?lastname=bar")); + let response = srv.block_on(req.send()).unwrap(); + assert!(response.status().is_success()); + + assert_eq!( + response.headers().get(http::header::CONTENT_TYPE).unwrap(), + "text/html" + ); + + let bytes = srv.block_on(response.body()).unwrap(); + assert_eq!( + bytes, + Bytes::from_static( + "\ + \ + Actix web\ +

Welcome!

\ +

What is your name?

\ +
\ + Name: \ +
Last name: \ +

\ +
\ + " + .as_ref() + ) + ); + } } diff --git a/template_yarte/templates/deep/more/card/form.hbs b/template_yarte/templates/deep/more/card/form.hbs new file mode 100644 index 00000000..a5b75a60 --- /dev/null +++ b/template_yarte/templates/deep/more/card/form.hbs @@ -0,0 +1,20 @@ +{{! + Form: What is your name? +!}} +{{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}} +
+ {{! Title !}} +

What is your name?

+ + {{! Form !}} +
+ {{! Input name !}} + Name: + {{! Input last name !}} +
Last name: + + {{! Submit !}} +

+ + {{! Order and remove whitespace with comments !}} +
diff --git a/template_yarte/templates/deep/more/card/hi.hbs b/template_yarte/templates/deep/more/card/hi.hbs new file mode 100644 index 00000000..7bd929f1 --- /dev/null +++ b/template_yarte/templates/deep/more/card/hi.hbs @@ -0,0 +1,8 @@ +{{! + Hi message: + args: + - lastname + - name +!}} +

Hi, {{ name }} {{ lastname }}!

+{{~> alias/welcome id = "hi", tag = 'p', tail = "" }} diff --git a/template_yarte/templates/deep/more/deep/welcome.hbs b/template_yarte/templates/deep/more/deep/welcome.hbs index 5cb2a971..e0fe08b1 100644 --- a/template_yarte/templates/deep/more/deep/welcome.hbs +++ b/template_yarte/templates/deep/more/deep/welcome.hbs @@ -1 +1,8 @@ -<{{ tag }}>Welcome! +{{! + Welcome card: + args: + - tag + - id + - tail +!}} +<{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }} diff --git a/template_yarte/templates/deep/more/doc/head.hbs b/template_yarte/templates/deep/more/doc/head.hbs new file mode 100644 index 00000000..309846d0 --- /dev/null +++ b/template_yarte/templates/deep/more/doc/head.hbs @@ -0,0 +1 @@ +{{ title }} diff --git a/template_yarte/templates/deep/more/doc/t.hbs b/template_yarte/templates/deep/more/doc/t.hbs new file mode 100644 index 00000000..0e76edd6 --- /dev/null +++ b/template_yarte/templates/deep/more/doc/t.hbs @@ -0,0 +1 @@ + diff --git a/template_yarte/templates/index.hbs b/template_yarte/templates/index.hbs index 503bb845..b3afdf48 100644 --- a/template_yarte/templates/index.hbs +++ b/template_yarte/templates/index.hbs @@ -1,22 +1,12 @@ - +{{! Simple example !}} +{{> doc/t ~}} - - - Actix web - +{{~> doc/head title = "Actix web" ~}} -{{~#if let Some(name) = query.get("name") ~}} -

Hi, {{ name }}!

- {{~> alias/welcome tag='p' ~}} -{{~ else ~}} - {{~> alias/welcome tag="h1" ~}} -

-

What is your name?

-
-
-

-
-

-{{~/if~}} - - + {{~#if let Some(name) = query.get("name") }} + {{ let lastname = query.get("lastname").ok_or(yarte::Error)? }} + {{> card/hi ~}} + {{ else ~}} + {{> card/form ~}} + {{/if ~}} + diff --git a/template_yarte/yarte.toml b/template_yarte/yarte.toml index bcf89202..b3c60a67 100644 --- a/template_yarte/yarte.toml +++ b/template_yarte/yarte.toml @@ -5,3 +5,5 @@ dir = "templates" # Alias for partials. In call, change the start of partial path with one of this, if exist. [partials] alias = "./deep/more/deep" +doc = "./deep/more/doc" +card = "./deep/more/card"