Skip to main content

allowthem_server/
shell_context.rs

1use serde::Serialize;
2
3use crate::nav::{NavItem, nav_items_for};
4
5#[derive(Debug, Clone, Serialize)]
6pub struct ShellContext {
7    pub is_admin: bool,
8    pub current_path: String,
9    pub application_name: String,
10    pub nav_items: Vec<NavItem>,
11    /// Displayed in the modeline. Defaults to "ANON" when not set.
12    /// Authenticated pages should set this to the user's email.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub status_session: Option<String>,
15}
16
17impl ShellContext {
18    pub fn new(is_admin: bool, current_path: &str, application_name: &str) -> Self {
19        let nav_items = nav_items_for(is_admin, current_path);
20        Self {
21            is_admin,
22            current_path: current_path.to_string(),
23            application_name: application_name.to_string(),
24            nav_items,
25            status_session: None,
26        }
27    }
28
29    /// Set the session status displayed in the modeline (typically the user's
30    /// email address on authenticated pages).
31    pub fn with_session(mut self, email: &str) -> Self {
32        self.status_session = Some(email.to_string());
33        self
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn builds_admin_shell_with_five_items() {
43        let s = ShellContext::new(true, "/admin/sessions", "allowthem");
44        assert!(s.is_admin);
45        assert_eq!(s.application_name, "allowthem");
46        assert_eq!(s.nav_items.len(), 5);
47    }
48
49    #[test]
50    fn builds_user_shell_with_two_items() {
51        let s = ShellContext::new(false, "/settings", "allowthem");
52        assert!(!s.is_admin);
53        assert_eq!(s.nav_items.len(), 2);
54    }
55
56    #[test]
57    fn with_session_sets_status() {
58        let s = ShellContext::new(false, "/settings", "allowthem").with_session("user@example.com");
59        assert_eq!(s.status_session.as_deref(), Some("user@example.com"));
60    }
61}