Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-fairy authored Jan 4, 2024
2 parents f077f0b + b3a98c9 commit f36060b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
[#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)
- Add support for `warp` v0.3.6
[#404](https://github.com/lambda-fairy/maud/pull/404)
- Support `rocket` v0.5
[#406](https://github.com/lambda-fairy/maud/pull/406)

## [0.25.0] - 2023-04-16

Expand Down
16 changes: 7 additions & 9 deletions docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,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("/<name>")]
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])
}
```

Expand Down Expand Up @@ -168,10 +167,9 @@ 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();
}
```

Expand Down
4 changes: 2 additions & 2 deletions doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ actix-web = { version = "4.0.0-rc.2", default-features = false, features = ["mac
ammonia = "3"
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp"] }
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"] }
axum = "0.6"
axum = "0.7"
warp = "0.3.6"

[dependencies.async-std]
Expand Down
6 changes: 3 additions & 3 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ 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 }
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 }
warp = { version = "0.3.6", optional = true }

[dev-dependencies]
Expand Down
14 changes: 7 additions & 7 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
impl<'r> Responder<'r, 'static> for PreEscaped<String> {
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()
}
}
Expand Down Expand Up @@ -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<String> {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
Expand Down
14 changes: 11 additions & 3 deletions maud/tests/basic_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<this byte_string="false" 123="123" 2.5 true a="a" b="b" of-course></this>"#
r#"<this raw_string="false" 123="123" 123usize 2.5 true of-course></this>"#
);
}

#[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#"<custom:element-001 class="m-2 p-2" test:123-test="123"></custom:element-001>"#
);
}

Expand Down
24 changes: 6 additions & 18 deletions maud/tests/warnings/non-string-literal.stderr
Original file line number Diff line number Diff line change
@@ -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;
| ^^^^
Expand All @@ -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;
| ^^^^^
Expand Down
37 changes: 24 additions & 13 deletions maud_macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -702,27 +708,32 @@ impl Parser {
/// Parses an identifier, without dealing with namespaces.
fn try_name(&mut self) -> Option<TokenStream> {
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())
Expand Down

0 comments on commit f36060b

Please sign in to comment.