arch_pkg_text/srcinfo/query/
forgetful.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
use super::{
    utils::{non_blank_trimmed_lines, parse_line},
    ChecksumValue, Checksums, ChecksumsMut, Query, QueryChecksumItem, QueryMut, QueryRawTextItem,
    Section,
};
use crate::{
    srcinfo::field::{FieldName, ParsedField, RawField},
    value::{Architecture, Name},
};
use iter_scan::IterScan;
use pipe_trait::Pipe;

/// [Query] without a cache.
#[derive(Debug, Clone, Copy)]
pub struct ForgetfulQuerier<'a>(&'a str);

impl<'a> ForgetfulQuerier<'a> {
    /// Query the `text` without cache.
    pub const fn new(text: &'a str) -> Self {
        ForgetfulQuerier(text)
    }

    /// List all items of known fields.
    fn all_known_items(
        &self,
    ) -> impl Iterator<Item = (Section<'a>, (ParsedField<&'a str>, &'a str))> {
        self.0
            .pipe(non_blank_trimmed_lines)
            .map_while(parse_line)
            .filter_map(known_field)
            .filter(|(_, value)| !value.is_empty())
            .scan_state_copy(Section::Base, scan_section)
    }
}

impl<'a> Query<'a> for ForgetfulQuerier<'a> {
    fn query_raw_text(&self, field_name: FieldName) -> impl Iterator<Item = QueryRawTextItem<'a>> {
        self.all_known_items()
            .filter(move |(_, (field, _))| *field.name() == field_name)
            .map(|(section, (field, value))| {
                QueryRawTextItem::from_tuple3((
                    value,
                    section,
                    field.architecture_str().map(Architecture::new),
                ))
            })
    }
}

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

impl<'a> Checksums<'a> for ForgetfulQuerier<'a> {
    fn checksums(&self) -> impl Iterator<Item = QueryChecksumItem<'a>> {
        self.all_known_items()
            .filter_map(|(section, (field, value))| {
                ChecksumValue::try_from_field_name(*field.name(), value)
                    .map(|value| (value, section, field.architecture_str().map(Architecture)))
            })
            .map(QueryChecksumItem::from_tuple3)
    }
}

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

/// Callback function to pass to `.filter_map` to filter out unknown fields.
fn known_field<'a, Architecture, Acquaintance>(
    (field, acquaintance): (RawField<'a>, Acquaintance),
) -> Option<(ParsedField<Architecture>, Acquaintance)>
where
    &'a str: TryInto<Architecture>,
{
    field
        .to_parsed::<FieldName, Architecture>()
        .map(|field| (field, acquaintance))
        .ok()
}

/// Callback function to pass to `.scan_state_*` to attach sections to items.
fn scan_section<'a>(
    section: Section<'a>,
    (field, value): (ParsedField<&'a str>, &'a str),
) -> (Section<'a>, (ParsedField<&'a str>, &'a str)) {
    match field.name() {
        FieldName::Name => (Section::Derivative(Name(value)), (field, value)),
        _ => (section, (field, value)),
    }
}