Skip to main content

serenade_session/
session.rs

1//! In-request session bag (Symfony `SessionInterface` / attribute bag analogue).
2
3use std::collections::HashMap;
4
5/// Mutable key/value session attributes for one request lifecycle.
6#[derive(Clone, Debug, Default, Eq, PartialEq)]
7pub struct Session {
8    id: String,
9    attributes: HashMap<String, String>,
10    is_new: bool,
11    invalidated: bool,
12    dirty: bool,
13}
14
15impl Session {
16    /// Creates a new empty session with `id`.
17    #[must_use]
18    pub fn new(id: impl Into<String>) -> Self {
19        Self {
20            id: id.into(),
21            attributes: HashMap::new(),
22            is_new: true,
23            invalidated: false,
24            dirty: false,
25        }
26    }
27
28    /// Creates a session loaded from a store.
29    #[must_use]
30    pub fn existing(id: impl Into<String>, attributes: HashMap<String, String>) -> Self {
31        Self {
32            id: id.into(),
33            attributes,
34            is_new: false,
35            invalidated: false,
36            dirty: false,
37        }
38    }
39
40    /// Session identifier (opaque; placed in the session cookie).
41    #[must_use]
42    pub fn id(&self) -> &str {
43        &self.id
44    }
45
46    /// Whether this id was minted for the current request.
47    #[must_use]
48    pub const fn is_new(&self) -> bool {
49        self.is_new
50    }
51
52    /// Whether [`Self::invalidate`] was called.
53    #[must_use]
54    pub const fn is_invalidated(&self) -> bool {
55        self.invalidated
56    }
57
58    /// Whether attributes changed since load (or the session is new).
59    #[must_use]
60    pub const fn is_dirty(&self) -> bool {
61        self.dirty || self.is_new
62    }
63
64    /// Borrowed attribute value.
65    #[must_use]
66    pub fn get(&self, key: &str) -> Option<&str> {
67        self.attributes.get(key).map(String::as_str)
68    }
69
70    /// Sets an attribute.
71    pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) {
72        self.attributes.insert(key.into(), value.into());
73        self.dirty = true;
74    }
75
76    /// Removes an attribute.
77    pub fn remove(&mut self, key: &str) -> Option<String> {
78        let previous = self.attributes.remove(key);
79        if previous.is_some() {
80            self.dirty = true;
81        }
82        previous
83    }
84
85    /// Clears all attributes (keeps the same id unless invalidated).
86    pub fn clear(&mut self) {
87        if !self.attributes.is_empty() {
88            self.attributes.clear();
89            self.dirty = true;
90        }
91    }
92
93    /// Marks the session destroyed: attributes cleared, store delete on commit.
94    pub fn invalidate(&mut self) {
95        self.attributes.clear();
96        self.invalidated = true;
97        self.dirty = true;
98    }
99
100    /// Attribute map for persistence.
101    #[must_use]
102    pub const fn attributes(&self) -> &HashMap<String, String> {
103        &self.attributes
104    }
105
106    pub(crate) fn take_for_save(&self) -> HashMap<String, String> {
107        self.attributes.clone()
108    }
109}