diff options
| author | Christine Dodrill <me@christine.website> | 2020-09-27 12:35:24 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-27 12:35:24 -0400 |
| commit | f106c2c9d23c9c12a88b5a73f704f4be5c455926 (patch) | |
| tree | 6b3888c1bcf2f8c379acbc897bab7f3fa7b9c8ca /examples | |
| parent | e460ebdcbee67224a3f9872b0a59e6f2733b6861 (diff) | |
| download | xesite-f106c2c9d23c9c12a88b5a73f704f4be5c455926.tar.xz xesite-f106c2c9d23c9c12a88b5a73f704f4be5c455926.zip | |
go-stdlib-rust post (#215)
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/http.rs | 27 | ||||
| -rw-r--r-- | examples/http_fail.rs | 28 | ||||
| -rw-r--r-- | examples/json.rs | 32 | ||||
| -rw-r--r-- | examples/logger_test.rs | 11 | ||||
| -rw-r--r-- | examples/warp.rs | 14 |
5 files changed, 112 insertions, 0 deletions
diff --git a/examples/http.rs b/examples/http.rs new file mode 100644 index 0000000..c1533a6 --- /dev/null +++ b/examples/http.rs @@ -0,0 +1,27 @@ +use eyre::Result; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Author { + pub id: i32, + pub name: String, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Comment { + pub id: i32, + pub author: Author, + pub body: String, + pub in_reply_to: i32, +} + +#[tokio::main] +async fn main() -> Result<()> { + let c: Comment = reqwest::get("https://xena.greedo.xeserv.us/files/comment.json") + .await? + .json() + .await?; + println!("comment: {:#?}", c); + + Ok(()) +} diff --git a/examples/http_fail.rs b/examples/http_fail.rs new file mode 100644 index 0000000..98e027a --- /dev/null +++ b/examples/http_fail.rs @@ -0,0 +1,28 @@ +use eyre::Result; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Author { + pub id: i32, + pub name: String, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Comment { + pub id: i32, + pub author: Author, + pub body: String, + pub in_reply_to: i32, +} + +#[tokio::main] +async fn main() -> Result<()> { + let c: Comment = reqwest::get("https://xena.greedo.xeserv.us/files/comment2.json") + .await? + .error_for_status()? + .json() + .await?; + println!("comment: {:#?}", c); + + Ok(()) +} diff --git a/examples/json.rs b/examples/json.rs new file mode 100644 index 0000000..bf128ed --- /dev/null +++ b/examples/json.rs @@ -0,0 +1,32 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Author { + pub id: i32, + pub name: String, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Comment { + pub id: i32, + pub author: Author, + pub body: String, + pub in_reply_to: i32, +} + +fn main() { + let data = r#" + { + "id": 31337, + "author": { + "id": 420, + "name": "Cadey" + }, + "body": "hahaha its is an laughter image", + "in_reply_to": 31335 + } + "#; + + let c: Comment = serde_json::from_str(data).expect("json to parse"); + println!("comment: {:#?}", c); +} diff --git a/examples/logger_test.rs b/examples/logger_test.rs new file mode 100644 index 0000000..f917d66 --- /dev/null +++ b/examples/logger_test.rs @@ -0,0 +1,11 @@ +use log::{debug, error, info, trace, warn}; + +fn main() { + pretty_env_logger::init(); + + trace!("starting main"); + debug!("debug message"); + info!("this is some information"); + warn!("oh no something bad is about to happen"); + error!("oh no it's an error"); +} diff --git a/examples/warp.rs b/examples/warp.rs new file mode 100644 index 0000000..c705b47 --- /dev/null +++ b/examples/warp.rs @@ -0,0 +1,14 @@ +use warp::Filter; + +#[tokio::main] +async fn main() { + let hello = warp::path!("hello" / String) + .map(|name| format!("Hello, {}!", name)); + let health = warp::path!(".within" / "health") + .map(|| "OK"); + let routes = hello.or(health); + + warp::serve(routes) + .run(([0, 0, 0, 0], 3030)) + .await; +} |
