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

Rename EndpointResult to Result #240

Closed
Closed
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
6 changes: 3 additions & 3 deletions examples/src/body_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use tide::{
error::ResultExt,
forms::{self, ExtractForms},
response, App, Context, EndpointResult,
response, App, Context,
};

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -23,13 +23,13 @@ async fn echo_bytes(mut cx: Context<()>) -> Vec<u8> {
msg
}

async fn echo_json(mut cx: Context<()>) -> EndpointResult {
async fn echo_json(mut cx: Context<()>) -> tide::Result {
let msg = cx.body_json().await.client_err()?;
println!("JSON: {:?}", msg);
Ok(response::json(msg))
}

async fn echo_form(mut cx: Context<()>) -> EndpointResult {
async fn echo_form(mut cx: Context<()>) -> tide::Result {
let msg = cx.body_form().await?;
println!("Form: {:?}", msg);
Ok(forms::form(msg))
Expand Down
4 changes: 2 additions & 2 deletions examples/src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use http::status::StatusCode;
use juniper::graphql_object;
use std::sync::{atomic, Arc};
use tide::{error::ResultExt, response, App, Context, EndpointResult};
use tide::{error::ResultExt, response, App, Context};

// First, we define `Data` that holds accumulator state. This is accessible as App data in
// Tide, and as executor context in Juniper.
Expand Down Expand Up @@ -42,7 +42,7 @@ type Schema = juniper::RootNode<'static, Query, Mutation>;

// Finally, we'll bridge between Tide and Juniper. `GraphQLRequest` from Juniper implements
// `Deserialize`, so we use `Json` extractor to deserialize the request body.
async fn handle_graphql(mut cx: Context<Data>) -> EndpointResult {
async fn handle_graphql(mut cx: Context<Data>) -> tide::Result {
let query: juniper::http::GraphQLRequest = cx.body_json().await.client_err()?;
let schema = Schema::new(Query, Mutation);
let response = query.execute(&schema, cx.state());
Expand Down
8 changes: 4 additions & 4 deletions examples/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use http::status::StatusCode;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use tide::{error::ResultExt, response, App, Context, EndpointResult};
use tide::{error::ResultExt, response, App, Context};

#[derive(Default)]
struct Database {
Expand Down Expand Up @@ -37,12 +37,12 @@ impl Database {
}
}

async fn new_message(mut cx: Context<Database>) -> EndpointResult<String> {
async fn new_message(mut cx: Context<Database>) -> tide::Result<String> {
let msg = cx.body_json().await.client_err()?;
Ok(cx.state().insert(msg).to_string())
}

async fn set_message(mut cx: Context<Database>) -> EndpointResult<()> {
async fn set_message(mut cx: Context<Database>) -> tide::Result<()> {
let msg = cx.body_json().await.client_err()?;
let id = cx.param("id").client_err()?;

Expand All @@ -53,7 +53,7 @@ async fn set_message(mut cx: Context<Database>) -> EndpointResult<()> {
}
}

async fn get_message(cx: Context<Database>) -> EndpointResult {
async fn get_message(cx: Context<Database>) -> tide::Result {
let id = cx.param("id").client_err()?;
if let Some(msg) = cx.state().get(id) {
Ok(response::json(msg))
Expand Down
4 changes: 2 additions & 2 deletions examples/src/multipart_form/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use std::io::Read;
use tide::{forms::ExtractForms, response, App, Context, EndpointResult};
use tide::{forms::ExtractForms, response, App, Context};

#[derive(Serialize, Deserialize, Clone)]
struct Message {
Expand All @@ -9,7 +9,7 @@ struct Message {
file: Option<String>,
}

async fn upload_file(mut cx: Context<()>) -> EndpointResult {
async fn upload_file(mut cx: Context<()>) -> tide::Result {
// https://stackoverflow.com/questions/43424982/how-to-parse-multipart-forms-using-abonander-multipart-with-rocket
let mut message = Message {
key1: None,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/staticfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use http::{
StatusCode,
};
use http_service::Body;
use tide::{App, Context, EndpointResult, Response};
use tide::{App, Context, Response};

use std::path::{Component, Path, PathBuf};
use std::{fs, io};
Expand Down Expand Up @@ -106,7 +106,7 @@ impl StaticFile {
}
}

async fn handle_path(ctx: Context<StaticFile>) -> EndpointResult {
async fn handle_path(ctx: Context<StaticFile>) -> tide::Result {
let path = ctx.uri().path();
ctx.state()
.stream_bytes(path, ctx.headers())
Expand Down
10 changes: 5 additions & 5 deletions tide-core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ use crate::{
///
/// use tide::error::ResultExt;
///
/// async fn hello(cx: tide::Context<()>) -> tide::EndpointResult<String> {
/// async fn hello(cx: tide::Context<()>) -> tide::Result<String> {
/// let user: String = cx.param("user").client_err()?;
/// Ok(format!("Hello, {}!", user))
/// }
///
/// async fn goodbye(cx: tide::Context<()>) -> tide::EndpointResult<String> {
/// async fn goodbye(cx: tide::Context<()>) -> tide::Result<String> {
/// let user: String = cx.param("user").client_err()?;
/// Ok(format!("Goodbye, {}.", user))
/// }
Expand All @@ -81,7 +81,7 @@ use crate::{
/// use http::status::StatusCode;
/// use serde::{Deserialize, Serialize};
/// use std::sync::Mutex;
/// use tide::{error::ResultExt, response, App, Context, EndpointResult};
/// use tide::{error::ResultExt, response, App, Context};
///
/// #[derive(Default)]
/// struct Database {
Expand All @@ -106,12 +106,12 @@ use crate::{
/// }
/// }
///
/// async fn new_message(mut cx: Context<Database>) -> EndpointResult<String> {
/// async fn new_message(mut cx: Context<Database>) -> tide::Result<String> {
/// let msg = cx.body_json().await.client_err()?;
/// Ok(cx.state().insert(msg).to_string())
/// }
///
/// async fn get_message(cx: Context<Database>) -> EndpointResult {
/// async fn get_message(cx: Context<Database>) -> tide::Result {
/// let id = cx.param("id").client_err()?;
/// if let Some(msg) = cx.state().get(id) {
/// Ok(response::json(msg))
Expand Down
30 changes: 15 additions & 15 deletions tide-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! err_fmt {
}

/// A convenient `Result` instantiation appropriate for most endpoints.
pub type EndpointResult<T = Response<Body>> = Result<T, Error>;
pub type Result<T = Response<Body>> = std::result::Result<T, Error>;

/// A generic endpoint error, which can be converted into a response.
#[derive(Debug)]
Expand Down Expand Up @@ -67,27 +67,27 @@ impl<T> ResponseExt for Response<T> {

/// Extends the `Result` type with convenient methods for constructing Tide errors.
pub trait ResultExt<T>: Sized {
/// Convert to an `EndpointResult`, treating the `Err` case as a client
/// Convert to an `tide::Result`, treating the `Err` case as a client
/// error (response code 400).
fn client_err(self) -> EndpointResult<T> {
fn client_err(self) -> Result<T> {
self.with_err_status(400)
}

/// Convert to an `EndpointResult`, treating the `Err` case as a server
/// Convert to an `tide::Result`, treating the `Err` case as a server
/// error (response code 500).
fn server_err(self) -> EndpointResult<T> {
fn server_err(self) -> Result<T> {
self.with_err_status(500)
}

/// Convert to an `EndpointResult`, wrapping the `Err` case with a custom
/// Convert to an `tide::Result`, wrapping the `Err` case with a custom
/// response status.
fn with_err_status<S>(self, status: S) -> EndpointResult<T>
fn with_err_status<S>(self, status: S) -> Result<T>
where
StatusCode: HttpTryFrom<S>;
}

impl<T, E: std::error::Error + Send + Sync + 'static> ResultExt<T> for std::result::Result<T, E> {
fn with_err_status<S>(self, status: S) -> EndpointResult<T>
fn with_err_status<S>(self, status: S) -> Result<T>
where
StatusCode: HttpTryFrom<S>,
{
Expand All @@ -99,27 +99,27 @@ impl<T, E: std::error::Error + Send + Sync + 'static> ResultExt<T> for std::resu
/// Extends the `Result` type using `std::error::Error` trait object as the error type with
/// convenient methods for constructing Tide errors.
pub trait ResultDynErrExt<T>: Sized {
/// Convert to an `EndpointResult`, treating the `Err` case as a client
/// Convert to an `tide::Result`, treating the `Err` case as a client
/// error (response code 400).
fn client_err(self) -> EndpointResult<T> {
fn client_err(self) -> Result<T> {
self.with_err_status(400)
}

/// Convert to an `EndpointResult`, treating the `Err` case as a server
/// Convert to an `tide::Result`, treating the `Err` case as a server
/// error (response code 500).
fn server_err(self) -> EndpointResult<T> {
fn server_err(self) -> Result<T> {
self.with_err_status(500)
}

/// Convert to an `EndpointResult`, wrapping the `Err` case with a custom
/// Convert to an `tide::Result`, wrapping the `Err` case with a custom
/// response status.
fn with_err_status<S>(self, status: S) -> EndpointResult<T>
fn with_err_status<S>(self, status: S) -> Result<T>
where
StatusCode: HttpTryFrom<S>;
}

impl<T> ResultDynErrExt<T> for std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
fn with_err_status<S>(self, status: S) -> EndpointResult<T>
fn with_err_status<S>(self, status: S) -> Result<T>
where
StatusCode: HttpTryFrom<S>,
{
Expand Down
2 changes: 1 addition & 1 deletion tide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use crate::{
app::{App, Server},
context::Context,
endpoint::Endpoint,
error::{EndpointResult, Error},
error::{Error, Result},
response::Response,
route::Route,
};
4 changes: 2 additions & 2 deletions tide/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::pin::Pin;
use futures::future::Future;

pub use tide_core::error::{EndpointResult, Error, ResponseExt, ResultExt, StringError};
pub use tide_core::error::{Error, ResponseExt, Result, ResultExt, StringError};

pub(crate) type BoxTryFuture<T> = Pin<Box<dyn Future<Output = EndpointResult<T>> + Send + 'static>>;
pub(crate) type BoxTryFuture<T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'static>>;
4 changes: 1 addition & 3 deletions tide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ pub mod middleware;
pub mod querystring;

#[doc(inline)]
pub use tide_core::{
response, App, Context, Endpoint, EndpointResult, Error, Response, Route, Server,
};
pub use tide_core::{response, App, Context, Endpoint, Error, Response, Result, Route, Server};

pub use http;