Skip to content

Commit

Permalink
Merge pull request loco-rs#486 from PolpOnline/into-string
Browse files Browse the repository at this point in the history
Use Into<String> in function signature to allow String to be passed in directly
  • Loading branch information
jondot authored Mar 8, 2024
2 parents d1f27ea + 6dc61e2 commit c3597ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
10 changes: 4 additions & 6 deletions examples/demo/src/controllers/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ async fn register(
let user = match res {
Ok(user) => user,
Err(err) => {
tracing::info!(
message = err.to_string(),
user_email = &params.email,
"could not register user",
);
return bad_request("could not register user");
let msg = "could not register user";

tracing::info!(message = err.to_string(), user_email = &params.email, msg,);
return bad_request(msg);
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/views/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct PaginationResponse {}
impl From<notes::Model> for ListResponse {
fn from(note: notes::Model) -> Self {
Self {
id: note.id.clone(),
id: note.id,
title: note.title.clone(),
content: note.content,
}
Expand Down
43 changes: 22 additions & 21 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@
//! }
//! ```
use axum::extract::FromRequest;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use colored::Colorize;
use serde::Serialize;

pub use app_routes::{AppRoutes, ListRoutes};
pub use routes::Routes;

use crate::{errors::Error, Result};

mod app_routes;
mod backtrace;
#[cfg(feature = "channels")]
Expand All @@ -84,17 +97,6 @@ mod ping;
mod routes;
pub mod views;

pub use app_routes::{AppRoutes, ListRoutes};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use colored::Colorize;
pub use routes::Routes;
use serde::Serialize;

use crate::{errors::Error, Result};

/// Create an unauthorized error with a specified message.
///
/// This function is used to generate an `Error::Unauthorized` variant with a
Expand All @@ -120,17 +122,17 @@ use crate::{errors::Error, Result};
/// format::json(())
/// }
/// ````
pub fn unauthorized<T>(msg: &str) -> Result<T> {
Err(Error::Unauthorized(msg.to_string()))
pub fn unauthorized<T: Into<String>, U>(msg: T) -> Result<U> {
Err(Error::Unauthorized(msg.into()))
}

/// Return a bad request with a message
///
/// # Errors
///
/// This function will return an error result
pub fn bad_request<T>(msg: &str) -> Result<T> {
Err(Error::BadRequest(msg.to_string()))
pub fn bad_request<T: Into<String>, U>(msg: T) -> Result<U> {
Err(Error::BadRequest(msg.into()))
}

/// return not found status code
Expand All @@ -153,24 +155,23 @@ pub struct ErrorDetail {
impl ErrorDetail {
/// Create a new `ErrorDetail` with the specified error and description.
#[must_use]
pub fn new(error: &str, description: &str) -> Self {
pub fn new<T: Into<String>>(error: T, description: T) -> Self {
Self {
error: Some(error.to_string()),
description: Some(description.to_string()),
error: Some(error.into()),
description: Some(description.into()),
}
}

/// Create an `ErrorDetail` with only an error reason and no description.
#[must_use]
pub fn with_reason(error: &str) -> Self {
pub fn with_reason<T: Into<String>>(error: T) -> Self {
Self {
error: Some(error.to_string()),
error: Some(error.into()),
description: None,
}
}
}

use axum::extract::FromRequest;
#[derive(Debug, FromRequest)]
#[from_request(via(axum::Json), rejection(Error))]
pub struct Json<T>(pub T);
Expand Down

0 comments on commit c3597ab

Please sign in to comment.