web_static_pack_common/
pack_path.rs

1//! Pack path contains custom type for representing path inside a `pack`.
2
3use rkyv::{Archive, Serialize};
4use std::{borrow::Borrow, ops::Deref};
5
6/// [PackPath] represents path inside a `pack`. It will correspond to http
7/// request path (aka. uri/url) directly.
8///
9/// Custom type is used to enforce some rules, eg. starts with "/", contains
10/// only valid characters, etc.
11#[derive(Archive, Serialize, PartialEq, Eq, Hash, Debug)]
12#[rkyv(archived = PackPathArchived)]
13#[rkyv(derive(PartialEq, Eq, Hash, Debug))]
14pub struct PackPath {
15    inner: String,
16}
17impl PackPath {
18    /// Construct path from string representation. Refer to [self] for details.
19    /// Providing invalid path won't result in catastrophic failure, but
20    /// such will will never be resolved.
21    pub fn from_string(inner: String) -> Self {
22        Self { inner }
23    }
24}
25
26// to allow searching in HashMap directly by http path (which is str)
27impl Deref for PackPath {
28    type Target = str;
29
30    fn deref(&self) -> &Self::Target {
31        &self.inner
32    }
33}
34impl Borrow<str> for PackPath {
35    fn borrow(&self) -> &str {
36        self.inner.as_str()
37    }
38}
39
40impl Deref for PackPathArchived {
41    type Target = str;
42
43    fn deref(&self) -> &Self::Target {
44        &self.inner
45    }
46}
47impl Borrow<str> for PackPathArchived {
48    fn borrow(&self) -> &str {
49        self.inner.as_str()
50    }
51}