diff --git a/content/cat.md b/content/cat.md new file mode 100644 index 0000000..3b5058c --- /dev/null +++ b/content/cat.md @@ -0,0 +1,3 @@ +Let's say this page talks about cats. + +We will want to _insist_ on some. diff --git a/content/sunny.md b/content/sunny.md new file mode 100644 index 0000000..0c9acb2 --- /dev/null +++ b/content/sunny.md @@ -0,0 +1 @@ +This could be any sunny day. diff --git a/src/main.rs b/src/main.rs index c9d872b..91bc321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,33 +9,35 @@ use rocket::request::{Request, FromParam}; use rocket::http::RawStr; use rocket_contrib::templates::Template; +const CONTENT_DIR:&str = "content"; + #[derive(serde::Serialize, Debug)] -struct User { - name: String, +struct Content { + title: String, content: String, } -impl<'r> FromParam<'r> for User { +impl<'r> FromParam<'r> for Content { type Error = &'r RawStr; fn from_param(param: &'r RawStr) -> Result { - let filename = format!("users/{}.md", param); + let filename = format!("{}/{}.md", CONTENT_DIR, param); let content = fs::read_to_string(&filename); match content { - Ok(value) => Ok(User{ name: param.to_string(), content: value }), + Ok(value) => Ok(Content{ title: param.to_string(), content: value }), Err(_) => Err(param), } } } -fn list_users() -> io::Result> { - let mut users: Vec = vec!(); - for path in fs::read_dir("users")? { +fn list_contents() -> io::Result> { + let mut contents: Vec = vec!(); + for path in fs::read_dir(CONTENT_DIR)? { let file = path?; - let user = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", ""); - users.push(user); + let content = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", ""); + contents.push(content); } - Ok(users) + Ok(contents) } #[derive(serde::Serialize)] @@ -54,16 +56,15 @@ fn not_found(req: &Request<'_>) -> Template { Template::render("error/404", &map) } -#[get("/")] -fn show(user: User) -> Template { - //format!("{:#?}", user) - Template::render("user", &user) +#[get("/")] +fn show(content: Content) -> Template { + Template::render("content", &content) } #[get("/")] fn index() -> io::Result