Reputation: 320
I'm trying to create a Rocket route that returns a string to the client, but I can't get it to work. So far, this is what I have:
#![feature(plugin)]
#![plugin(rocket_codegen)]
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate rocket;
mod utilities;
mod pipeline_config;
mod assets;
use std::path::PathBuf;
#[get("/resources/<path..>")]
fn get_resource(path: PathBuf) -> Result<String, ()> {
if let Some(page_path) = path.to_str() {
match assets::get(&format!("{}/{}", "resources", page_path)) {
Ok(page) => Ok(utf8_to_string(page)),
Err(e) => Err(()),
}
}
Err(())
}
fn main() {
let rocket_obj = rocket::ignite();
rocket_obj.mount("/", routes![get_resource]).launch();
}
pub fn utf8_to_string(bytes: &[u8]) -> String {
let vector: Vec<u8> = Vec::from(bytes);
String::from_utf8(vector).unwrap()
}
It looks like it should work, but it gives me an error expected (), found enum std::result::Result
:
error[E0308]: mismatched types
--> src/main.rs:18:9
|
18 | / match assets::get(&format!("{}/{}", "resources", page_path)) {
19 | | Ok(page) => Ok(utf8_to_string(page)),
20 | | Err(e) => Err(()),
21 | | }
| | ^- help: try adding a semicolon: `;`
| |_________|
| expected (), found enum `std::result::Result`
|
= note: expected type `()`
found type `std::result::Result<std::string::String, ()>`
which makes no sense to me because I'm returning a Result
with a String
.
Upvotes: 0
Views: 496
Reputation: 42849
When you use the functional-like style, see one function as one expression. Your function get_resource
has two expressions put side by side. The compiler does not understand anything. You must put the alternative in an else
branch, i.e.: if I could get some path, do this; else this is an error:
fn get_resource(path: PathBuf) -> Result<String, ()> {
if let Some(page_path) = path.to_str() {
match assets::get(&format!("{}/{}", "resources", page_path)) {
Ok(page) => Ok(utf8_to_string(page)),
Err(e) => Err(()),
}
} else {
Err(())
}
}
In other words, you can see a function as a logic tree where each if
or match
block is a set of branches. The program will follow a path in this tree depending on the input: so you must have only one trunk, otherwise it does not make sense.
Upvotes: 3