arch_pkg_text/parse/srcinfo/
checksums.rs

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
use super::{ParsedSrcinfo, ParsedSrcinfoBaseSection, ParsedSrcinfoDerivativeSection};
use crate::{
    srcinfo::{ChecksumType, ChecksumValue, Checksums, ChecksumsMut, Query, QueryChecksumItem},
    value,
};
use pipe_trait::Pipe;

macro_rules! def_impl {
    ($(
        $checksum_variant:ident($checksum_content:ident) <- $query_method:ident;
    )*) => {
        /// Private [iterator](Iterator) type to be used as the underlying return types in [`Checksums`] and [`ChecksumsMut`].
        enum ChecksumsIter<$($checksum_variant,)*> {$(
            $checksum_variant($checksum_variant),
        )*}

        impl<'a, $($checksum_variant,)*> Iterator for ChecksumsIter<$($checksum_variant,)*>
        where
            $($checksum_variant: Iterator<Item = QueryChecksumItem<'a>>,)*
        {
            type Item = QueryChecksumItem<'a>;
            fn next(&mut self) -> Option<Self::Item> {
                match self {$(
                    ChecksumsIter::$checksum_variant(iter) => iter.next(),
                )*}
            }
        }

        impl<'a> ParsedSrcinfo<'a> {
            /// Query checksums from all sections of a single [`ChecksumType`].
            fn checksums_single_type(&self, checksum_type: ChecksumType) -> impl Iterator<Item = QueryChecksumItem<'a>> + '_ {
                match checksum_type {$(
                    ChecksumType::$checksum_variant => {
                        self.$query_method()
                            .map(|item| item.map(ChecksumValue::$checksum_variant))
                            .pipe(ChecksumsIter::$checksum_variant)
                    }
                )*}
            }
        }

        impl<'a> Checksums<'a> for ParsedSrcinfo<'a> {
            fn checksums(&self) -> impl Iterator<Item = QueryChecksumItem<'a>> {
                ChecksumType::all_types()
                    .flat_map(|checksum_type| self.checksums_single_type(*checksum_type))
            }
        }

        impl<'a> ChecksumsMut<'a> for ParsedSrcinfo<'a> {
            fn checksums_mut(&mut self) -> impl Iterator<Item = QueryChecksumItem<'a>> {
                self.checksums()
            }
        }

        /// Slice of list of checksums.
        enum SectionChecksumsView<'a, 'r> {$(
            $checksum_variant(&'r [(value::$checksum_content<'a>, Option<value::Architecture<'a>>)]),
        )*}

        /// Private [iterator](Iterator) type to be used as the underlying return types in
        /// [`ParsedSrcinfoBaseSection::checksums`] and [`ParsedSrcinfoDerivativeSection::checksums`].
        struct SectionChecksumsIter<'a, 'r> {
            index: usize,
            view: SectionChecksumsView<'a, 'r>,
        }

        impl<'a, 'r> SectionChecksumsIter<'a, 'r> {
            /// Create a new iterator.
            fn new(view: SectionChecksumsView<'a, 'r>) -> Self {
                SectionChecksumsIter { index: 0, view }
            }
        }

        impl<'a, 'r> Iterator for SectionChecksumsIter<'a, 'r> {
            type Item = (ChecksumValue<'a>, Option<value::Architecture<'a>>);
            fn next(&mut self) -> Option<Self::Item> {
                let value = match &self.view {$(
                    SectionChecksumsView::$checksum_variant(view) => {
                        let (value, architecture) = view.get(self.index)?;
                        (ChecksumValue::$checksum_variant(*value), *architecture)
                    }
                )*};
                self.index += 1;
                Some(value)
            }
        }

        impl<'a> ParsedSrcinfoBaseSection<'a> {
            /// Query checksums of a single [`ChecksumType`].
            fn checksums_single_type(&self, checksum_type: ChecksumType) -> impl Iterator<Item = (ChecksumValue<'a>, Option<value::Architecture<'a>>)> + '_ {
                let view = match checksum_type {$(
                    ChecksumType::$checksum_variant => SectionChecksumsView::$checksum_variant(self.$query_method()),
                )*};
                SectionChecksumsIter::new(view)
            }
        }

        impl<'a> ParsedSrcinfoDerivativeSection<'a> {
            /// Query checksums of a single [`ChecksumType`].
            fn checksums_single_type(&self, checksum_type: ChecksumType) -> impl Iterator<Item = (ChecksumValue<'a>, Option<value::Architecture<'a>>)> + '_ {
                let view = match checksum_type {$(
                    ChecksumType::$checksum_variant => SectionChecksumsView::$checksum_variant(self.$query_method()),
                )*};
                SectionChecksumsIter::new(view)
            }
        }
    };
}

def_impl! {
    Md5(SkipOrHex128) <- md5_checksums;
    Sha1(SkipOrHex160) <- sha1_checksums;
    Sha224(SkipOrHex224) <- sha224_checksums;
    Sha256(SkipOrHex256) <- sha256_checksums;
    Sha384(SkipOrHex384) <- sha384_checksums;
    Sha512(SkipOrHex512) <- sha512_checksums;
    Blake2b(SkipOrHex512) <- blake2b_checksums;
}

impl<'a> ParsedSrcinfoBaseSection<'a> {
    /// List checksums of all [`ChecksumType`].
    pub fn checksums(
        &self,
    ) -> impl Iterator<Item = (ChecksumValue<'a>, Option<value::Architecture<'a>>)> + '_ {
        ChecksumType::all_types()
            .flat_map(|checksum_type| self.checksums_single_type(*checksum_type))
    }
}

impl<'a> ParsedSrcinfoDerivativeSection<'a> {
    /// List checksums of all [`ChecksumType`].
    pub fn checksums(
        &self,
    ) -> impl Iterator<Item = (ChecksumValue<'a>, Option<value::Architecture<'a>>)> + '_ {
        ChecksumType::all_types()
            .flat_map(|checksum_type| self.checksums_single_type(*checksum_type))
    }
}