|
| 1 | +#[macro_use] extern crate rocket; |
| 2 | + |
| 3 | +use rocket::fs::{FileServer, relative}; |
| 4 | +use rocket::State; |
| 5 | +use rocket_dyn_templates::{Template}; |
| 6 | +use serde::Serialize; |
| 7 | + |
| 8 | +use fennec::prelude::*; |
| 9 | + |
| 10 | +#[derive(Clone, Serialize)] |
| 11 | +struct AppContext<'a> { |
| 12 | + title: &'a str, |
| 13 | + appname: &'a str, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Clone, Serialize)] |
| 17 | +struct DefinitionsContext<'a> { |
| 18 | + app: AppContext<'a>, |
| 19 | + entries: Vec<DictionaryEntry>, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Clone, Serialize)] |
| 23 | +struct SnippetsContext<'a> { |
| 24 | + app: AppContext<'a>, |
| 25 | + snippets: Vec<SnippetRow>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Serialize)] |
| 29 | +struct RootContext<'a> { |
| 30 | + app: AppContext<'a>, |
| 31 | + dictionary: Dictionary, |
| 32 | + notebook: Notebook, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Clone, Serialize)] |
| 36 | +struct SnippetRow { |
| 37 | + source: String, |
| 38 | + description: String, |
| 39 | + transcribed: bool, |
| 40 | + notes: Vec<Note>, |
| 41 | + words: Vec<WordRow>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Clone, Serialize)] |
| 45 | +struct WordRow { |
| 46 | + word_type: String, |
| 47 | + is_tunic: bool, |
| 48 | + is_english: bool, |
| 49 | + has_definition: bool, |
| 50 | + text: String, |
| 51 | + glyphs: Vec<Glyph>, |
| 52 | + has_border: bool, |
| 53 | + colored: bool, |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Clone, Serialize)] |
| 57 | +struct DictionaryEntry { |
| 58 | + glyphs: Vec<Glyph>, |
| 59 | + definition: String, |
| 60 | + notes: Vec<String>, |
| 61 | +} |
| 62 | + |
| 63 | +#[get("/")] |
| 64 | +fn index(state: &State<RootContext>) -> Template { |
| 65 | + Template::render("index", state.app.clone()) |
| 66 | +} |
| 67 | + |
| 68 | +#[get("/snippets")] |
| 69 | +fn snippets(state: &State<RootContext>) -> Template { |
| 70 | + let snippets: Vec<SnippetRow> = state |
| 71 | + .notebook |
| 72 | + .snippets |
| 73 | + .iter() |
| 74 | + .map(|snip| { |
| 75 | + let source = match &snip.source { |
| 76 | + Some(Source::ManualPageNumber(page_number)) => format!("/media/manual_pages/page{:0>2}.jpg", page_number), |
| 77 | + Some(Source::ScreenshotFilename(file)) => format!("/media/screenshots/{file}"), |
| 78 | + Some(Source::Other(_)) => "/media/404".to_owned(), |
| 79 | + None => "/media/404".to_owned(), |
| 80 | + }; |
| 81 | + |
| 82 | + let description = snip.description.clone(); |
| 83 | + |
| 84 | + let transcribed = snip.transcribed; |
| 85 | + |
| 86 | + let notes = snip.notes.clone(); |
| 87 | + |
| 88 | + let words: Vec<WordRow> = snip |
| 89 | + .words |
| 90 | + .iter() |
| 91 | + .map(|word| { |
| 92 | + match &word.word_type { |
| 93 | + WordType::English(english_word) => WordRow { |
| 94 | + word_type: "English".to_owned(), |
| 95 | + is_tunic: false, |
| 96 | + is_english: true, |
| 97 | + has_definition: true, |
| 98 | + text: english_word.text(), |
| 99 | + glyphs: vec![], |
| 100 | + has_border: false, |
| 101 | + colored: false, |
| 102 | + }, |
| 103 | + WordType::Tunic(tunic_word) => { |
| 104 | + let (has_definition, definition) = state |
| 105 | + .dictionary |
| 106 | + .get(&tunic_word.into()) |
| 107 | + .map(|entry| { |
| 108 | + let definition = match entry.definition() { |
| 109 | + Definition::Undefined => "[Undefined]".to_owned(), |
| 110 | + Definition::Tentative(text) => text.clone(), |
| 111 | + Definition::Confirmed(text) => text.clone(), |
| 112 | + }; |
| 113 | + |
| 114 | + (true, definition) |
| 115 | + }) |
| 116 | + .unwrap_or((false, "".to_owned())); |
| 117 | + |
| 118 | + WordRow { |
| 119 | + word_type: "Tunic".to_owned(), |
| 120 | + is_tunic: true, |
| 121 | + is_english: false, |
| 122 | + has_definition, |
| 123 | + text: definition, |
| 124 | + glyphs: tunic_word.glyphs(), |
| 125 | + has_border: tunic_word.has_border(), |
| 126 | + colored: tunic_word.colored(), |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + }) |
| 131 | + .collect(); |
| 132 | + |
| 133 | + SnippetRow { |
| 134 | + source, |
| 135 | + description, |
| 136 | + transcribed, |
| 137 | + notes, |
| 138 | + words, |
| 139 | + } |
| 140 | + }) |
| 141 | + .collect(); |
| 142 | + |
| 143 | + let context = SnippetsContext { |
| 144 | + app: state.app.clone(), |
| 145 | + snippets, |
| 146 | + }; |
| 147 | + |
| 148 | + Template::render("snippets", context) |
| 149 | +} |
| 150 | + |
| 151 | +#[get("/definitions")] |
| 152 | +fn definitions(state: &State<RootContext>) -> Template { |
| 153 | + let context = DefinitionsContext { |
| 154 | + app: state.app.clone(), |
| 155 | + entries: to_dictionary_entries(&state.dictionary), |
| 156 | + }; |
| 157 | + |
| 158 | + Template::render("definitions", context) |
| 159 | +} |
| 160 | + |
| 161 | +fn to_dictionary_entries(dictionary: &Dictionary) -> Vec<DictionaryEntry> { |
| 162 | + dictionary |
| 163 | + .entries() |
| 164 | + .iter() |
| 165 | + .map(|(word, entry)| { |
| 166 | + let glyphs = word.glyphs(); |
| 167 | + let definition = match entry.definition() { |
| 168 | + Definition::Undefined => "[Undefined]".to_owned(), |
| 169 | + Definition::Tentative(text) => text.clone(), |
| 170 | + Definition::Confirmed(text) => text.clone(), |
| 171 | + }; |
| 172 | + let notes: Vec<String> = entry |
| 173 | + .notes() |
| 174 | + .iter() |
| 175 | + .map(|n| n.into()) |
| 176 | + .collect(); |
| 177 | + |
| 178 | + DictionaryEntry { |
| 179 | + glyphs, |
| 180 | + definition, |
| 181 | + notes, |
| 182 | + } |
| 183 | + }) |
| 184 | + .collect() |
| 185 | +} |
| 186 | + |
| 187 | +#[launch] |
| 188 | +fn rocket() -> _ { |
| 189 | + let (notebook, _yaml) = notebook_from_yaml_file(DEFAULT_NOTEBOOK_FILE) |
| 190 | + .unwrap_or_else(|error| { |
| 191 | + println!("Unable to load notebook file: {}", DEFAULT_NOTEBOOK_FILE); |
| 192 | + println!("{:?}", error); |
| 193 | + panic!("Search aborted"); |
| 194 | + }); |
| 195 | + |
| 196 | + let (dictionary, _yaml) = dictionary_from_yaml_file(DEFAULT_DICTIONARY_FILE) |
| 197 | + .unwrap_or_else(|error| { |
| 198 | + println!("Unable to load dictionary file: {}", DEFAULT_NOTEBOOK_FILE); |
| 199 | + println!("{:?}", error); |
| 200 | + panic!("Search aborted"); |
| 201 | + }); |
| 202 | + |
| 203 | + let root_context = RootContext { |
| 204 | + app: AppContext { |
| 205 | + title: "Fennec", |
| 206 | + appname: "Fennec", |
| 207 | + }, |
| 208 | + notebook, |
| 209 | + dictionary, |
| 210 | + }; |
| 211 | + |
| 212 | + rocket::build() |
| 213 | + .mount("/", routes![index, definitions, snippets]) |
| 214 | + .mount("/media", FileServer::from(relative!("sources"))) |
| 215 | + .manage(root_context) |
| 216 | + .attach(Template::fairing()) |
| 217 | +} |
0 commit comments