cloudfront_logs/referential/
raw.rs

1use crate::{borrowed::UnvalidatedRawLogline as BorrowedLine, shared::validate_line, types::*};
2
3/// A (thread safe) line string
4///
5/// We use a [`Arc<str>`] over [`String`] to communicate its immutability and fixedness.
6/// (`Arc<str>`/`Box<str>`/`&str` don't carry any capacity data, only the length.)
7///
8/// While `Box<str>` is also possible, the box is not thread safe and
9/// and both types have pretty similar performance characteristics.
10pub type LineStr = Arc<str>;
11
12self_cell::self_cell!(
13    struct Container {
14        owner: LineStr,
15
16        #[covariant]
17        dependent: BorrowedLine,
18    }
19
20    impl {Debug, PartialEq}
21);
22
23impl Clone for Container {
24    fn clone(&self) -> Self {
25        let input = Arc::clone(self.borrow_owner());
26        Self::new(input, |line| BorrowedLine::from(line.as_ref()))
27    }
28}
29
30pub type UnvalidatedLogline = Logline<Unvalidated>;
31pub type ValidatedLogline = Logline<Validated>;
32
33#[derive(Debug, Clone, PartialEq)]
34pub struct Logline<V> {
35    inner: Container,
36    _marker: PhantomData<V>,
37}
38
39impl<V> Logline<V> {
40    pub fn view(&self) -> &BorrowedLine<'_> {
41        self.inner.borrow_dependent()
42    }
43
44    pub fn as_raw(&self) -> &str {
45        self.inner.borrow_owner()
46    }
47
48    pub fn into_raw(self) -> LineStr {
49        self.inner.into_owner()
50    }
51}
52
53macro_rules! impl_try_from {
54    ($in:ty, $out_v:ident, $out_u:ident) => {
55        impl TryFrom<$in> for $out_v {
56            type Error = &'static str;
57
58            fn try_from(line: $in) -> Result<Self, Self::Error> {
59                let line: LineStr = line.into();
60                validate_line(&line)?;
61                let container = Container::new(line, |line| BorrowedLine::from(line.as_ref()));
62                Ok($out_v {
63                    inner: container,
64                    _marker: PhantomData,
65                })
66            }
67        }
68
69        impl TryFrom<$in> for $out_u {
70            type Error = &'static str;
71
72            fn try_from(line: $in) -> Result<Self, Self::Error> {
73                let container =
74                    Container::new(line.into(), |line| BorrowedLine::from(line.as_ref()));
75                Ok($out_u {
76                    inner: container,
77                    _marker: PhantomData,
78                })
79            }
80        }
81    };
82}
83
84impl_try_from!(&str, ValidatedLogline, UnvalidatedLogline);
85impl_try_from!(String, ValidatedLogline, UnvalidatedLogline);
86impl_try_from!(Box<str>, ValidatedLogline, UnvalidatedLogline);
87impl_try_from!(Arc<str>, ValidatedLogline, UnvalidatedLogline);