acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Session types and data structures
//!
//! This module provides the core session types used throughout the framework.

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Unique session identifier
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SessionId(String);

impl SessionId {
    /// Generate a new cryptographically secure session ID
    #[must_use]
    pub fn generate() -> Self {
        Self(Uuid::new_v4().to_string())
    }

    /// Create from a string (validates format)
    ///
    /// # Errors
    ///
    /// Returns error if the string is not a valid UUID
    pub fn try_from_string(s: String) -> Result<Self, SessionError> {
        Uuid::parse_str(&s)
            .map(|_| Self(s))
            .map_err(|_| SessionError::InvalidSessionId)
    }

    /// Get the session ID as a string reference
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for SessionId {
    type Err = SessionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from_string(s.to_string())
    }
}

/// Session data stored per-session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionData {
    /// When this session was created
    pub created_at: DateTime<Utc>,
    /// When this session was last accessed
    pub last_accessed: DateTime<Utc>,
    /// When this session expires
    pub expires_at: DateTime<Utc>,
    /// User ID (if authenticated)
    pub user_id: Option<i64>,
    /// Custom session data (key-value store)
    pub data: HashMap<String, serde_json::Value>,
    /// Flash messages queued for next request
    pub flash_messages: Vec<FlashMessage>,
}

impl SessionData {
    /// Create new session data with default expiration (24 hours)
    #[must_use]
    pub fn new() -> Self {
        let now = Utc::now();
        Self {
            created_at: now,
            last_accessed: now,
            expires_at: now + Duration::hours(24),
            user_id: None,
            data: HashMap::new(),
            flash_messages: Vec::new(),
        }
    }

    /// Create session with custom expiration duration
    #[must_use]
    pub fn with_expiration(duration: Duration) -> Self {
        let now = Utc::now();
        Self {
            created_at: now,
            last_accessed: now,
            expires_at: now + duration,
            user_id: None,
            data: HashMap::new(),
            flash_messages: Vec::new(),
        }
    }

    /// Check if session is expired
    #[must_use]
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }

    /// Update last accessed time and extend expiration
    pub fn touch(&mut self, extend_by: Duration) {
        self.last_accessed = Utc::now();
        self.expires_at = self.last_accessed + extend_by;
    }

    /// Validate session is not expired and touch it if valid
    ///
    /// This method combines the common pattern of checking expiry and
    /// extending the session lifetime in a single operation.
    ///
    /// # Returns
    ///
    /// - `true` if the session is valid (not expired) - session is touched
    /// - `false` if the session has expired - session is not modified
    ///
    /// # Example
    ///
    /// ```
    /// use acton_htmx::auth::session::SessionData;
    /// use chrono::Duration;
    ///
    /// let mut session = SessionData::new();
    /// assert!(session.validate_and_touch(Duration::hours(24)));
    ///
    /// // Expired session returns false
    /// let mut expired = SessionData::with_expiration(Duration::seconds(-1));
    /// assert!(!expired.validate_and_touch(Duration::hours(24)));
    /// ```
    pub fn validate_and_touch(&mut self, extend_by: Duration) -> bool {
        if self.is_expired() {
            false
        } else {
            self.touch(extend_by);
            true
        }
    }

    /// Get a value from session data
    #[must_use]
    pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T> {
        self.data
            .get(key)
            .and_then(|v| serde_json::from_value(v.clone()).ok())
    }

    /// Set a value in session data
    ///
    /// # Errors
    ///
    /// Returns error if value cannot be serialized to JSON
    pub fn set<T: Serialize>(&mut self, key: String, value: T) -> Result<(), SessionError> {
        let json_value = serde_json::to_value(value)?;
        self.data.insert(key, json_value);
        Ok(())
    }

    /// Remove a value from session data
    pub fn remove(&mut self, key: &str) -> Option<serde_json::Value> {
        self.data.remove(key)
    }

    /// Clear all session data (keeps metadata)
    pub fn clear(&mut self) {
        self.data.clear();
        self.flash_messages.clear();
        self.user_id = None;
    }
}

impl Default for SessionData {
    fn default() -> Self {
        Self::new()
    }
}

/// Flash message for one-time display
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FlashMessage {
    /// Message level (success, info, warning, error)
    pub level: FlashLevel,
    /// Message text
    pub message: String,
    /// Optional title
    pub title: Option<String>,
}

impl FlashMessage {
    /// Create a success flash message
    #[must_use]
    pub fn success(message: impl Into<String>) -> Self {
        Self {
            level: FlashLevel::Success,
            message: message.into(),
            title: None,
        }
    }

    /// Create an info flash message
    #[must_use]
    pub fn info(message: impl Into<String>) -> Self {
        Self {
            level: FlashLevel::Info,
            message: message.into(),
            title: None,
        }
    }

    /// Create a warning flash message
    #[must_use]
    pub fn warning(message: impl Into<String>) -> Self {
        Self {
            level: FlashLevel::Warning,
            message: message.into(),
            title: None,
        }
    }

    /// Create an error flash message
    #[must_use]
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            level: FlashLevel::Error,
            message: message.into(),
            title: None,
        }
    }

    /// Set the title for this flash message
    #[must_use]
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Get CSS class for this flash level
    #[must_use]
    pub const fn css_class(&self) -> &'static str {
        self.level.css_class()
    }
}

/// Flash message severity level
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum FlashLevel {
    /// Success message (green)
    Success,
    /// Informational message (blue)
    Info,
    /// Warning message (yellow)
    Warning,
    /// Error message (red)
    Error,
}

impl FlashLevel {
    /// Get CSS class for this level
    #[must_use]
    pub const fn css_class(self) -> &'static str {
        match self {
            Self::Success => "flash-success",
            Self::Info => "flash-info",
            Self::Warning => "flash-warning",
            Self::Error => "flash-error",
        }
    }
}

/// Session-related errors
#[derive(Debug, thiserror::Error)]
pub enum SessionError {
    /// Invalid session ID format
    #[error("Invalid session ID")]
    InvalidSessionId,

    /// Session not found
    #[error("Session not found")]
    NotFound,

    /// Session expired
    #[error("Session expired")]
    Expired,

    /// Serialization error
    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),

    /// Redis error
    #[cfg(feature = "redis")]
    #[error("Redis error: {0}")]
    Redis(String),

    /// Agent communication error
    #[error("Agent error: {0}")]
    Agent(String),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_session_id_generate() {
        let id1 = SessionId::generate();
        let id2 = SessionId::generate();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_session_id_from_string() {
        let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
        let result = SessionId::try_from_string(uuid_str.to_string());
        assert!(result.is_ok());
    }

    #[test]
    fn test_session_id_invalid() {
        let result = SessionId::try_from_string("not-a-uuid".to_string());
        assert!(result.is_err());
    }

    #[test]
    fn test_session_data_new() {
        let data = SessionData::new();
        assert!(!data.is_expired());
        assert!(data.user_id.is_none());
        assert!(data.data.is_empty());
    }

    #[test]
    fn test_session_data_expiration() {
        let data = SessionData::with_expiration(Duration::seconds(-1));
        assert!(data.is_expired());
    }

    #[test]
    fn test_session_data_touch() {
        let mut data = SessionData::new();
        let original_expiry = data.expires_at;
        std::thread::sleep(std::time::Duration::from_millis(10));
        data.touch(Duration::hours(24));
        assert!(data.expires_at > original_expiry);
    }

    #[test]
    fn test_session_data_validate_and_touch_valid() {
        let mut data = SessionData::new();
        let original_expiry = data.expires_at;
        std::thread::sleep(std::time::Duration::from_millis(10));

        // Valid session should return true and extend expiry
        assert!(data.validate_and_touch(Duration::hours(24)));
        assert!(data.expires_at > original_expiry);
    }

    #[test]
    fn test_session_data_validate_and_touch_expired() {
        let mut data = SessionData::with_expiration(Duration::seconds(-1));
        let original_expiry = data.expires_at;

        // Expired session should return false and not modify expiry
        assert!(!data.validate_and_touch(Duration::hours(24)));
        assert_eq!(data.expires_at, original_expiry);
    }

    #[test]
    fn test_session_data_get_set() {
        let mut data = SessionData::new();
        data.set("key".to_string(), "value").unwrap();
        let value: Option<String> = data.get("key");
        assert_eq!(value, Some("value".to_string()));
    }

    #[test]
    fn test_session_data_remove() {
        let mut data = SessionData::new();
        data.set("key".to_string(), "value").unwrap();
        let removed = data.remove("key");
        assert!(removed.is_some());
        let value: Option<String> = data.get("key");
        assert!(value.is_none());
    }

    #[test]
    fn test_flash_message_creation() {
        let flash = FlashMessage::success("Test").with_title("Success");
        assert_eq!(flash.level, FlashLevel::Success);
        assert_eq!(flash.message, "Test");
        assert_eq!(flash.title, Some("Success".to_string()));
    }

    #[test]
    fn test_flash_level_css_class() {
        assert_eq!(FlashLevel::Success.css_class(), "flash-success");
        assert_eq!(FlashLevel::Info.css_class(), "flash-info");
        assert_eq!(FlashLevel::Warning.css_class(), "flash-warning");
        assert_eq!(FlashLevel::Error.css_class(), "flash-error");
    }
}