Add nice html formatting for search
This commit is contained in:
30
src/search_result.rs
Normal file
30
src/search_result.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::elastic_response::File;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct SearchResult {
|
||||
pub name: String,
|
||||
pub modified: String,
|
||||
pub size: usize,
|
||||
}
|
||||
|
||||
impl SearchResult {
|
||||
pub fn to_html(&self) -> String {
|
||||
format!(
|
||||
"<li><span><a href=\"/{name}\">{name}</a>({size} Bytes)</li>{modified}</span>",
|
||||
name = self.name,
|
||||
size = self.size,
|
||||
modified = self.modified
|
||||
)
|
||||
}
|
||||
}
|
||||
impl From<&File> for SearchResult {
|
||||
fn from(file: &File) -> Self {
|
||||
Self {
|
||||
name: file.filename.clone(),
|
||||
modified: file.last_modified.clone(),
|
||||
size: file.filesize,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,11 @@ fn search_to_html(results: Vec<SearchResult>) -> String {
|
||||
.map(|res| res.to_html())
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
let body = format!("<ul>{}</ul>", res_html);
|
||||
let body = format!(
|
||||
"<div>Yay, found the following {} lists!</div><div><ul>{}</ul></div>",
|
||||
results.len(),
|
||||
res_html
|
||||
);
|
||||
HTML_TEMPLATE.replace("{body}", &body)
|
||||
}
|
||||
fn redirect(req: &Request) -> Response {
|
||||
|
||||
Reference in New Issue
Block a user