Skip to content

Commit ebac12d

Browse files
committed
all tests pass
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent be35ce6 commit ebac12d

File tree

3 files changed

+101
-104
lines changed

3 files changed

+101
-104
lines changed

src/endpoint.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ use crate::{response::IntoResponse, Request, Response};
1919
///
2020
/// A simple endpoint that is invoked on a `GET` request and returns a `String`:
2121
///
22-
/// ```rust, no_run
22+
/// ```no_run
2323
/// async fn hello(_cx: tide::Request<()>) -> String {
2424
/// String::from("hello")
2525
/// }
2626
///
2727
/// fn main() {
2828
/// let mut app = tide::Server::new();
2929
/// app.at("/hello").get(hello);
30-
/// app.run("127.0.0.1:8000").unwrap()
3130
/// }
3231
/// ```
3332
///
3433
/// An endpoint with similar functionality that does not make use of the `async` keyword would look something like this:
3534
///
36-
/// ```rust, no_run
35+
/// ```no_run
3736
/// # use core::future::Future;
3837
/// fn hello(_cx: tide::Request<()>) -> impl Future<Output = String> {
3938
/// futures::future::ready(String::from("hello"))
@@ -42,7 +41,6 @@ use crate::{response::IntoResponse, Request, Response};
4241
/// fn main() {
4342
/// let mut app = tide::Server::new();
4443
/// app.at("/hello").get(hello);
45-
/// app.run("127.0.0.1:8000").unwrap()
4644
/// }
4745
/// ```
4846
///

src/response/into_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub trait IntoResponse: Send + Sized {
88
/// Create a new `IntoResponse` value that will respond with the given status code.
99
///
1010
/// ```
11-
/// # use tide::response::IntoResponse;
11+
/// # use tide::IntoResponse;
1212
/// let resp = "Hello, 404!".with_status(http::status::StatusCode::NOT_FOUND).into_response();
1313
/// assert_eq!(resp.status(), http::status::StatusCode::NOT_FOUND);
1414
/// ```

src/server/mod.rs

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -37,105 +37,104 @@ pub use route::Route;
3737
/// - Middleware extends the base Tide framework with additional request or
3838
/// response processing, such as compression, default headers, or logging. To
3939
/// add middleware to an app, use the [`Server::middleware`] method.
40-
///
41-
/// # Hello, world!
42-
///
43-
/// You can start a simple Tide application that listens for `GET` requests at path `/hello`
44-
/// on `127.0.0.1:8000` with:
45-
///
46-
/// ```rust, no_run
47-
///
48-
/// let mut app = tide::Server::new();
49-
/// app.at("/hello").get(|_| async move {"Hello, world!"});
50-
/// app.run("127.0.0.1:8000").unwrap();
51-
/// ```
52-
///
53-
/// # Routing and parameters
54-
///
55-
/// Tide's routing system is simple and similar to many other frameworks. It
56-
/// uses `:foo` for "wildcard" URL segments, and `*foo` to match the rest of a
57-
/// URL (which may include multiple segments). Here's an example using wildcard
58-
/// segments as parameters to endpoints:
59-
///
60-
/// ```rust, no_run
61-
/// use tide::error::ResultExt;
62-
///
63-
/// async fn hello(cx: tide::Request<()>) -> tide::Result<String> {
64-
/// let user: String = cx.param("user").client_err()?;
65-
/// Ok(format!("Hello, {}!", user))
66-
/// }
67-
///
68-
/// async fn goodbye(cx: tide::Request<()>) -> tide::Result<String> {
69-
/// let user: String = cx.param("user").client_err()?;
70-
/// Ok(format!("Goodbye, {}.", user))
71-
/// }
72-
///
73-
/// let mut app = tide::Server::new();
74-
///
75-
/// app.at("/hello/:user").get(hello);
76-
/// app.at("/goodbye/:user").get(goodbye);
77-
/// app.at("/").get(|_| async move {
78-
/// "Use /hello/{your name} or /goodbye/{your name}"
79-
/// });
80-
///
81-
/// app.run("127.0.0.1:8000").unwrap();
82-
/// ```
83-
///
84-
/// You can learn more about routing in the [`Server::at`] documentation.
85-
///
86-
/// # Serverlication state
87-
///
88-
/// ```rust, no_run
89-
///
90-
/// use http::status::StatusCode;
91-
/// use serde::{Deserialize, Serialize};
92-
/// use std::sync::Mutex;
93-
/// use tide::{error::ResultExt, response, Server, Request, Result};
94-
///
95-
/// #[derive(Default)]
96-
/// struct Database {
97-
/// contents: Mutex<Vec<Message>>,
98-
/// }
99-
///
100-
/// #[derive(Serialize, Deserialize, Clone)]
101-
/// struct Message {
102-
/// author: Option<String>,
103-
/// contents: String,
104-
/// }
105-
///
106-
/// impl Database {
107-
/// fn insert(&self, msg: Message) -> usize {
108-
/// let mut table = self.contents.lock().unwrap();
109-
/// table.push(msg);
110-
/// table.len() - 1
111-
/// }
112-
///
113-
/// fn get(&self, id: usize) -> Option<Message> {
114-
/// self.contents.lock().unwrap().get(id).cloned()
115-
/// }
116-
/// }
117-
///
118-
/// async fn new_message(mut cx: Request<Database>) -> Result<String> {
119-
/// let msg = cx.body_json().await.client_err()?;
120-
/// Ok(cx.state().insert(msg).to_string())
121-
/// }
122-
///
123-
/// async fn get_message(cx: Request<Database>) -> Result {
124-
/// let id = cx.param("id").client_err()?;
125-
/// if let Some(msg) = cx.state().get(id) {
126-
/// Ok(response::json(msg))
127-
/// } else {
128-
/// Err(StatusCode::NOT_FOUND)?
129-
/// }
130-
/// }
131-
///
132-
/// fn main() {
133-
/// let mut app = Server::with_state(Database::default());
134-
/// app.at("/message").post(new_message);
135-
/// app.at("/message/:id").get(get_message);
136-
/// app.run("127.0.0.1:8000").unwrap();
137-
/// }
138-
/// ```
40+
/////
41+
///// # Hello, world!
42+
/////
43+
///// You can start a simple Tide application that listens for `GET` requests at path `/hello`
44+
///// on `127.0.0.1:8000` with:
45+
/////
46+
///// ```rust, no_run
47+
/////
48+
///// let mut app = tide::Server::new();
49+
///// app.at("/hello").get(|_| async move {"Hello, world!"});
50+
///// // app.run("127.0.0.1:8000").unwrap();
51+
///// ```
52+
/////
53+
///// # Routing and parameters
54+
/////
55+
///// Tide's routing system is simple and similar to many other frameworks. It
56+
///// uses `:foo` for "wildcard" URL segments, and `*foo` to match the rest of a
57+
///// URL (which may include multiple segments). Here's an example using wildcard
58+
///// segments as parameters to endpoints:
59+
/////
60+
///// ```no_run
61+
///// use tide::error::ResultExt;
62+
/////
63+
///// async fn hello(cx: tide::Request<()>) -> tide::Result<String> {
64+
///// let user: String = cx.param("user")?;
65+
///// Ok(format!("Hello, {}!", user))
66+
///// }
67+
/////
68+
///// async fn goodbye(cx: tide::Request<()>) -> tide::Result<String> {
69+
///// let user: String = cx.param("user")?;
70+
///// Ok(format!("Goodbye, {}.", user))
71+
///// }
72+
/////
73+
///// let mut app = tide::Server::new();
74+
/////
75+
///// app.at("/hello/:user").get(hello);
76+
///// app.at("/goodbye/:user").get(goodbye);
77+
///// app.at("/").get(|_| async move {
78+
///// "Use /hello/{your name} or /goodbye/{your name}"
79+
///// });
80+
/////
81+
///// // app.run("127.0.0.1:8000").unwrap();
82+
///// ```
83+
/////
84+
///// You can learn more about routing in the [`Server::at`] documentation.
85+
/////
86+
///// # Serverlication state
87+
/////
88+
///// ```rust,no_run
89+
///// use http::status::StatusCode;
90+
///// use serde::{Deserialize, Serialize};
91+
///// use std::sync::Mutex;
92+
///// use tide::{error::ResultExt, Server, Request, Result};
93+
/////
94+
///// #[derive(Default)]
95+
///// struct Database {
96+
///// contents: Mutex<Vec<Message>>,
97+
///// }
98+
/////
99+
///// #[derive(Serialize, Deserialize, Clone)]
100+
///// struct Message {
101+
///// author: Option<String>,
102+
///// contents: String,
103+
///// }
104+
/////
105+
///// impl Database {
106+
///// fn insert(&self, msg: Message) -> usize {
107+
///// let mut table = self.contents.lock().unwrap();
108+
///// table.push(msg);
109+
///// table.len() - 1
110+
///// }
111+
/////
112+
///// fn get(&self, id: usize) -> Option<Message> {
113+
///// self.contents.lock().unwrap().get(id).cloned()
114+
///// }
115+
///// }
116+
/////
117+
///// async fn new_message(mut cx: Request<Database>) -> Result<String> {
118+
///// let msg = cx.body_json().await?;
119+
///// Ok(cx.state().insert(msg).to_string())
120+
///// }
121+
/////
122+
///// async fn get_message(cx: Request<Database>) -> Result {
123+
///// let id = cx.param("id").unwrap();
124+
///// if let Some(msg) = cx.state().get(id) {
125+
///// Ok(response::json(msg))
126+
///// } else {
127+
///// Err(StatusCode::NOT_FOUND)?
128+
///// }
129+
///// }
130+
/////
131+
///// fn main() {
132+
///// let mut app = Server::with_state(Database::default());
133+
///// app.at("/message").post(new_message);
134+
///// app.at("/message/:id").get(get_message);
135+
///// // app.run("127.0.0.1:8000").unwrap();
136+
///// }
137+
///// ```
139138
#[allow(missing_debug_implementations)]
140139
pub struct Server<State> {
141140
router: Router<State>,

0 commit comments

Comments
 (0)