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 Context<Data> to Context<State> #260

Merged
merged 1 commit into from
May 22, 2019
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
16 changes: 8 additions & 8 deletions examples/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use juniper::graphql_object;
use std::sync::{atomic, Arc};
use tide::{error::ResultExt, response, App, Context, EndpointResult};

// First, we define `Data` that holds accumulator state. This is accessible as App data in
// First, we define `State` that holds accumulator state. This is accessible as state in
// Tide, and as executor context in Juniper.
#[derive(Clone, Default)]
struct Data(Arc<atomic::AtomicIsize>);
struct State(Arc<atomic::AtomicIsize>);

impl juniper::Context for Data {}
impl juniper::Context for State {}

// We define `Query` unit struct here. GraphQL queries will refer to this struct. The struct itself
// doesn't have any associated data (and there's no need to do so), but instead it exposes the
// doesn't have any associated state (and there's no need to do so), but instead it exposes the
// accumulator state from the context.
struct Query;

graphql_object!(Query: Data |&self| {
graphql_object!(Query: State |&self| {
// GraphQL integers are signed and 32 bits long.
field accumulator(&executor) -> i32 as "Current value of the accumulator" {
executor.context().0.load(atomic::Ordering::Relaxed) as i32
Expand All @@ -31,7 +31,7 @@ graphql_object!(Query: Data |&self| {
// `Query`, but it provides the way to "mutate" the accumulator state.
struct Mutation;

graphql_object!(Mutation: Data |&self| {
graphql_object!(Mutation: State |&self| {
field add(&executor, by: i32) -> i32 as "Add given value to the accumulator." {
executor.context().0.fetch_add(by as isize, atomic::Ordering::Relaxed) as i32 + by
}
Expand All @@ -43,7 +43,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<State>) -> EndpointResult {
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 All @@ -58,7 +58,7 @@ async fn handle_graphql(mut cx: Context<Data>) -> EndpointResult {
}

fn main() {
let mut app = App::with_state(Data::default());
let mut app = App::with_state(State::default());
app.at("/graphql").post(handle_graphql);
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion src/querystring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait ContextExt<'de> {
fn url_query<T: Deserialize<'de>>(&'de self) -> Result<T, Error>;
}

impl<'de, Data> ContextExt<'de> for Context<Data> {
impl<'de, State> ContextExt<'de> for Context<State> {
#[inline]
fn url_query<T: Deserialize<'de>>(&'de self) -> Result<T, Error> {
let query = self.uri().query();
Expand Down
10 changes: 5 additions & 5 deletions tide-compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl Compression {
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for Compression {
fn handle<'a>(&'a self, cx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
impl<State: Send + Sync + 'static> Middleware<State> for Compression {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move {
let encoding = match self.preferred_encoding(cx.headers()) {
Ok(encoding) => encoding,
Expand Down Expand Up @@ -212,11 +212,11 @@ impl Decompression {
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for Decompression {
impl<State: Send + Sync + 'static> Middleware<State> for Decompression {
fn handle<'a>(
&'a self,
mut cx: Context<Data>,
next: Next<'a, Data>,
mut cx: Context<State>,
next: Next<'a, State>,
) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move {
match self.decode(cx.request_mut()) {
Expand Down
6 changes: 3 additions & 3 deletions tide-cookies/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ impl CookiesMiddleware {
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for CookiesMiddleware {
impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
fn handle<'a>(
&'a self,
mut cx: Context<Data>,
next: Next<'a, Data>,
mut cx: Context<State>,
next: Next<'a, State>,
) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move {
let cookie_data = cx
Expand Down
20 changes: 10 additions & 10 deletions tide-core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ use crate::{
pub struct App<State> {
router: Router<State>,
middleware: Vec<Arc<dyn Middleware<State>>>,
data: State,
state: State,
}

impl App<()> {
Expand All @@ -155,7 +155,7 @@ impl<State: Send + Sync + 'static> App<State> {
App {
router: Router::new(),
middleware: Vec::new(),
data: state,
state,
}
}

Expand Down Expand Up @@ -229,7 +229,7 @@ impl<State: Send + Sync + 'static> App<State> {
pub fn into_http_service(self) -> Server<State> {
Server {
router: Arc::new(self.router),
data: Arc::new(self.data),
state: Arc::new(self.state),
middleware: Arc::new(self.middleware),
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<State: Send + Sync + 'static> App<State> {
#[allow(missing_debug_implementations)]
pub struct Server<State> {
router: Arc<Router<State>>,
data: Arc<State>,
state: Arc<State>,
middleware: Arc<Vec<Arc<dyn Middleware<State>>>>,
}

Expand All @@ -291,12 +291,12 @@ impl<State: Sync + Send + 'static> HttpService for Server<State> {
let method = req.method().to_owned();
let router = self.router.clone();
let middleware = self.middleware.clone();
let data = self.data.clone();
let state = self.state.clone();

FutureExt::boxed(async move {
let fut = {
let Selection { endpoint, params } = router.route(&path, method);
let cx = Context::new(data, req, params);
let cx = Context::new(state, req, params);

let next = Next {
endpoint,
Expand All @@ -319,19 +319,19 @@ mod tests {
use super::*;
use crate::{middleware::Next, router::Selection, Context, Response};

fn simulate_request<'a, Data: Default + Clone + Send + Sync + 'static>(
app: &'a App<Data>,
fn simulate_request<'a, State: Default + Clone + Send + Sync + 'static>(
app: &'a App<State>,
path: &'a str,
method: http::Method,
) -> BoxFuture<'a, Response> {
let Selection { endpoint, params } = app.router.route(path, method.clone());

let data = Arc::new(Data::default());
let state = Arc::new(State::default());
let req = http::Request::builder()
.method(method)
.body(http_service::Body::empty())
.unwrap();
let cx = Context::new(data, req, params);
let cx = Context::new(state, req, params);
let next = Next {
endpoint,
next_middleware: &app.middleware,
Expand Down
4 changes: 2 additions & 2 deletions tide-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use http_service::Body;
use route_recognizer::Params;
use std::{str::FromStr, sync::Arc};

/// Data associated with a request-response lifecycle.
/// State associated with a request-response lifecycle.
///
/// The `Context` gives endpoints access to basic information about the incoming
/// request, route parameters, and various ways of accessing the request's body.
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<State> Context<State> {
&mut self.request
}

/// Access app-global data.
/// Access the state.
pub fn state(&self) -> &State {
&self.state
}
Expand Down
9 changes: 6 additions & 3 deletions tide-core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ pub trait Middleware<State>: 'static + Send + Sync {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response>;
}

impl<Data, F> Middleware<Data> for F
impl<State, F> Middleware<State> for F
where
F: Send + Sync + 'static + for<'a> Fn(Context<Data>, Next<'a, Data>) -> BoxFuture<'a, Response>,
F: Send
+ Sync
+ 'static
+ for<'a> Fn(Context<State>, Next<'a, State>) -> BoxFuture<'a, Response>,
{
fn handle<'a>(&'a self, cx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
(self)(cx, next)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tide-core/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<State: 'static> Router<State> {
}
}

fn not_found_endpoint<Data>(_cx: Context<Data>) -> BoxFuture<'static, Response> {
fn not_found_endpoint<State>(_cx: Context<State>) -> BoxFuture<'static, Response> {
FutureExt::boxed(async move {
http::Response::builder()
.status(http::StatusCode::NOT_FOUND)
Expand Down
4 changes: 2 additions & 2 deletions tide-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl DefaultHeaders {
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for DefaultHeaders {
fn handle<'a>(&'a self, cx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
impl<State: Send + Sync + 'static> Middleware<State> for DefaultHeaders {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move {
let mut res = next.run(cx).await;
let headers = res.headers_mut();
Expand Down
10 changes: 5 additions & 5 deletions tide-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ impl RequestLogger {
Self { target }
}

async fn log_basic<'a, Data: Send + Sync + 'static>(
async fn log_basic<'a, State: Send + Sync + 'static>(
&'a self,
ctx: Context<Data>,
next: Next<'a, Data>,
ctx: Context<State>,
next: Next<'a, State>,
) -> Response {
let path = ctx.uri().path().to_owned();
let method = ctx.method().as_str().to_owned();
Expand All @@ -70,8 +70,8 @@ impl RequestLogger {
}
}

impl<Data: Send + Sync + 'static> Middleware<Data> for RequestLogger {
fn handle<'a>(&'a self, ctx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
impl<State: Send + Sync + 'static> Middleware<State> for RequestLogger {
fn handle<'a>(&'a self, ctx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move { self.log_basic(ctx, next).await })
}
}
4 changes: 2 additions & 2 deletions tide-slog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Default for RequestLogger {

/// Stores information during request phase and logs information once the response
/// is generated.
impl<Data: Send + Sync + 'static> Middleware<Data> for RequestLogger {
fn handle<'a>(&'a self, cx: Context<Data>, next: Next<'a, Data>) -> BoxFuture<'a, Response> {
impl<State: Send + Sync + 'static> Middleware<State> for RequestLogger {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
FutureExt::boxed(async move {
let path = cx.uri().path().to_owned();
let method = cx.method().as_str().to_owned();
Expand Down