From f7ae2e65d2ae5ee93080ee84490e5cd03a68dfa9 Mon Sep 17 00:00:00 2001 From: Yong Wen Chua Date: Tue, 18 Dec 2018 17:43:13 +0800 Subject: [PATCH] Update examples --- examples/fairing.rs | 9 +++++---- examples/guard.rs | 13 ++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/fairing.rs b/examples/fairing.rs index 70a2d64..c82a89e 100644 --- a/examples/fairing.rs +++ b/examples/fairing.rs @@ -4,14 +4,14 @@ use rocket_cors; use rocket::http::Method; use rocket::{get, routes}; -use rocket_cors::{AllowedHeaders, AllowedOrigins}; +use rocket_cors::{AllowedHeaders, AllowedOrigins, Error}; #[get("/")] fn cors<'a>() -> &'a str { "Hello CORS" } -fn main() { +fn main() -> Result<(), Error> { let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]); assert!(failed_origins.is_empty()); @@ -23,11 +23,12 @@ fn main() { allow_credentials: true, ..Default::default() } - .to_cors() - .expect("To not fail"); + .to_cors()?; rocket::ignite() .mount("/", routes![cors]) .attach(cors) .launch(); + + Ok(()) } diff --git a/examples/guard.rs b/examples/guard.rs index 01834fb..20d8705 100644 --- a/examples/guard.rs +++ b/examples/guard.rs @@ -7,7 +7,7 @@ use std::io::Cursor; use rocket::http::Method; use rocket::Response; use rocket::{get, options, routes}; -use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder}; +use rocket_cors::{AllowedHeaders, AllowedOrigins, Error, Guard, Responder}; /// Using a `Responder` -- the usual way you would use this #[get("/")] @@ -35,18 +35,19 @@ fn manual(cors: Guard<'_>) -> Responder<'_, &str> { cors.responder("Manual OPTIONS preflight handling") } -fn main() { +fn main() -> Result<(), Error> { let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]); assert!(failed_origins.is_empty()); // You can also deserialize this - let options = rocket_cors::CorsOptions { + let cors = rocket_cors::CorsOptions { allowed_origins: allowed_origins, allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(), allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]), allow_credentials: true, ..Default::default() - }; + } + .to_cors()?; rocket::ignite() .mount("/", routes![responder, response]) @@ -54,6 +55,8 @@ fn main() { .mount("/", rocket_cors::catch_all_options_routes()) // You can also manually mount an OPTIONS route that will be used instead .mount("/", routes![manual, manual_options]) - .manage(options.to_cors().expect("Not to fail")) + .manage(cors) .launch(); + + Ok(()) }