Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Display and Debug for Text and Json #912

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! Read more: <https://salvo.rs>

use std::fmt::{self, Display};
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use indexmap::IndexMap;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl FromStr for CompressionAlgo {
impl Display for CompressionAlgo {
#[allow(unreachable_patterns)]
#[allow(unused_variables)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "brotli")]
CompressionAlgo::Brotli => write!(f, "br"),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/conn/native_tls/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! native_tls module
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
use std::fs::File;
use std::io::{Error as IoError, ErrorKind, Result as IoResult, Read};
use std::path::{Path, PathBuf};
Expand All @@ -21,7 +21,7 @@ pub struct NativeTlsConfig {
pub password: String,
}

impl fmt::Debug for NativeTlsConfig {
impl Debug for NativeTlsConfig {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("NativeTlsConfig").finish()
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/conn/openssl/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! openssl module
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
use std::fs::File;
use std::future::{Ready, ready};
use std::io::{Error as IoError, Read, Result as IoResult};
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct OpensslConfig {
pub alpn_protocols: Vec<u8>,
}

impl fmt::Debug for OpensslConfig {
impl Debug for OpensslConfig {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("OpensslConfig").finish()
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/depot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};

/// Store temp data for current request.
///
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Depot {
}
}

impl fmt::Debug for Depot {
impl Debug for Depot {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Depot")
.field("keys", &self.map.keys())
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/http/body/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::fmt::{self, Debug, Formatter};
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -148,8 +148,8 @@ impl tokio::io::AsyncWrite for BodySender {
}
}

impl fmt::Debug for BodySender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl Debug for BodySender {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut builder = f.debug_tuple("BodySender");

builder.finish()
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/http/body/req.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -298,8 +298,8 @@ cfg_feature! {
}
}

impl fmt::Debug for ReqBody {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl Debug for ReqBody {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
ReqBody::None => write!(f, "ReqBody::None"),
ReqBody::Once(value) => f.debug_tuple("ReqBody::Once").field(value).finish(),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/http/body/res.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::VecDeque;
use std::fmt::Debug;
use std::fmt::{self, Debug};
use std::future::Future;
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
Expand Down Expand Up @@ -286,7 +286,7 @@ where
}

impl Debug for ResBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::None => write!(f, "ResBody::None"),
Self::Once(value) => f.debug_tuple("ResBody::Once").field(value).finish(),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/http/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! HTTP request.
use std::error::Error as StdError;
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
#[cfg(feature = "quinn")]
use std::sync::Arc;
use std::sync::OnceLock;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub struct Request {
pub(crate) secure_max_size: Option<usize>,
}

impl fmt::Debug for Request {
impl Debug for Request {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Request")
.field("method", self.method())
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/http/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! HTTP response.
use std::collections::VecDeque;
use std::fmt::{self, Display, Formatter};
use std::fmt::{self, Debug, Display, Formatter};
use std::path::PathBuf;

#[cfg(feature = "cookie")]
Expand Down Expand Up @@ -506,7 +506,7 @@ impl Response {
}
}

impl fmt::Debug for Response {
impl Debug for Response {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status_code", &self.status_code)
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/routing/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod opts;
mod others;
mod path;

use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};

use self::opts::*;
use crate::http::uri::Scheme;
Expand All @@ -21,7 +21,7 @@ pub use path::*;
///
/// View [module level documentation](../index.html) for more details.

pub trait Filter: fmt::Debug + Send + Sync + 'static {
pub trait Filter: Debug + Send + Sync + 'static {
#[doc(hidden)]
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self>()
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/routing/filters/others.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};

use crate::http::uri::Scheme;
use crate::http::{Method, Request};
Expand All @@ -19,7 +19,7 @@ impl Filter for MethodFilter {
req.method() == self.0
}
}
impl fmt::Debug for MethodFilter {
impl Debug for MethodFilter {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "method:{:?}", self.0)
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Filter for SchemeFilter {
.unwrap_or(self.lack)
}
}
impl fmt::Debug for SchemeFilter {
impl Debug for SchemeFilter {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "scheme:{:?}", self.scheme)
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Filter for HostFilter {
.unwrap_or(self.lack)
}
}
impl fmt::Debug for HostFilter {
impl Debug for HostFilter {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "host:{:?}", self.host)
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Filter for PortFilter {
.unwrap_or(self.lack)
}
}
impl fmt::Debug for PortFilter {
impl Debug for PortFilter {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "port:{:?}", self.port)
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/routing/filters/path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Path filter implementation.

use std::collections::HashMap;
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
use std::sync::{Arc, LazyLock};

use indexmap::IndexSet;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl PathWisp for WispKind {
}
}
}
impl fmt::Debug for WispKind {
impl Debug for WispKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Const(wisp) => wisp.fmt(f),
Expand Down Expand Up @@ -240,7 +240,7 @@ pub struct CharsWisp {
min_width: usize,
max_width: Option<usize>,
}
impl fmt::Debug for CharsWisp {
impl Debug for CharsWisp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -905,7 +905,7 @@ pub struct PathFilter {
path_wisps: Vec<WispKind>,
}

impl fmt::Debug for PathFilter {
impl Debug for PathFilter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "path:{}", &self.raw_value)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/routing/router.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Formatter};
use std::fmt::{self, Debug, Formatter};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

Expand Down Expand Up @@ -337,7 +337,7 @@ const SYMBOL_DOWN: &str = "│";
const SYMBOL_TEE: &str = "├";
const SYMBOL_ELL: &str = "└";
const SYMBOL_RIGHT: &str = "─";
impl fmt::Debug for Router {
impl Debug for Router {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
fn print(f: &mut Formatter, prefix: &str, last: bool, router: &Router) -> fmt::Result {
let mut path = "".to_owned();
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl<A: Acceptor + Send> Server<A> {

/// Try to serve a [`Service`].
#[cfg(feature = "server-handle")]
#[allow(clippy::manual_async_fn)]//Fix: https://github.com/salvo-rs/salvo/issues/902
pub fn try_serve<S>(self, service: S) -> impl Future<Output=IoResult<()>> + Send
where
S: Into<Service> + Send,
Expand Down
12 changes: 12 additions & 0 deletions crates/core/src/writing/json.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Debug, Display, Formatter};

use async_trait::async_trait;
use serde::Serialize;

Expand Down Expand Up @@ -47,6 +49,16 @@ where
}
}
}
impl<T: Debug> Debug for Json<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_tuple("Json").field(&self.0).finish()
}
}
impl<T: Display> Display for Json<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}

#[cfg(test)]
mod tests {
Expand Down
34 changes: 34 additions & 0 deletions crates/core/src/writing/text.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Debug, Display, Formatter};

use super::Scribe;
use crate::http::header::{HeaderValue, CONTENT_TYPE};
use crate::http::Response;
Expand Down Expand Up @@ -104,6 +106,38 @@ impl<'a> Scribe for Text<&'a String> {
res.write_body(content.as_bytes().to_vec()).ok();
}
}
impl<C: Debug> Debug for Text<C> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Text::Plain(content) => f.debug_tuple("Text::Plain").field(content).finish(),
Text::Json(content) => f.debug_tuple("Text::Json").field(content).finish(),
Text::Xml(content) => f.debug_tuple("Text::Xml").field(content).finish(),
Text::Html(content) => f.debug_tuple("Text::Html").field(content).finish(),
Text::Js(content) => f.debug_tuple("Text::Js").field(content).finish(),
Text::Css(content) => f.debug_tuple("Text::Css").field(content).finish(),
Text::Csv(content) => f.debug_tuple("Text::Csv").field(content).finish(),
Text::Atom(content) => f.debug_tuple("Text::Atom").field(content).finish(),
Text::Rss(content) => f.debug_tuple("Text::Rss").field(content).finish(),
Text::Rdf(content) => f.debug_tuple("Text::Rdf").field(content).finish(),
}
}
}
impl<C: Display> Display for Text<C> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Text::Plain(content) => Display::fmt(content, f),
Text::Json(content) => Display::fmt(content, f),
Text::Xml(content) => Display::fmt(content, f),
Text::Html(content) => Display::fmt(content, f),
Text::Js(content) => Display::fmt(content, f),
Text::Css(content) => Display::fmt(content, f),
Text::Csv(content) => Display::fmt(content, f),
Text::Atom(content) => Display::fmt(content, f),
Text::Rss(content) => Display::fmt(content, f),
Text::Rdf(content) => Display::fmt(content, f),
}
}
}

#[cfg(test)]
mod tests {
Expand Down
7 changes: 4 additions & 3 deletions crates/cors/src/allow_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fmt, sync::Arc};
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;

use salvo_core::http::header::{self, HeaderName, HeaderValue};
use salvo_core::{Depot, Request};
Expand Down Expand Up @@ -69,8 +70,8 @@ impl From<bool> for AllowCredentials {
}
}

impl fmt::Debug for AllowCredentials {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl Debug for AllowCredentials {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
AllowCredentialsInner::Yes => f.debug_tuple("Yes").finish(),
AllowCredentialsInner::No => f.debug_tuple("No").finish(),
Expand Down
6 changes: 3 additions & 3 deletions crates/cors/src/allow_headers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::fmt::{self, Debug, Formatter};
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -93,8 +93,8 @@ impl AllowHeaders {
}
}

impl fmt::Debug for AllowHeaders {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl Debug for AllowHeaders {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.0 {
AllowHeadersInner::None => f.debug_tuple("None").finish(),
AllowHeadersInner::Exact(inner) => f.debug_tuple("Exact").field(inner).finish(),
Expand Down
6 changes: 3 additions & 3 deletions crates/cors/src/allow_methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;

use salvo_core::http::header::{self, HeaderName, HeaderValue};
Expand Down Expand Up @@ -105,8 +105,8 @@ impl AllowMethods {
}
}

impl fmt::Debug for AllowMethods {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl Debug for AllowMethods {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.0 {
AllowMethodsInner::None => f.debug_tuple("None").finish(),
AllowMethodsInner::Exact(inner) => f.debug_tuple("Exact").field(inner).finish(),
Expand Down
Loading