如何使用actix-web从以下URL 解析name和color参数?
http://example.com/path/to/page?name=ferret&color=purple
Run Code Online (Sandbox Code Playgroud)
我想我的路径应该是/path/to/page,然后当我尝试查询时name收到一个空字符串(req.match_info().query("name")where req: &HttpRequest)。
我发现的唯一文档是关于匹配名称的(例如,如果路径/people/{page}/匹配/people/123/,则这样匹配,page = 123但这不是我想要的)。
我想StructOpt使用枚举,这样每次用户传递-d sunday它时都会被解析为Day::Sunday:
#[macro_use]
extern crate structopt;
use std::path::PathBuf;
use structopt::StructOpt;
// My enum
enum Day {
Sunday, Monday
}
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
/// Set speed
#[structopt(short = "s", long = "speed", default_value = "42")]
speed: f64,
/// Input file
#[structopt(parse(from_os_str))]
input: PathBuf,
/// Day of the week
#[structopt(short = "d", long = "day", default_value = Day::Monday)]
day: Day,
}
fn main() { …Run Code Online (Sandbox Code Playgroud) 我有一个rusoto_core::ByteStream实现期货的Stream特征:
let chunks = vec![b"1234".to_vec(), b"5678".to_vec()];
let stream = ByteStream::new(stream::iter_ok(chunks));
Run Code Online (Sandbox Code Playgroud)
我想将它传递给actix_web 的HttpResponseBuilder::streaming方法。
use actix_web::dev::HttpResponseBuilder; // 0.7.18
use rusoto_core::ByteStream; // 0.36.0
fn example(stream: ByteStream, builder: HttpResponseBuilder) {
builder.streaming(stream);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时,我收到以下错误:
let chunks = vec![b"1234".to_vec(), b"5678".to_vec()];
let stream = ByteStream::new(stream::iter_ok(chunks));
Run Code Online (Sandbox Code Playgroud)
我相信原因是streaming()期望 a S: Stream<Item = Bytes, Error>(ie, Item = Bytes) 但我ByteStream有Item = Vec<u8>. 我该如何解决?
我认为解决方案是flatmap我的,ByteStream但我找不到这样的流方法。
这是一个如何streaming()使用的示例:
let text = …Run Code Online (Sandbox Code Playgroud) #[macro_use]
extern crate failure;
use actix_web::{error, http, HttpResponse};
#[derive(Fail, Debug)]
enum UserError {
#[fail(display = "Validation error on field: {}", field)]
ValidationError { field: String },
}
impl error::ResponseError for UserError {
fn error_response(&self) -> HttpResponse {
match *self {
UserError::ValidationError { .. } =>
HttpResponse::new(http::StatusCode::BAD_REQUEST),
}
}
}
Run Code Online (Sandbox Code Playgroud)
{ .. }这里的意思是什么?