Skip to content

Commit

Permalink
Merge pull request #160 from EspressoSystems/fix/bincode
Browse files Browse the repository at this point in the history
Re-implement `StatusCode` to support bincode
  • Loading branch information
jbearer authored Dec 21, 2023
2 parents 57a9652 + 1bb0b2a commit 4ced31c
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 32 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tide-disco"
version = "0.4.4"
version = "0.4.6"
edition = "2021"
authors = ["Espresso Systems <[email protected]>"]
description = "Discoverability for Tide"
Expand Down Expand Up @@ -29,6 +29,8 @@ lazy_static = "1.4.0"
libc = "0.2.151"
markdown = "0.3"
maud = { version = "0.25", features = ["tide"] }
num-derive = "0.4"
num-traits = "0.2"
parking_lot = "0.12.0"
prometheus = "0.13"
routefinder = "0.5.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::FutureExt;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::io;
use tide_disco::{http::StatusCode, Api, App, Error, RequestError};
use tide_disco::{Api, App, Error, RequestError, StatusCode};
use tracing::info;

#[derive(Clone, Debug, Deserialize, Serialize, Snafu)]
Expand Down
5 changes: 2 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
request::{best_response_type, RequestParam, RequestParams},
route::{self, health_check_response, respond_with, Handler, Route, RouteError},
socket::SocketError,
Html,
Html, StatusCode,
};
use async_std::sync::Arc;
use futures::future::{BoxFuture, FutureExt};
Expand All @@ -33,7 +33,6 @@ use std::path::PathBuf;
use tide::{
http::{headers::HeaderValue, mime},
security::{CorsMiddleware, Origin},
StatusCode,
};
use tide_websockets::WebSocket;

Expand Down Expand Up @@ -166,7 +165,7 @@ impl<State: Send + Sync + 'static, Error: 'static> App<State, Error> {
let mut modules = HashMap::new();
let mut status = HealthStatus::Available;
for (name, api) in &self.apis {
let health = api.health(req.clone(), state).await.status();
let health = StatusCode::from(api.health(req.clone(), state).await.status());
if health != StatusCode::Ok {
status = HealthStatus::Unhealthy;
}
Expand Down
24 changes: 2 additions & 22 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
// You should have received a copy of the MIT License
// along with the tide-disco library. If not, see <https://mit-license.org/>.

use crate::{request::RequestError, route::RouteError, socket::SocketError};
use crate::{request::RequestError, route::RouteError, socket::SocketError, StatusCode};
use config::ConfigError;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use snafu::Snafu;
use std::fmt::Display;
use std::io::Error as IoError;
use tide::StatusCode;

/// Errors which can be serialized in a response body.
///
Expand Down Expand Up @@ -54,7 +53,7 @@ pub trait Error: std::error::Error + Serialize + DeserializeOwned + Send + Sync
fn from_server_error(source: tide::Error) -> Self {
match source.downcast::<Self>() {
Ok(err) => err,
Err(source) => Self::catch_all(source.status(), source.to_string()),
Err(source) => Self::catch_all(source.status().into(), source.to_string()),
}
}
}
Expand All @@ -67,29 +66,10 @@ pub trait Error: std::error::Error + Serialize + DeserializeOwned + Send + Sync
#[derive(Clone, Debug, Snafu, Serialize, Deserialize, PartialEq, Eq)]
#[snafu(display("Error {}: {}", status, message))]
pub struct ServerError {
#[serde(with = "ser_status")]
pub status: StatusCode,
pub message: String,
}

mod ser_status {
//! The deserialization implementation for [StatusCode] uses `deserialize_any` unnecessarily,
//! which prevents it from working with [bincode].
use super::*;
use serde::{
de::{Deserializer, Error},
ser::Serializer,
};

pub fn serialize<S: Serializer>(status: &StatusCode, s: S) -> Result<S::Ok, S::Error> {
u16::from(*status).serialize(s)
}

pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<StatusCode, D::Error> {
u16::deserialize(d)?.try_into().map_err(D::Error::custom)
}
}

impl Error for ServerError {
fn catch_all(status: StatusCode, message: String) -> Self {
Self { status, message }
Expand Down
2 changes: 1 addition & 1 deletion src/healthcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// You should have received a copy of the MIT License
// along with the tide-disco library. If not, see <https://mit-license.org/>.

use crate::StatusCode;
use serde::{Deserialize, Serialize};
use tide::StatusCode;

/// A response to a healthcheck endpoint.
///
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,15 @@ pub mod metrics;
pub mod request;
pub mod route;
pub mod socket;
pub mod status;

pub use api::Api;
pub use app::App;
pub use error::Error;
pub use method::Method;
pub use request::{RequestError, RequestParam, RequestParamType, RequestParamValue, RequestParams};
pub use tide::http::{self, StatusCode};
pub use status::StatusCode;
pub use tide::http;
pub use url::Url;

pub type Html = maud::Markup;
Expand Down
3 changes: 1 addition & 2 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
metrics,
request::{best_response_type, RequestError, RequestParam, RequestParamType, RequestParams},
socket::{self, SocketError},
Html,
Html, StatusCode,
};
use async_std::sync::Arc;
use async_trait::async_trait;
Expand All @@ -32,7 +32,6 @@ use tide::{
self,
content::Accept,
mime::{self, Mime},
StatusCode,
},
Body,
};
Expand Down
3 changes: 2 additions & 1 deletion src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
//! An interface for asynchronous communication with clients, using WebSockets.

use crate::{
http::{content::Accept, mime, StatusCode},
http::{content::Accept, mime},
method::Method,
request::{best_response_type, RequestError, RequestParams},
StatusCode,
};
use async_std::sync::Arc;
use futures::{
Expand Down
Loading

0 comments on commit 4ced31c

Please sign in to comment.