summaryrefslogtreecommitdiffstats
path: root/front_end/src/not_found_page.rs
diff options
context:
space:
mode:
authorKornel <kornel@geekhood.net>2019-04-14 16:20:08 +0100
committerKornel <kornel@geekhood.net>2019-04-14 16:26:30 +0100
commitd692439dcc2d17272ec3f83564818efa8e23a055 (patch)
tree54f13f58dbc4d3477818e1cfba518db21de8824e /front_end/src/not_found_page.rs
parent35e266efa5c021e245a4fa2639334f24227f94a4 (diff)
404 page
Diffstat (limited to 'front_end/src/not_found_page.rs')
-rw-r--r--front_end/src/not_found_page.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/front_end/src/not_found_page.rs b/front_end/src/not_found_page.rs
new file mode 100644
index 0000000..27f8c79
--- /dev/null
+++ b/front_end/src/not_found_page.rs
@@ -0,0 +1,83 @@
+use crate::templates;
+use crate::Page;
+use crate::Urler;
+use render_readme::Renderer;
+use std::io::Write;
+
+
+pub struct NotFoundPage<'a> {
+ markup: &'a Renderer,
+ pub results: &'a [search_index::CrateFound],
+ pub query: &'a str,
+}
+
+impl NotFoundPage<'_> {
+ pub fn new<'a>(query: &'a str, results: &'a [search_index::CrateFound], markup: &'a Renderer) -> NotFoundPage<'a> {
+ NotFoundPage {
+ query,
+ markup,
+ results,
+ }
+ }
+
+ pub fn page(&self) -> Page {
+
+ Page {
+ title: "Crate not found".into(),
+ description: Some("Error".into()),
+ item_name: None,
+ item_description: None,
+ keywords: None,
+ created: None,
+ alternate: None,
+ alternate_type: None,
+ canonical: None,
+ noindex: true,
+ search_meta: true,
+ critical_css_data: Some(include_str!("../../style/public/search.css")),
+ }
+ }
+
+ /// For color of the version
+ ///
+ /// It tries to guess which versions seem "unstable".
+ ///
+ /// TODO: Merge with the better version history analysis from the individual crate page.
+ pub fn version_class(&self, ver: &str) -> &str {
+ let v = semver::Version::parse(ver).expect("semver");
+ match (v.major, v.minor, v.patch, v.is_prerelease()) {
+ (1..=15, _, _, false) => "stable",
+ (0, m, p, false) if m >= 2 && p >= 3 => "stable",
+ (m, ..) if m >= 1 => "okay",
+ (0, 1, p, _) if p >= 10 => "okay",
+ (0, 3..=10, p, _) if p > 0 => "okay",
+ _ => "unstable",
+ }
+ }
+
+ /// Nicely rounded number of downloads
+ ///
+ /// To show that these numbers are just approximate.
+ pub fn downloads(&self, num: u64) -> (String, &str) {
+ match num {
+ a @ 0..=99 => (format!("{}", a), ""),
+ a @ 0..=500 => (format!("{}", a / 10 * 10), ""),
+ a @ 0..=999 => (format!("{}", a / 50 * 50), ""),
+ a @ 0..=9999 => (format!("{}.{}", a / 1000, a % 1000 / 100), "K"),
+ a @ 0..=999_999 => (format!("{}", a / 1000), "K"),
+ a => (format!("{}.{}", a / 1_000_000, a % 1_000_000 / 100_000), "M"),
+ }
+ }
+
+ /// Used to render descriptions
+ pub fn render_markdown_str(&self, s: &str) -> templates::Html<String> {
+ templates::Html(self.markup.markdown_str(s, false))
+ }
+}
+
+pub fn render_404_page(out: &mut dyn Write, query: &str, results: &[search_index::CrateFound], markup: &Renderer) -> Result<(), failure::Error> {
+ let urler = Urler::new();
+ let page = NotFoundPage::new(query, results, markup);
+ templates::not_found(out, &page, &urler)?;
+ Ok(())
+}