From d3b871189f73d0f230a01584b70da4bbe6fa6b6f Mon Sep 17 00:00:00 2001 From: Vidhan Bhatt Date: Sat, 30 Dec 2023 14:36:47 +0530 Subject: [PATCH 1/3] Support `axum` v0.7 through `axum-core` v0.4 and `http` v1 (#401) * chore: bump `axum-core` for use with new `axum` 0.7 release * chore: bump doctest axum too * chore: update changelog * chore: fix changelog * fix: doctest * fix: import right resp type * fix: bump http as well * fix: doctest * Update CHANGELOG.md --- CHANGELOG.md | 2 ++ docs/content/web-frameworks.md | 7 +++---- doctest/Cargo.toml | 2 +- maud/Cargo.toml | 4 ++-- maud/src/lib.rs | 6 +++--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60f636d2..0e5f58f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ [#380](https://github.com/lambda-fairy/maud/pull/380) - Accept literals in attribute names [#396](https://github.com/lambda-fairy/maud/pull/396) +- Support `axum` v0.7 through `axum-core` v0.4 and `http` v1 + [#401](https://github.com/lambda-fairy/maud/pull/401) ## [0.25.0] - 2023-04-16 diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index 3579f1af..c81bf5a6 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -167,9 +167,8 @@ async fn main() { let app = Router::new().route("/", get(hello_world)); // run it with hyper on localhost:3000 - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) - .serve(app.into_make_service()) - .await - .unwrap(); + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + + axum::serve(listener, app.into_make_service()).await.unwrap(); } ``` diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index 35df0a43..ee2794c4 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -13,7 +13,7 @@ rocket = "0.4" rouille = "3" tide = "0.16" tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] } -axum = "0.6" +axum = "0.7" [dependencies.async-std] version = "1.9.0" diff --git a/maud/Cargo.toml b/maud/Cargo.toml index d9088537..16d9a9b8 100644 --- a/maud/Cargo.toml +++ b/maud/Cargo.toml @@ -25,8 +25,8 @@ rocket = { version = ">= 0.3, < 0.5", optional = true } futures-util = { version = "0.3.0", optional = true, default-features = false } actix-web-dep = { package = "actix-web", version = "4", optional = true, default-features = false } tide = { version = "0.16.0", optional = true, default-features = false } -axum-core = { version = "0.3", optional = true } -http = { version = "0.2", optional = true } +axum-core = { version = "0.4", optional = true } +http = { version = "1", optional = true } [dev-dependencies] trybuild = { version = "1.0.33", features = ["diff"] } diff --git a/maud/src/lib.rs b/maud/src/lib.rs index 53aa3fa9..02de568c 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -338,11 +338,11 @@ mod tide_support { mod axum_support { use crate::PreEscaped; use alloc::string::String; - use axum_core::{body::BoxBody, response::IntoResponse}; - use http::{header, HeaderMap, HeaderValue, Response}; + use axum_core::response::{IntoResponse, Response}; + use http::{header, HeaderMap, HeaderValue}; impl IntoResponse for PreEscaped { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let mut headers = HeaderMap::new(); headers.insert( header::CONTENT_TYPE, From 0de60b0c8615b1ba7fe7f47f324281a4255456de Mon Sep 17 00:00:00 2001 From: Paul Jones <1019888+SenojLuap@users.noreply.github.com> Date: Sat, 30 Dec 2023 04:10:30 -0500 Subject: [PATCH 2/3] Update rocket support to v0.5 (#406) * update rocket support to v0.5 * added rocket upgrade to CHANGELOG.md * fix documentation code for rocket v0.5 --------- Co-authored-by: Chris Wong --- CHANGELOG.md | 2 ++ docs/content/web-frameworks.md | 9 ++++----- doctest/Cargo.toml | 2 +- maud/Cargo.toml | 2 +- maud/src/lib.rs | 8 ++++---- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e5f58f9..69b4df92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ [#396](https://github.com/lambda-fairy/maud/pull/396) - Support `axum` v0.7 through `axum-core` v0.4 and `http` v1 [#401](https://github.com/lambda-fairy/maud/pull/401) +- Support `rocket` v0.5 + [#406](https://github.com/lambda-fairy/maud/pull/406) ## [0.25.0] - 2023-04-16 diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index c81bf5a6..b5442a56 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -60,22 +60,21 @@ maud = { version = "*", features = ["rocket"] } This adds a `Responder` implementation for the `Markup` type, so you can return the result directly: ```rust,no_run -#![feature(decl_macro)] - use maud::{html, Markup}; use rocket::{get, routes}; use std::borrow::Cow; #[get("/")] -fn hello<'a>(name: Cow<'a, str>) -> Markup { +fn hello(name: &str) -> Markup { html! { h1 { "Hello, " (name) "!" } p { "Nice to meet you!" } } } -fn main() { - rocket::ignite().mount("/", routes![hello]).launch(); +#[rocket::launch] +fn launch() -> _ { + rocket::build().mount("/", routes![hello]) } ``` diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index ee2794c4..9f479436 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -9,7 +9,7 @@ actix-web = { version = "4.0.0-rc.2", default-features = false, features = ["mac ammonia = "3" maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum"] } pulldown-cmark = "0.8" -rocket = "0.4" +rocket = "0.5" rouille = "3" tide = "0.16" tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] } diff --git a/maud/Cargo.toml b/maud/Cargo.toml index 16d9a9b8..d4b61ca4 100644 --- a/maud/Cargo.toml +++ b/maud/Cargo.toml @@ -21,7 +21,7 @@ axum = ["axum-core", "http"] [dependencies] maud_macros = { version = "0.25.0", path = "../maud_macros" } itoa = "1" -rocket = { version = ">= 0.3, < 0.5", optional = true } +rocket = { version = "0.5", optional = true } futures-util = { version = "0.3.0", optional = true, default-features = false } actix-web-dep = { package = "actix-web", version = "4", optional = true, default-features = false } tide = { version = "0.16.0", optional = true, default-features = false } diff --git a/maud/src/lib.rs b/maud/src/lib.rs index 02de568c..d6b45b7f 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -284,17 +284,17 @@ mod rocket_support { use crate::PreEscaped; use alloc::string::String; use rocket::{ - http::{ContentType, Status}, + http::ContentType, request::Request, response::{Responder, Response}, }; use std::io::Cursor; - impl Responder<'static> for PreEscaped { - fn respond_to(self, _: &Request) -> Result, Status> { + impl<'r> Responder<'r, 'static> for PreEscaped { + fn respond_to(self, _: &Request) -> rocket::response::Result<'static> { Response::build() .header(ContentType::HTML) - .sized_body(Cursor::new(self.0)) + .sized_body(self.0.len(), Cursor::new(self.0)) .ok() } } From b3a98c98d503f98826a29e96ff5ce528cd44152d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20K=C3=A4llman?= <94024065+RedPhoenixQ@users.noreply.github.com> Date: Thu, 4 Jan 2024 05:32:25 +0100 Subject: [PATCH 3/3] Parse many literals along side idents in names (#398) * Parse many literals along side idents in names * Accept ints as literals We will not accept floats because `123.123` is a float literal, but `123 .123` is a int literal followed by a class called `123`. This could be confusing so it will not be accepted. Ints can have leading zeros, like `0123`, but this is not guarranteed by the rust compiler to always work, which could cause future errors. An example would be truncating `001` to `1`. * Limit accepted literals using existing function * Update error output for non-string-literal * Test output of ints with specified type This outputs exactly what is written, which is the obvious behaviour * Use nightly version to generate output Previous verison was not using nightly, causing errors in the automated test that are using nightly * Replace "byte_string" with "raw_string" in test --------- Co-authored-by: Chris Wong --- maud/tests/basic_syntax.rs | 14 +++++-- maud/tests/warnings/non-string-literal.stderr | 24 +++--------- maud_macros/src/parse.rs | 37 ++++++++++++------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/maud/tests/basic_syntax.rs b/maud/tests/basic_syntax.rs index 96e0235e..c2c5dc5f 100644 --- a/maud/tests/basic_syntax.rs +++ b/maud/tests/basic_syntax.rs @@ -201,11 +201,19 @@ fn raw_string_literals_in_attribute_names() { #[test] fn other_literals_in_attribute_names() { - let result = - html! { this b"byte_string"="false" 123="123" 2.5 true 'a'="a" b'b'="b" of-course {} }; + let result = html! { this r#"raw_string"#="false" 123="123" 123usize "2.5" true of-course {} }; assert_eq!( result.into_string(), - r#""# + r#""# + ); +} + +#[test] +fn idents_and_literals_in_names() { + let result = html! { custom:element-001 test:123-"test"="123" .m-2.p-2 {} }; + assert_eq!( + result.into_string(), + r#""# ); } diff --git a/maud/tests/warnings/non-string-literal.stderr b/maud/tests/warnings/non-string-literal.stderr index 635617b8..7755e3f0 100644 --- a/maud/tests/warnings/non-string-literal.stderr +++ b/maud/tests/warnings/non-string-literal.stderr @@ -1,41 +1,29 @@ -error: literal must be double-quoted: `"42"` - --> $DIR/non-string-literal.rs:5:9 - | -5 | 42 - | ^^ - -error: literal must be double-quoted: `"42usize"` - --> $DIR/non-string-literal.rs:6:9 - | -6 | 42usize - | ^^^^^^^ - error: literal must be double-quoted: `"42.0"` - --> $DIR/non-string-literal.rs:7:9 + --> tests/warnings/non-string-literal.rs:7:9 | 7 | 42.0 | ^^^^ error: literal must be double-quoted: `"a"` - --> $DIR/non-string-literal.rs:8:9 + --> tests/warnings/non-string-literal.rs:8:9 | 8 | 'a' | ^^^ error: expected string - --> $DIR/non-string-literal.rs:9:9 + --> tests/warnings/non-string-literal.rs:9:9 | 9 | b"a" | ^^^^ error: expected string - --> $DIR/non-string-literal.rs:10:9 + --> tests/warnings/non-string-literal.rs:10:9 | 10 | b'a' | ^^^^ error: attribute value must be a string - --> $DIR/non-string-literal.rs:13:24 + --> tests/warnings/non-string-literal.rs:13:24 | 13 | input disabled=true; | ^^^^ @@ -44,7 +32,7 @@ error: attribute value must be a string = help: to toggle the attribute, use square brackets: `disabled[some_boolean_flag]` error: attribute value must be a string - --> $DIR/non-string-literal.rs:14:24 + --> tests/warnings/non-string-literal.rs:14:24 | 14 | input disabled=false; | ^^^^^ diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs index 662b8309..1b85d1f3 100644 --- a/maud_macros/src/parse.rs +++ b/maud_macros/src/parse.rs @@ -204,7 +204,13 @@ impl Parser { } // Boolean literals are idents, so `Lit::Bool` is handled in // `markup`, not here. - Lit::Int(..) | Lit::Float(..) => { + Lit::Int(lit_int) => { + return ast::Markup::Literal { + content: lit_int.to_string(), + span: SpanRange::single_span(literal.span()), + }; + } + Lit::Float(..) => { emit_error!(literal, r#"literal must be double-quoted: `"{}"`"#, literal); } Lit::Char(lit_char) => { @@ -702,27 +708,32 @@ impl Parser { /// Parses an identifier, without dealing with namespaces. fn try_name(&mut self) -> Option { let mut result = Vec::new(); - match self.peek() { - Some(token @ TokenTree::Ident(_)) | Some(token @ TokenTree::Literal(_)) => { - self.advance(); - result.push(token); - } - _ => return None, - }; - let mut expect_ident = false; + let mut expect_ident_or_literal = true; loop { - expect_ident = match self.peek() { + expect_ident_or_literal = match self.peek() { Some(TokenTree::Punct(ref punct)) if punct.as_char() == '-' => { self.advance(); result.push(TokenTree::Punct(punct.clone())); true } - Some(TokenTree::Ident(ref ident)) if expect_ident => { + Some(token @ TokenTree::Ident(_)) if expect_ident_or_literal => { self.advance(); - result.push(TokenTree::Ident(ident.clone())); + result.push(token); false } - _ => break, + Some(TokenTree::Literal(ref literal)) if expect_ident_or_literal => { + self.literal(literal.clone()); + self.advance(); + result.push(TokenTree::Literal(literal.clone())); + false + } + _ => { + if result.is_empty() { + return None; + } else { + break; + } + } }; } Some(result.into_iter().collect())