Skip to content

Commit

Permalink
add global alert to notify users about upcoming breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroalbini committed Sep 16, 2019
1 parent ff27e25 commit c07f340
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ pub mod utils;
mod docbuilder;
mod web;

use web::page::GlobalAlert;


// Warning message shown in the navigation bar of every page. Set to `None` to hide it.
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = Some(GlobalAlert {
url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html",
text: "Upcoming docs.rs breaking changes!",
});


/// Version string generated at build time contains last git
/// commit hash and build date
Expand Down
20 changes: 20 additions & 0 deletions src/web/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ use iron::response::Response;
use handlebars_iron::Template;


pub(crate) struct GlobalAlert {
pub(crate) url: &'static str,
pub(crate) text: &'static str,
}

impl ToJson for GlobalAlert {
fn to_json(&self) -> Json {
let mut map = BTreeMap::new();
map.insert("url".to_string(), self.url.to_json());
map.insert("text".to_string(), self.text.to_json());
Json::Object(map)
}
}


pub struct Page<T: ToJson> {
title: Option<String>,
content: T,
Expand Down Expand Up @@ -89,6 +104,11 @@ impl<T: ToJson> ToJson for Page<T> {
tree.insert("title".to_owned(), title.to_json());
}

tree.insert("has_global_alert".to_owned(), ::GLOBAL_ALERT.is_some().to_json());
if let Some(ref global_alert) = ::GLOBAL_ALERT {
tree.insert("global_alert".to_owned(), global_alert.to_json());
}

tree.insert("content".to_owned(), self.content.to_json());
tree.insert("cratesfyi_version".to_owned(), ::BUILD_VERSION.to_json());
tree.insert("cratesfyi_version_safe".to_owned(),
Expand Down
1 change: 1 addition & 0 deletions templates/navigation.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<li class="pure-menu-item"><a href="/about" class="pure-menu-link">About Docs.rs</a></li>
</ul>
</li>
{{> navigation_global_alert}}
</ul>
</form>
</div>
Expand Down
10 changes: 10 additions & 0 deletions templates/navigation_global_alert.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#if ../has_global_alert}}
<li class="pure-menu-item">
<a href="{{../global_alert.url}}" class="pure-menu-link warn">
<i class="fa fa-fw fa-warning"></i>
{{../global_alert.text}}
<i class="fa fa-fw fa-warning"></i>
</a>
</li>
{{/if}}

1 change: 1 addition & 0 deletions templates/navigation_rustdoc.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
</ul>
</li>
{{/with}}
{{> navigation_global_alert}}
</ul>
</form>
</div>
Expand Down

0 comments on commit c07f340

Please sign in to comment.