Skip to main content

shared_framework/response/
html.rs

1//! HTML responses: shortcut for returning rendered markup from handlers.
2//!
3//! [`HtmlServiceResult::ok`] wraps an HTML string in a 200 [`ServiceResult`](super::ServiceResult)
4//! with [`ResponseType::Html`](super::ResponseType), so the router renders it as `text/html`.
5//! ```ignore
6//! async fn page(ctx: CorrelationContext) -> Result<ServiceResult<String>, ErrorResult> {
7//!     Ok(HtmlServiceResult::ok("<h1>Hello</h1>"))
8//! }
9//! ```
10
11use super::{ResponseType, ServiceResult};
12
13/// Shortcut constructors for HTML handler results.
14pub struct HtmlServiceResult;
15
16impl HtmlServiceResult {
17    /// Wraps `html` in a 200 success result rendered as `text/html`.
18    pub fn ok(html: impl Into<String>) -> ServiceResult<String> {
19        ServiceResult::new("success", "OK", Some(html.into()), 200)
20            .with_response_type(ResponseType::Html)
21    }
22}