moosicbox_profiles 0.4.0

MoosicBox profiles package
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Profile management for `MoosicBox`.
//!
//! This crate provides a global registry for managing profile names, allowing applications
//! to track and switch between different user profiles or configurations.
//!
//! # Features
//!
//! * `events` - Enables profile update event listeners for reacting to profile changes
//! * `api` - Provides actix-web extractors for HTTP requests with profile information
//!
//! # Example
//!
//! ```rust
//! use moosicbox_profiles::PROFILES;
//!
//! // Add a profile to the global registry
//! PROFILES.add("user1".to_string());
//!
//! // Retrieve a profile
//! if let Some(profile) = PROFILES.get("user1") {
//!     println!("Found profile: {}", profile);
//! }
//!
//! // List all profiles
//! let all_profiles = PROFILES.names();
//! ```

#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]

use std::{
    collections::BTreeSet,
    sync::{Arc, LazyLock, RwLock},
};

/// Profile update event handling.
///
/// Provides listeners and event triggers for profile additions and removals.
#[cfg(feature = "events")]
pub mod events;

/// Global instance of the profiles registry.
pub static PROFILES: LazyLock<Profiles> = LazyLock::new(Profiles::default);

/// Registry for managing profile names.
#[allow(clippy::module_name_repetitions)]
#[derive(Default)]
pub struct Profiles {
    profiles: Arc<RwLock<BTreeSet<String>>>,
}

impl Profiles {
    /// Adds a profile to the registry.
    ///
    /// # Panics
    ///
    /// Will panic if `RwLock` is poisoned
    pub fn add(&self, profile: String) {
        self.profiles.write().unwrap().insert(profile);
    }

    /// Removes a profile from the registry.
    ///
    /// # Panics
    ///
    /// Will panic if `RwLock` is poisoned
    pub fn remove(&self, profile: &str) {
        self.profiles.write().unwrap().retain(|p| p != profile);
    }

    /// Adds a profile to the registry and returns it.
    ///
    /// # Panics
    ///
    /// Will panic if `RwLock` is poisoned or the profile somehow wasn't added to the list of
    /// profiles
    #[must_use]
    pub fn add_fetch(&self, profile: &str) -> String {
        self.add(profile.to_owned());
        self.get(profile).unwrap()
    }

    /// Retrieves a profile from the registry.
    ///
    /// # Panics
    ///
    /// Will panic if `RwLock` is poisoned
    #[allow(clippy::must_use_candidate)]
    pub fn get(&self, profile: &str) -> Option<String> {
        self.profiles
            .read()
            .unwrap()
            .iter()
            .find_map(|p| if p == profile { Some(p.clone()) } else { None })
    }

    /// Returns all profile names in the registry.
    ///
    /// # Panics
    ///
    /// Will panic if `RwLock` is poisoned
    #[must_use]
    pub fn names(&self) -> Vec<String> {
        self.profiles.read().unwrap().iter().cloned().collect()
    }
}

#[cfg(feature = "api")]
pub mod api {
    //! actix-web integration for profile extraction from HTTP requests.
    //!
    //! Provides extractors for retrieving profile names from HTTP request headers
    //! (`moosicbox-profile`) or query parameters (`moosicboxProfile`).
    //!
    //! # Example
    //!
    //! ```rust,ignore
    //! use actix_web::{web, HttpResponse};
    //! use moosicbox_profiles::api::ProfileName;
    //!
    //! async fn handler(profile: ProfileName) -> HttpResponse {
    //!     HttpResponse::Ok().body(format!("Profile: {}", profile.as_ref()))
    //! }
    //! ```
    use actix_web::{FromRequest, HttpRequest, dev::Payload, error::ErrorBadRequest};
    use futures::future::{Ready, err, ok};
    use qstring::QString;

    use crate::PROFILES;

    /// Extracts profile name from query parameters.
    ///
    /// # Errors
    ///
    /// * Missing `moosicboxProfile` query parameter
    fn from_query(req: &HttpRequest) -> Result<String, actix_web::Error> {
        let query_string = req.query_string();
        let query: Vec<_> = QString::from(query_string).into();
        let profile = query
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case("moosicboxProfile"))
            .map(|(_, value)| value);

        let Some(profile) = profile else {
            return Err(ErrorBadRequest("Missing moosicboxProfile query param"));
        };

        Ok(profile.to_owned())
    }

    /// Extracts profile name from HTTP headers.
    ///
    /// # Errors
    ///
    /// * Missing `moosicbox-profile` header
    /// * Invalid UTF-8 in `moosicbox-profile` header value
    fn from_header(req: &HttpRequest) -> Result<&str, actix_web::Error> {
        let Some(profile_header_value) = req.headers().get("moosicbox-profile") else {
            return Err(ErrorBadRequest("Missing moosicbox-profile header"));
        };
        let Ok(profile) = profile_header_value.to_str() else {
            return Err(ErrorBadRequest("Invalid moosicbox-profile header"));
        };

        Ok(profile)
    }

    /// Profile name extracted from request without verification.
    ///
    /// This struct extracts a profile name from either HTTP request headers
    /// (`moosicbox-profile`) or query parameters (`moosicboxProfile`) but does not
    /// verify that the profile exists in the registry.
    ///
    /// Use [`ProfileName`] if you need to ensure the profile exists before proceeding.
    #[derive(Debug)]
    pub struct ProfileNameUnverified(
        /// The profile name string extracted from the request.
        pub String,
    );

    impl From<ProfileNameUnverified> for String {
        fn from(value: ProfileNameUnverified) -> Self {
            value.0
        }
    }

    impl ProfileNameUnverified {
        /// Extracts profile name from request headers or query parameters.
        ///
        /// # Errors
        ///
        /// * Missing both `moosicbox-profile` header and `moosicboxProfile` query parameter
        /// * Invalid UTF-8 in `moosicbox-profile` header value
        ///
        /// # Examples
        ///
        /// ```rust,ignore
        /// use actix_web::test::TestRequest;
        /// use moosicbox_profiles::api::ProfileNameUnverified;
        ///
        /// let req = TestRequest::default()
        ///     .uri("/?moosicboxProfile=default")
        ///     .to_http_request();
        /// let extracted = ProfileNameUnverified::from_request_inner(&req)?;
        /// assert_eq!(extracted.0, "default");
        /// # Ok::<(), actix_web::Error>(())
        /// ```
        pub fn from_request_inner(req: &HttpRequest) -> Result<Self, actix_web::Error> {
            from_query(req)
                .or_else(|_| from_header(req).map(std::borrow::ToOwned::to_owned))
                .map(Self)
        }
    }

    impl FromRequest for ProfileNameUnverified {
        type Error = actix_web::Error;
        type Future = Ready<Result<Self, actix_web::Error>>;

        fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
            match Self::from_request_inner(req) {
                Ok(x) => ok(x),
                Err(x) => err(x),
            }
        }
    }

    /// Verified profile name extracted from request.
    ///
    /// This struct extracts a profile name from either HTTP request headers
    /// (`moosicbox-profile`) or query parameters (`moosicboxProfile`) and verifies
    /// that the profile exists in the global [`PROFILES`] registry.
    ///
    /// If you don't need verification, use [`ProfileNameUnverified`] instead.
    pub struct ProfileName(
        /// The verified profile name string that exists in the registry.
        pub String,
    );

    impl From<ProfileName> for String {
        fn from(value: ProfileName) -> Self {
            value.0
        }
    }

    impl AsRef<str> for ProfileName {
        fn as_ref(&self) -> &str {
            &self.0
        }
    }

    impl ProfileName {
        /// Extracts and verifies profile name from request.
        ///
        /// # Errors
        ///
        /// * Missing both `moosicbox-profile` header and `moosicboxProfile` query parameter
        /// * Invalid UTF-8 in `moosicbox-profile` header value
        /// * Profile name not found in the global registry
        ///
        /// # Examples
        ///
        /// ```rust,ignore
        /// use actix_web::test::TestRequest;
        /// use moosicbox_profiles::{PROFILES, api::ProfileName};
        ///
        /// PROFILES.add("default".to_string());
        /// let req = TestRequest::default()
        ///     .uri("/?moosicboxProfile=default")
        ///     .to_http_request();
        /// let extracted = ProfileName::from_request_inner(&req)?;
        /// assert_eq!(extracted.as_ref(), "default");
        /// PROFILES.remove("default");
        /// # Ok::<(), actix_web::Error>(())
        /// ```
        pub fn from_request_inner(req: &HttpRequest) -> Result<Self, actix_web::Error> {
            let profile = match ProfileNameUnverified::from_request_inner(req) {
                Ok(profile) => {
                    let profile = profile.0;
                    if !PROFILES.names().iter().any(|x| x == &profile) {
                        return Err(ErrorBadRequest(format!(
                            "Profile '{profile}' does not exist"
                        )));
                    }
                    profile
                }
                Err(e) => {
                    return Err(e);
                }
            };

            Ok(Self(profile))
        }
    }

    impl FromRequest for ProfileName {
        type Error = actix_web::Error;
        type Future = Ready<Result<Self, actix_web::Error>>;

        fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
            match Self::from_request_inner(req) {
                Ok(x) => ok(x),
                Err(x) => err(x),
            }
        }
    }
}

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

    #[test_log::test]
    fn test_add_and_get_profile() {
        let profiles = Profiles::default();
        profiles.add("test_profile".to_string());

        let result = profiles.get("test_profile");
        assert_eq!(result, Some("test_profile".to_string()));
    }

    #[test_log::test]
    fn test_get_nonexistent_profile() {
        let profiles = Profiles::default();
        let result = profiles.get("nonexistent");
        assert_eq!(result, None);
    }

    #[test_log::test]
    fn test_remove_profile() {
        let profiles = Profiles::default();
        profiles.add("to_remove".to_string());

        assert!(profiles.get("to_remove").is_some());

        profiles.remove("to_remove");

        assert!(profiles.get("to_remove").is_none());
    }

    #[test_log::test]
    fn test_remove_nonexistent_profile() {
        let profiles = Profiles::default();
        // Should not panic when removing a profile that doesn't exist
        profiles.remove("nonexistent");
        assert!(profiles.get("nonexistent").is_none());
    }

    #[test_log::test]
    fn test_add_fetch() {
        let profiles = Profiles::default();
        let result = profiles.add_fetch("test_fetch");

        assert_eq!(result, "test_fetch");
        assert_eq!(profiles.get("test_fetch"), Some("test_fetch".to_string()));
    }

    #[test_log::test]
    fn test_names_returns_all_profiles() {
        let profiles = Profiles::default();
        profiles.add("profile1".to_string());
        profiles.add("profile2".to_string());
        profiles.add("profile3".to_string());

        let names = profiles.names();
        assert_eq!(names.len(), 3);
        assert!(names.contains(&"profile1".to_string()));
        assert!(names.contains(&"profile2".to_string()));
        assert!(names.contains(&"profile3".to_string()));
    }

    #[test_log::test]
    fn test_names_empty_when_no_profiles() {
        let profiles = Profiles::default();
        let names = profiles.names();
        assert!(names.is_empty());
    }

    #[test_log::test]
    fn test_profile_names_are_case_sensitive() {
        let profiles = Profiles::default();
        profiles.add("TestProfile".to_string());

        assert!(profiles.get("TestProfile").is_some());
        assert!(profiles.get("testprofile").is_none());
        assert!(profiles.get("TESTPROFILE").is_none());
    }

    #[test_log::test]
    fn test_duplicate_profile_add() {
        let profiles = Profiles::default();
        profiles.add("duplicate".to_string());
        profiles.add("duplicate".to_string());

        let names = profiles.names();
        // BTreeSet should deduplicate
        assert_eq!(names.len(), 1);
        assert_eq!(names[0], "duplicate");
    }

    #[test_log::test]
    fn test_concurrent_reads() {
        {
            use std::sync::Arc;
            use std::thread;

            let profiles = Arc::new(Profiles::default());
            profiles.add("concurrent_test".to_string());

            let mut handles = vec![];
            for _ in 0..10 {
                let profiles_clone = Arc::clone(&profiles);
                let handle = thread::spawn(move || {
                    for _ in 0..100 {
                        let result = profiles_clone.get("concurrent_test");
                        assert_eq!(result, Some("concurrent_test".to_string()));
                    }
                });
                handles.push(handle);
            }

            for handle in handles {
                handle.join().unwrap();
            }
        }
    }

    #[test_log::test]
    fn test_concurrent_adds() {
        {
            use std::sync::Arc;
            use std::thread;

            let profiles = Arc::new(Profiles::default());
            let mut handles = vec![];

            for i in 0..10 {
                let profiles_clone = Arc::clone(&profiles);
                let handle = thread::spawn(move || {
                    for j in 0..10 {
                        profiles_clone.add(format!("profile_{i}_{j}"));
                    }
                });
                handles.push(handle);
            }

            for handle in handles {
                handle.join().unwrap();
            }

            // Should have 100 unique profiles
            let names = profiles.names();
            assert_eq!(names.len(), 100);
        }
    }

    #[test_log::test]
    fn test_concurrent_mixed_operations() {
        {
            use std::sync::Arc;
            use std::thread;

            let profiles = Arc::new(Profiles::default());
            // Pre-populate some profiles
            for i in 0..10 {
                profiles.add(format!("initial_{i}"));
            }

            let mut handles = vec![];

            // Readers
            for _ in 0..5 {
                let profiles_clone = Arc::clone(&profiles);
                let handle = thread::spawn(move || {
                    for i in 0..10 {
                        let _ = profiles_clone.get(&format!("initial_{i}"));
                    }
                });
                handles.push(handle);
            }

            // Writers
            for i in 0..5 {
                let profiles_clone = Arc::clone(&profiles);
                let handle = thread::spawn(move || {
                    for j in 0..10 {
                        profiles_clone.add(format!("new_{i}_{j}"));
                    }
                });
                handles.push(handle);
            }

            for handle in handles {
                handle.join().unwrap();
            }

            let names = profiles.names();
            // Should have 10 initial + 50 new = 60 profiles
            assert_eq!(names.len(), 60);
        }
    }

    #[cfg(feature = "api")]
    mod api_tests {
        use super::*;
        use actix_web::test::TestRequest;

        #[test_log::test]
        fn test_profile_name_unverified_from_query() {
            let req = TestRequest::default()
                .uri("/?moosicboxProfile=unverified_profile")
                .to_http_request();

            let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
            assert!(result.is_ok());
            assert_eq!(result.unwrap().0, "unverified_profile");
        }

        #[test_log::test]
        fn test_profile_name_unverified_from_query_case_insensitive() {
            let test_cases = vec![
                "/?moosicboxProfile=test",
                "/?MoosicboxProfile=test",
                "/?MOOSICBOXPROFILE=test",
                "/?moosicboxprofile=test",
            ];

            for uri in test_cases {
                let req = TestRequest::default().uri(uri).to_http_request();
                let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
                assert!(result.is_ok(), "Failed for URI: {uri}");
                assert_eq!(result.unwrap().0, "test");
            }
        }

        #[test_log::test]
        fn test_profile_name_unverified_missing_both() {
            let req = TestRequest::default()
                .uri("/?other=value")
                .to_http_request();
            let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
            assert!(result.is_err());
        }

        #[test_log::test]
        fn test_profile_name_unverified_from_header() {
            let req = TestRequest::default()
                .insert_header(("moosicbox-profile", "header_profile"))
                .to_http_request();

            let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
            assert!(result.is_ok());
            assert_eq!(result.unwrap().0, "header_profile");
        }

        #[test_log::test]
        fn test_profile_name_unverified_prefers_query_over_header() {
            let req = TestRequest::default()
                .uri("/?moosicboxProfile=query_profile")
                .insert_header(("moosicbox-profile", "header_profile"))
                .to_http_request();

            let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
            assert!(result.is_ok());
            assert_eq!(result.unwrap().0, "query_profile");
        }

        #[test_log::test]
        fn test_profile_name_unverified_fallback_to_header() {
            let req = TestRequest::default()
                .insert_header(("moosicbox-profile", "header_profile"))
                .to_http_request();

            let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
            assert!(result.is_ok());
            assert_eq!(result.unwrap().0, "header_profile");
        }

        #[test_log::test]
        fn test_profile_name_verified_with_existing_profile() {
            PROFILES.add("verified_test".to_string());

            let req = TestRequest::default()
                .uri("/?moosicboxProfile=verified_test")
                .to_http_request();

            let result = super::super::api::ProfileName::from_request_inner(&req);
            assert!(result.is_ok());
            assert_eq!(result.unwrap().0, "verified_test");

            // Cleanup
            PROFILES.remove("verified_test");
        }

        #[test_log::test]
        fn test_profile_name_verified_with_nonexistent_profile() {
            let req = TestRequest::default()
                .uri("/?moosicboxProfile=nonexistent")
                .to_http_request();

            let result = super::super::api::ProfileName::from_request_inner(&req);
            assert!(result.is_err());
        }

        #[test_log::test]
        fn test_profile_name_as_ref() {
            let profile = super::super::api::ProfileName("test".to_string());
            let as_str: &str = profile.as_ref();
            assert_eq!(as_str, "test");
        }

        #[test_log::test]
        fn test_profile_name_into_string() {
            let profile = super::super::api::ProfileName("test".to_string());
            let string: String = profile.into();
            assert_eq!(string, "test");
        }

        #[test_log::test]
        fn test_profile_name_unverified_into_string() {
            let profile = super::super::api::ProfileNameUnverified("test".to_string());
            let string: String = profile.into();
            assert_eq!(string, "test");
        }

        #[test_log::test]
        fn test_profile_name_unverified_invalid_utf8_header() {
            {
                use actix_web::http::header::HeaderValue;

                // Create a header value with non-UTF8 bytes
                let non_utf8_value = HeaderValue::from_bytes(&[0x80, 0x81, 0x82]).unwrap();

                let req = TestRequest::default()
                    .insert_header(("moosicbox-profile", non_utf8_value))
                    .to_http_request();

                let result = super::super::api::ProfileNameUnverified::from_request_inner(&req);
                assert!(result.is_err());
            }
        }
    }
}