netlist_db/
builder.rs

1use crate::span::Span;
2use alloc::borrow::Cow;
3pub trait Builder<'s> {
4    type Out: 's;
5    fn build(&self, file: &'s str) -> Self::Out;
6}
7#[macro_export]
8macro_rules! self_builder {
9    ($t:ty) => {
10        impl<'s> $crate::builder::Builder<'s> for $t {
11            type Out = $t;
12            #[inline]
13            fn build(&self, _file: &'s str) -> Self::Out {
14                *self
15            }
16        }
17    };
18}
19self_builder!(usize);
20self_builder!(f64);
21self_builder!(u8);
22
23impl<'s> Builder<'s> for Span {
24    type Out = Cow<'s, str>;
25    #[inline]
26    fn build(&self, file: &'s str) -> Self::Out {
27        Cow::Borrowed(&file[self])
28    }
29}
30
31impl<'s, T: Builder<'s>> Builder<'s> for Vec<T> {
32    type Out = Vec<T::Out>;
33    #[inline]
34    fn build(&self, file: &'s str) -> Self::Out {
35        self.iter().map(|s| s.build(file)).collect()
36    }
37}
38
39impl<'s, T: Builder<'s>> Builder<'s> for Option<T> {
40    type Out = Option<T::Out>;
41    #[inline]
42    fn build(&self, file: &'s str) -> Self::Out {
43        self.as_ref().map(|s| s.build(file))
44    }
45}
46impl<'s, T1: Builder<'s>, T2: Builder<'s>> Builder<'s> for (T1, T2) {
47    type Out = (T1::Out, T2::Out);
48    #[inline]
49    fn build(&self, file: &'s str) -> Self::Out {
50        (self.0.build(file), self.1.build(file))
51    }
52}
53impl<'s, T1: Builder<'s>, T2: Builder<'s>, T3: Builder<'s>> Builder<'s> for (T1, T2, T3) {
54    type Out = (T1::Out, T2::Out, T3::Out);
55    #[inline]
56    fn build(&self, file: &'s str) -> Self::Out {
57        (self.0.build(file), self.1.build(file), self.2.build(file))
58    }
59}