Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 19, 2018 00:33
Show Gist options
  • Save rust-play/3a7a9b9bdc03aaac9b17d86d8e74af8c to your computer and use it in GitHub Desktop.
Save rust-play/3a7a9b9bdc03aaac9b17d86d8e74af8c to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// error[E0277]: the trait bound `(): actix_web::ResponseError` is not satisfied
// --> src/main.rs:25:32
// |
//25 | r.method(http::Method::GET).with_async(index)
// | ^^^^^^^^^^ the trait `actix_web::ResponseError` is not implemented for `()`
// |
// = note: required because of the requirements on the impl of `std::convert::From<()>` for `actix_web::Error`
// = note: required because of the requirements on the impl of `std::convert::Into<actix_web::Error>` for `()`
extern crate hyper;
use std::io::{self, Write};
use hyper::{Client, Method, Request};
use hyper::rt::{self, Future, Stream};
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde_json::{Value};
extern crate actix_web;
use actix_web::{http, server, App, Path, Responder};
fn index(info: Path<(u32, String)>) -> impl Future<Item = String, Error = ()> {
let client = Client::new();
let url = "http://some.json.url".parse().unwrap();
fetch_url(url)
}
fn main() {
server::new(
|| App::new()
.resource("/{id}/{name}/index.html", |r| {
r.method(http::Method::GET).with_async(index)
})
).bind("127.0.0.1:8080").unwrap().run()
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item = String, Error = ()> {
let client = Client::new();
client
// Fetch the url...
.get(url)
// And then, if we get a response back...
.and_then(|res| {
res.into_body().concat2()
})
// If all good, just tell the user...
.map(move |buffer| {
let parsed = serde_json::from_slice::<Value>(&buffer).unwrap();
println!("Done. {:#?}", parsed[0]["TRAIN_ID"]);
format!("done: {:#?}", parsed[0]["TRAIN_ID"])
})
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment