reorganize std::fs and std::io imports
This commit is contained in:
parent
79d66cfc4a
commit
84354c3834
17
src/main.rs
17
src/main.rs
|
@ -2,14 +2,12 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
|
|
||||||
//use std::fmt::Display;
|
use std::result::Result;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
//use rocket::Request;
|
use std::io;
|
||||||
use rocket_contrib::templates::Template;
|
|
||||||
//use rocket::request::{Outcome, Request, FromRequest, FromParam};
|
|
||||||
use rocket::request::{Request, FromParam};
|
use rocket::request::{Request, FromParam};
|
||||||
//use rocket::http::Status;
|
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
|
use rocket_contrib::templates::Template;
|
||||||
|
|
||||||
#[derive(serde::Serialize, Debug)]
|
#[derive(serde::Serialize, Debug)]
|
||||||
struct User {
|
struct User {
|
||||||
|
@ -20,7 +18,7 @@ struct User {
|
||||||
impl<'r> FromParam<'r> for User {
|
impl<'r> FromParam<'r> for User {
|
||||||
type Error = &'r RawStr;
|
type Error = &'r RawStr;
|
||||||
|
|
||||||
fn from_param(param: &'r RawStr) -> std::result::Result<Self, Self::Error> {
|
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
|
||||||
let filename = format!("users/{}.md", param);
|
let filename = format!("users/{}.md", param);
|
||||||
let content = fs::read_to_string(&filename);
|
let content = fs::read_to_string(&filename);
|
||||||
match content {
|
match content {
|
||||||
|
@ -30,7 +28,7 @@ impl<'r> FromParam<'r> for User {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_users() -> std::io::Result<Vec<String>> {
|
fn list_users() -> io::Result<Vec<String>> {
|
||||||
let mut users: Vec<String> = vec!();
|
let mut users: Vec<String> = vec!();
|
||||||
for path in fs::read_dir("users")? {
|
for path in fs::read_dir("users")? {
|
||||||
let file = path?;
|
let file = path?;
|
||||||
|
@ -63,16 +61,13 @@ fn show(user: User) -> Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> std::io::Result<Template> {
|
fn index() -> io::Result<Template> {
|
||||||
let users = list_users()?;
|
let users = list_users()?;
|
||||||
let context = IndexContext { name: "home", items: users };
|
let context = IndexContext { name: "home", items: users };
|
||||||
Ok(Template::render("index", &context))
|
Ok(Template::render("index", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
//let user_map = get_user_map();
|
|
||||||
//println!("{:#?}", user_map);
|
|
||||||
|
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![index, show])
|
.mount("/", routes![index, show])
|
||||||
|
|
Loading…
Reference in New Issue