From 2444ee437a5a5f55d9c3978442ac4d6675b85e82 Mon Sep 17 00:00:00 2001 From: Chris Wong Date: Fri, 12 Aug 2022 22:35:20 +1000 Subject: [PATCH 1/2] Add `maud::display` --- maud/src/lib.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/maud/src/lib.rs b/maud/src/lib.rs index 73ac24f5..43a82ada 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -12,7 +12,7 @@ extern crate alloc; use alloc::{borrow::Cow, boxed::Box, string::String}; -use core::fmt::{self, Arguments, Write}; +use core::fmt::{self, Arguments, Display, Write}; pub use maud_macros::{html, html_debug}; @@ -184,6 +184,35 @@ impl_render_with_itoa! { u8 u16 u32 u64 u128 usize } +/// Renders a value using its [`Display`] impl. +/// +/// # Example +/// +/// ```rust +/// use maud::html; +/// use std::net::Ipv4Addr; +/// +/// let ip_address = Ipv4Addr::new(127, 0, 0, 1); +/// +/// let markup = html! { +/// "My IP address is: " +/// (maud::display(ip_address)) +/// }; +/// +/// assert_eq!(markup.into_string(), "My IP address is: 127.0.0.1"); +/// ``` +pub fn display(value: impl Display) -> impl Render { + struct DisplayWrapper(T); + + impl Render for DisplayWrapper { + fn render_to(&self, w: &mut String) { + format_args!("{0}", self.0).render_to(w); + } + } + + DisplayWrapper(value) +} + /// A wrapper that renders the inner value without escaping. #[derive(Debug, Clone, Copy)] pub struct PreEscaped>(pub T); From 0b8f9f56801eeb3682165c3265dfe0e50fceb7f8 Mon Sep 17 00:00:00 2001 From: Chris Wong Date: Fri, 12 Aug 2022 22:42:32 +1000 Subject: [PATCH 2/2] Update change log --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eab52f20..e047a1e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ [#337](https://github.com/lambda-fairy/maud/pull/337) - Update to `actix-web` 4.0. [#331](https://github.com/lambda-fairy/maud/pull/331) +- Add a `maud::display` adapter that forwards to the `Display` impl + [#350](https://github.com/lambda-fairy/maud/pull/350) ## [0.23.0] - 2021-11-10