a3s-boot 0.1.3

Adapter-first modular Rust web framework for A3S inspired by Nest.js
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use super::header::{
    accepts_event_stream_response, accepts_json_response, get_header, is_json_media_type,
    matches_media_type, normalize_header_name, normalize_headers, parse_content_length,
    strict_content_length_values, validate_header_name, validate_header_value,
};
use super::method::HttpMethod;
use super::query::{parse_query, parse_query_pairs, split_path_query};
use crate::percent::validate_percent_encoding;
use crate::routing::host::normalize_host_header;
#[cfg(feature = "auth")]
use crate::AuthPrincipal;
use crate::{validate_value, BootError, ContextId, ModuleRef, ProviderToken, Result, Validate};
use serde::{de::DeserializeOwned, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
#[cfg(feature = "auth")]
use std::sync::RwLock;

/// Framework-neutral HTTP request passed to Boot route handlers.
#[derive(Debug, Clone)]
pub struct BootRequest {
    pub method: HttpMethod,
    pub path: String,
    pub query_string: Option<String>,
    pub query: BTreeMap<String, String>,
    pub params: BTreeMap<String, String>,
    pub host_params: BTreeMap<String, String>,
    pub headers: BTreeMap<String, String>,
    pub appended_headers: Vec<(String, String)>,
    pub body: Vec<u8>,
    module_ref: Option<ModuleRef>,
    #[cfg(feature = "auth")]
    auth_principal: Arc<RwLock<Option<AuthPrincipal>>>,
}

impl PartialEq for BootRequest {
    fn eq(&self, other: &Self) -> bool {
        self.method == other.method
            && self.path == other.path
            && self.query_string == other.query_string
            && self.query == other.query
            && self.params == other.params
            && self.host_params == other.host_params
            && self.headers == other.headers
            && self.appended_headers == other.appended_headers
            && self.body == other.body
    }
}

impl Eq for BootRequest {}

impl BootRequest {
    pub fn new(method: HttpMethod, path: impl Into<String>) -> Self {
        let (path, query_string, query) = split_path_query(path.into());
        Self {
            method,
            path,
            query_string,
            query,
            params: BTreeMap::new(),
            host_params: BTreeMap::new(),
            headers: BTreeMap::new(),
            appended_headers: Vec::new(),
            body: Vec::new(),
            module_ref: None,
            #[cfg(feature = "auth")]
            auth_principal: Arc::new(RwLock::new(None)),
        }
    }

    pub fn method(&self) -> HttpMethod {
        self.method
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn query_string(&self) -> Option<&str> {
        self.query_string.as_deref()
    }

    pub fn with_query_string(mut self, query_string: impl Into<String>) -> Self {
        let query_string = query_string.into();
        self.query = parse_query(&query_string);
        self.query_string = Some(query_string);
        self
    }

    pub(crate) fn with_matched_path(mut self, path: impl Into<String>) -> Self {
        self.path = path.into();
        self
    }

    pub(crate) fn with_module_ref(mut self, module_ref: ModuleRef) -> Self {
        self.module_ref = Some(module_ref);
        self
    }

    pub fn module_ref(&self) -> Option<&ModuleRef> {
        self.module_ref.as_ref()
    }

    /// Dependency-injection context attached while this request is dispatched.
    pub fn context_id(&self) -> Option<&ContextId> {
        self.module_ref.as_ref().and_then(ModuleRef::context_id)
    }

    pub fn get<T>(&self) -> Result<Arc<T>>
    where
        T: Send + Sync + 'static,
    {
        self.module_ref
            .as_ref()
            .ok_or_else(|| BootError::MissingProvider(ProviderToken::of::<T>().to_string()))?
            .get::<T>()
    }

    pub fn get_named<T>(&self, token: &str) -> Result<Arc<T>>
    where
        T: Send + Sync + 'static,
    {
        self.module_ref
            .as_ref()
            .ok_or_else(|| BootError::MissingProvider(ProviderToken::named(token).to_string()))?
            .get_named::<T>(token)
    }

    pub fn get_optional<T>(&self) -> Result<Option<Arc<T>>>
    where
        T: Send + Sync + 'static,
    {
        match &self.module_ref {
            Some(module_ref) => module_ref.get_optional::<T>(),
            None => Ok(None),
        }
    }

    pub fn get_optional_named<T>(&self, token: &str) -> Result<Option<Arc<T>>>
    where
        T: Send + Sync + 'static,
    {
        match &self.module_ref {
            Some(module_ref) => module_ref.get_optional_named::<T>(token),
            None => Ok(None),
        }
    }

    #[cfg(feature = "auth")]
    pub fn with_auth_principal(mut self, principal: AuthPrincipal) -> Self {
        self.auth_principal = Arc::new(RwLock::new(Some(principal)));
        self
    }

    #[cfg(feature = "auth")]
    pub fn set_auth_principal(&self, principal: AuthPrincipal) -> Result<()> {
        *self
            .auth_principal
            .write()
            .map_err(|_| BootError::Internal("auth principal lock is poisoned".to_string()))? =
            Some(principal);
        Ok(())
    }

    #[cfg(feature = "auth")]
    pub fn clear_auth_principal(&self) -> Result<()> {
        *self
            .auth_principal
            .write()
            .map_err(|_| BootError::Internal("auth principal lock is poisoned".to_string()))? =
            None;
        Ok(())
    }

    #[cfg(feature = "auth")]
    pub fn auth_principal(&self) -> Result<Option<AuthPrincipal>> {
        Ok(self
            .auth_principal
            .read()
            .map_err(|_| BootError::Internal("auth principal lock is poisoned".to_string()))?
            .clone())
    }

    #[cfg(feature = "auth")]
    pub fn require_auth_principal(&self) -> Result<AuthPrincipal> {
        self.auth_principal()?
            .ok_or_else(|| BootError::Unauthorized("missing authenticated principal".to_string()))
    }

    #[cfg(feature = "session")]
    pub fn session(&self) -> Result<crate::Session> {
        let manager = self.get::<crate::SessionManager>()?;
        let session_id = manager.require_session_id(self)?;
        crate::Session::from_manager_arc(manager, session_id)
    }

    #[cfg(feature = "session")]
    pub fn optional_session(&self) -> Result<Option<crate::Session>> {
        let Some(manager) = self.get_optional::<crate::SessionManager>()? else {
            return Ok(None);
        };
        let Some(session_id) = manager.session_id(self)? else {
            return Ok(None);
        };
        crate::Session::from_manager_arc(manager, session_id).map(Some)
    }

    pub fn with_path_params(mut self, params: BTreeMap<String, String>) -> Self {
        self.params = params;
        self
    }

    pub fn with_host_params(mut self, params: BTreeMap<String, String>) -> Self {
        self.host_params = params;
        self
    }

    pub fn with_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.params.insert(name.into(), value.into());
        self
    }

    pub fn with_host_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.host_params.insert(name.into(), value.into());
        self
    }

    pub fn with_query_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.query.insert(name.into(), value.into());
        self.query_string = None;
        self
    }

    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = body.into();
        self
    }

    pub fn body(&self) -> &[u8] {
        &self.body
    }

    pub fn into_body(self) -> Vec<u8> {
        self.body
    }

    pub fn with_text(self, body: impl Into<String>) -> Self {
        self.with_body(body.into())
            .with_header("content-type", "text/plain; charset=utf-8")
    }

    pub fn with_json<T>(self, body: &T) -> Result<Self>
    where
        T: Serialize,
    {
        let body = serde_json::to_vec(body).map_err(|err| BootError::Internal(err.to_string()))?;
        Ok(self
            .with_body(body)
            .with_header("content-type", "application/json"))
    }

    pub fn with_content_type(self, content_type: impl Into<String>) -> Self {
        self.with_header("content-type", content_type)
    }

    pub fn with_content_length(self, content_length: u64) -> Self {
        self.with_header("content-length", content_length.to_string())
    }

    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .insert(normalize_header_name(name), value.into());
        self
    }

    pub fn with_headers(mut self, headers: BTreeMap<String, String>) -> Self {
        self.headers = normalize_headers(headers);
        self
    }

    pub fn append_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.appended_headers
            .push((normalize_header_name(name), value.into()));
        self
    }

    pub fn header_entries(&self) -> impl Iterator<Item = (&str, &str)> {
        self.headers
            .iter()
            .map(|(name, value)| (name.as_str(), value.as_str()))
            .chain(
                self.appended_headers
                    .iter()
                    .map(|(name, value)| (name.as_str(), value.as_str())),
            )
    }

    pub fn validate_headers(&self) -> Result<()> {
        for (name, value) in self.header_entries() {
            validate_request_header(name, value)?;
        }

        Ok(())
    }

    pub fn text(&self) -> Result<String> {
        String::from_utf8(self.body.clone()).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn param(&self, name: &str) -> Option<&str> {
        self.params.get(name).map(String::as_str)
    }

    pub fn param_as<T>(&self, name: &str) -> Result<T>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_required_value(
            self.param(name).map(ToString::to_string),
            "path parameter",
            name,
        )
    }

    pub fn optional_param_as<T>(&self, name: &str) -> Result<Option<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_optional_value(
            self.param(name).map(ToString::to_string),
            "path parameter",
            name,
        )
    }

    pub fn host_param(&self, name: &str) -> Option<&str> {
        self.host_params.get(name).map(String::as_str)
    }

    pub fn host_param_as<T>(&self, name: &str) -> Result<T>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_required_value(
            self.host_param(name).map(ToString::to_string),
            "host parameter",
            name,
        )
    }

    pub fn optional_host_param_as<T>(&self, name: &str) -> Result<Option<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_optional_value(
            self.host_param(name).map(ToString::to_string),
            "host parameter",
            name,
        )
    }

    pub fn params<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let params = serde_urlencoded::to_string(&self.params)
            .map_err(|err| BootError::BadRequest(err.to_string()))?;
        serde_urlencoded::from_str(&params).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn validated_params<T>(&self) -> Result<T>
    where
        T: DeserializeOwned + Validate,
    {
        let value = self.params()?;
        validate_value(value)
    }

    pub fn host_params<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let params = serde_urlencoded::to_string(&self.host_params)
            .map_err(|err| BootError::BadRequest(err.to_string()))?;
        serde_urlencoded::from_str(&params).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn validated_host_params<T>(&self) -> Result<T>
    where
        T: DeserializeOwned + Validate,
    {
        let value = self.host_params()?;
        validate_value(value)
    }

    pub fn query_param(&self, name: &str) -> Option<&str> {
        self.query.get(name).map(String::as_str)
    }

    pub fn query_value(&self, name: &str) -> Result<Option<String>> {
        Ok(self
            .query_pairs()?
            .into_iter()
            .find_map(|(key, value)| (key == name).then_some(value)))
    }

    pub fn query_value_as<T>(&self, name: &str) -> Result<T>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_required_value(self.query_value(name)?, "query parameter", name)
    }

    pub fn optional_query_value_as<T>(&self, name: &str) -> Result<Option<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_optional_value(self.query_value(name)?, "query parameter", name)
    }

    pub fn query_values(&self, name: &str) -> Result<Vec<String>> {
        Ok(self
            .query_pairs()?
            .into_iter()
            .filter_map(|(key, value)| (key == name).then_some(value))
            .collect())
    }

    pub fn query_values_as<T>(&self, name: &str) -> Result<Vec<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        self.query_values(name)?
            .into_iter()
            .map(|value| parse_value(value, "query parameter", name))
            .collect()
    }

    pub fn query_pairs(&self) -> Result<Vec<(String, String)>> {
        match self.query_string.as_deref() {
            Some(query) => parse_query_pairs(query),
            None => Ok(self
                .query
                .iter()
                .map(|(key, value)| (key.clone(), value.clone()))
                .collect()),
        }
    }

    pub fn header(&self, name: &str) -> Option<&str> {
        get_header(&self.headers, name)
    }

    pub fn header_as<T>(&self, name: &str) -> Result<T>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_required_value(self.header(name).map(ToString::to_string), "header", name)
    }

    pub fn optional_header_as<T>(&self, name: &str) -> Result<Option<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_optional_value(self.header(name).map(ToString::to_string), "header", name)
    }

    pub fn host(&self) -> Option<&str> {
        self.header("host").and_then(normalize_host_header)
    }

    pub fn ip(&self) -> Option<String> {
        self.forwarded_for_ip()
            .or_else(|| self.forwarded_header_ip("x-forwarded-for"))
            .or_else(|| self.forwarded_header_ip("x-real-ip"))
    }

    pub fn ip_as<T>(&self) -> Result<T>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_required_value(self.ip(), "IP address", "ip")
    }

    pub fn optional_ip_as<T>(&self) -> Result<Option<T>>
    where
        T: FromStr,
        T::Err: fmt::Display,
    {
        parse_optional_value(self.ip(), "IP address", "ip")
    }

    pub fn header_values(&self, name: &str) -> Vec<&str> {
        let mut values = self.header(name).into_iter().collect::<Vec<_>>();
        values.extend(
            self.appended_headers
                .iter()
                .filter(|(key, _)| key.eq_ignore_ascii_case(name))
                .map(|(_, value)| value.as_str()),
        );
        values
    }

    pub fn authorization(&self) -> Option<&str> {
        self.header_values("authorization").into_iter().next()
    }

    fn forwarded_header_ip(&self, name: &str) -> Option<String> {
        self.header_values(name)
            .into_iter()
            .flat_map(|value| value.split(','))
            .map(str::trim)
            .find(|value| !value.is_empty())
            .map(ToString::to_string)
    }

    fn forwarded_for_ip(&self) -> Option<String> {
        self.header_values("forwarded")
            .into_iter()
            .flat_map(|value| value.split(','))
            .flat_map(|entry| entry.split(';'))
            .filter_map(|part| part.trim().split_once('='))
            .find_map(|(key, value)| {
                key.trim().eq_ignore_ascii_case("for").then(|| {
                    value
                        .trim()
                        .trim_matches('"')
                        .trim_matches(['[', ']'])
                        .to_string()
                })
            })
            .filter(|value| !value.is_empty())
    }

    pub fn bearer_token(&self) -> Option<&str> {
        let authorization = self.authorization()?.trim();
        let mut parts = authorization.splitn(2, char::is_whitespace);
        let scheme = parts.next()?;
        let token = parts.next()?.trim();

        if scheme.eq_ignore_ascii_case("bearer") && !token.is_empty() {
            Some(token)
        } else {
            None
        }
    }

    pub fn require_bearer_token(&self) -> Result<&str> {
        self.bearer_token()
            .ok_or_else(|| BootError::Unauthorized("missing bearer token".to_string()))
    }

    pub fn content_type(&self) -> Option<&str> {
        self.header_values("content-type").into_iter().next()
    }

    pub fn content_length(&self) -> Result<Option<u64>> {
        let Some(content_length) = self.header_values("content-length").into_iter().next() else {
            return Ok(None);
        };

        parse_content_length(content_length)
            .map(Some)
            .ok_or_else(|| {
                BootError::BadRequest(format!("invalid content-length header: {content_length}"))
            })
    }

    pub fn strict_content_length(&self) -> Result<Option<u64>> {
        strict_content_length_values(
            self.header_values("content-length"),
            |content_length| {
                BootError::BadRequest(format!("invalid content-length header: {content_length}"))
            },
            |expected_content_length, content_length| {
                BootError::BadRequest(format!(
                    "conflicting content-length headers: {expected_content_length} != {content_length}"
                ))
            },
        )
    }

    pub fn validate_content_length(&self) -> Result<()> {
        let Some(content_length) = self.strict_content_length()? else {
            return Ok(());
        };
        let actual_body_length = self.body.len() as u64;
        if actual_body_length == content_length {
            return Ok(());
        }

        Err(BootError::BadRequest(format!(
            "content-length header does not match request body length: expected {content_length}, got {actual_body_length}"
        )))
    }

    pub fn validate_body_limit(&self, body_limit: usize) -> Result<()> {
        if self
            .strict_content_length()?
            .is_some_and(|content_length| content_length > body_limit as u64)
            || self.body.len() > body_limit
        {
            return Err(BootError::PayloadTooLarge(format!(
                "request body exceeds {body_limit} bytes"
            )));
        }

        Ok(())
    }

    pub fn validate(&self) -> Result<()> {
        self.validate_headers()?;
        self.validate_content_length()
    }

    pub fn validate_with_body_limit(&self, body_limit: usize) -> Result<()> {
        self.validate_headers()?;
        self.validate_body_limit(body_limit)?;
        self.validate_content_length()
    }

    pub fn is_content_type(&self, media_type: &str) -> bool {
        self.content_type()
            .is_some_and(|content_type| matches_media_type(content_type, media_type))
    }

    pub fn is_json_content_type(&self) -> bool {
        self.content_type().is_some_and(is_json_media_type)
    }

    pub fn require_json_content_type(&self) -> Result<()> {
        if self.is_json_content_type() {
            return Ok(());
        }

        let message = match self.content_type() {
            Some(content_type) => format!("expected JSON content type, got {content_type}"),
            None => "expected JSON content type".to_string(),
        };
        Err(BootError::UnsupportedMediaType(message))
    }

    pub fn accepts_json(&self) -> bool {
        accepts_json_response(&self.header_values("accept"))
    }

    pub fn require_accepts_json(&self) -> Result<()> {
        if self.accepts_json() {
            return Ok(());
        }

        Err(BootError::NotAcceptable(
            "expected client to accept JSON response".to_string(),
        ))
    }

    pub fn accepts_event_stream(&self) -> bool {
        accepts_event_stream_response(&self.header_values("accept"))
    }

    pub fn require_accepts_event_stream(&self) -> Result<()> {
        if self.accepts_event_stream() {
            return Ok(());
        }

        Err(BootError::NotAcceptable(
            "expected client to accept text/event-stream response".to_string(),
        ))
    }

    pub fn json<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        serde_json::from_slice(&self.body).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn validated_json<T>(&self) -> Result<T>
    where
        T: DeserializeOwned + Validate,
    {
        let value = self.json()?;
        validate_value(value)
    }

    pub fn json_with_content_type<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        self.require_json_content_type()?;
        self.json()
    }

    pub fn validated_json_with_content_type<T>(&self) -> Result<T>
    where
        T: DeserializeOwned + Validate,
    {
        self.require_json_content_type()?;
        self.validated_json()
    }

    pub fn query<T>(&self) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let query = match self.query_string.as_deref() {
            Some(query) => {
                validate_percent_encoding(query)?;
                query.to_string()
            }
            None => serde_urlencoded::to_string(&self.query)
                .map_err(|err| BootError::BadRequest(err.to_string()))?,
        };
        serde_urlencoded::from_str(&query).map_err(|err| BootError::BadRequest(err.to_string()))
    }

    pub fn validated_query<T>(&self) -> Result<T>
    where
        T: DeserializeOwned + Validate,
    {
        let value = self.query()?;
        validate_value(value)
    }
}

pub(super) fn parse_required_value<T>(value: Option<String>, label: &str, name: &str) -> Result<T>
where
    T: FromStr,
    T::Err: fmt::Display,
{
    let Some(value) = value else {
        return Err(BootError::BadRequest(format!("missing {label}: {name}")));
    };
    parse_value(value, label, name)
}

pub(super) fn parse_optional_value<T>(
    value: Option<String>,
    label: &str,
    name: &str,
) -> Result<Option<T>>
where
    T: FromStr,
    T::Err: fmt::Display,
{
    value
        .map(|value| parse_value(value, label, name))
        .transpose()
}

pub(super) fn parse_value<T>(value: String, label: &str, name: &str) -> Result<T>
where
    T: FromStr,
    T::Err: fmt::Display,
{
    value
        .parse::<T>()
        .map_err(|error| BootError::BadRequest(format!("invalid {label} {name}: {error}")))
}

fn validate_request_header(name: &str, value: &str) -> Result<()> {
    validate_header_name(name).map_err(|message| {
        BootError::BadRequest(format!("invalid request header name {name:?}: {message}"))
    })?;
    validate_header_value(value).map_err(|message| {
        BootError::BadRequest(format!(
            "invalid request header value for {name:?}: {message}"
        ))
    })
}