arch_pkg_text/srcinfo/query/
memo.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
mod cache;

use super::{
    utils::{parse_line, trimmed_line_is_blank},
    ChecksumType, ChecksumValue, ChecksumsMut, QueryChecksumItem, QueryMut, QueryRawTextItem,
    Section,
};
use crate::{
    srcinfo::field::FieldName,
    value::{Architecture, Name},
};
use cache::Cache;
use core::str::Lines;
use pipe_trait::Pipe;

/// [Query](QueryMut) with a cache.
#[derive(Debug, Clone)]
pub struct MemoQuerier<'a> {
    remaining_lines: Lines<'a>,
    current_section: Section<'a>,
    cache: Cache<'a>,
}

impl<'a> MemoQuerier<'a> {
    /// Query the fields of a `.SRCINFO` file with a cache.
    pub fn new(srcinfo: &'a str) -> Self {
        MemoQuerier {
            remaining_lines: srcinfo.lines(),
            current_section: Section::Base,
            cache: Cache::default(),
        }
    }

    /// Shrink the cache's capacity to fit its length.
    pub fn shrink_cache_to_fit(&mut self) {
        self.cache.shrink_to_fit();
    }

    /// Private function for testing the internal cache.
    #[doc(hidden)]
    pub fn __has_cache(&self, field_name: FieldName, index: usize) -> bool {
        self.cache.get(field_name, index).is_some()
    }

    /// Parse the next key-value pair, save it the cache and return it.
    fn next_entry(&mut self) -> Option<(FieldName, QueryRawTextItem<'a>)> {
        let line = self.remaining_lines.next()?.trim();
        if trimmed_line_is_blank(line) {
            return self.next_entry();
        }
        let (raw_field, value) = parse_line(line)?;
        let Ok(field) = raw_field.to_parsed::<FieldName, &str>() else {
            return self.next_entry();
        };
        if value.is_empty() {
            return self.next_entry();
        }
        let architecture = field.architecture_str().map(Architecture);
        if *field.name() == FieldName::Name && architecture.is_none() {
            self.current_section = value.pipe(Name).pipe(Section::Derivative);
        }
        let item = QueryRawTextItem::from_tuple3((value, self.current_section, architecture));
        self.cache.add(*field.name(), item);
        Some((*field.name(), item))
    }
}

/// Return type of [`QueryMut::query_raw_text_mut`] on an instance of [`MemoQuerier`].
struct QueryIter<'a, 'r> {
    querier: &'r mut MemoQuerier<'a>,
    field_name: FieldName,
    index: usize,
}

impl<'a, 'r> QueryIter<'a, 'r> {
    /// Create an iterator that queries `field_name` from `querier`.
    fn new(querier: &'r mut MemoQuerier<'a>, field_name: FieldName) -> Self {
        QueryIter {
            querier,
            field_name,
            index: 0,
        }
    }
}

impl<'a> Iterator for QueryIter<'a, '_> {
    type Item = QueryRawTextItem<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let QueryIter {
            querier,
            field_name,
            index,
        } = self;
        loop {
            if let Some(item) = querier.cache.get(*field_name, *index) {
                *index += 1;
                return Some(item);
            } else {
                querier.next_entry()?;
                continue;
            }
        }
    }
}

impl<'a> QueryMut<'a> for MemoQuerier<'a> {
    fn query_raw_text_mut(
        &mut self,
        field_name: FieldName,
    ) -> impl Iterator<Item = QueryRawTextItem<'a>> {
        QueryIter::new(self, field_name)
    }
}

/// Return type of [`ChecksumsMut::checksums_mut`] on an instance of [`MemoQuerier`].
struct ChecksumIter<'a, 'r> {
    querier: &'r mut MemoQuerier<'a>,
    checksum_type_id: usize,
    checksum_index: usize,
}

impl<'a, 'r> ChecksumIter<'a, 'r> {
    /// Create an iterator that queries all checksums from `querier`.
    fn new(querier: &'r mut MemoQuerier<'a>) -> Self {
        ChecksumIter {
            querier,
            checksum_type_id: 0,
            checksum_index: 0,
        }
    }
}

impl<'a> Iterator for ChecksumIter<'a, '_> {
    type Item = QueryChecksumItem<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let ChecksumIter {
            querier,
            checksum_type_id,
            checksum_index,
        } = self;
        loop {
            let checksum_type = *ChecksumType::TYPES.get(*checksum_type_id)?;
            let field_name = checksum_type.into_field_name();
            if let Some(item) = querier.cache.get(field_name, *checksum_index) {
                *checksum_index += 1;
                return item
                    .map(move |value| ChecksumValue::new(checksum_type, value))
                    .pipe(Some);
            } else if querier.next_entry().is_none() {
                *checksum_type_id += 1;
                *checksum_index = 0;
            }
        }
    }
}

impl<'a> ChecksumsMut<'a> for MemoQuerier<'a> {
    fn checksums_mut(&mut self) -> impl Iterator<Item = QueryChecksumItem<'a>> {
        ChecksumIter::new(self)
    }
}