arch_pkg_text/desc/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
use super::QueryMut;
use crate::desc::field::{FieldName, ParsedField, RawField};

/// [Query](QueryMut) with a cache.
#[derive(Debug, Clone)]
pub struct MemoQuerier<'a> {
    text: &'a str,
    cache: Cache<'a>,
    last: Option<(&'a str, RawField<'a>)>,
}

impl<'a> MemoQuerier<'a> {
    /// Query the `text` with a cache.
    pub fn new(text: &'a str) -> Self {
        MemoQuerier {
            text,
            cache: Cache::default(),
            last: None,
        }
    }

    /// Parse the next key-value pair, save it to cache and return it.
    fn next_entry(&mut self) -> Option<(RawField<'a>, &'a str)> {
        let mut lines = self.text.lines();

        let (field_str, raw_field) = if let Some((field_str, raw_field)) = self.last {
            lines.next()?;
            (field_str, raw_field)
        } else {
            let field_str = lines.next()?.trim();
            let raw_field = RawField::parse_raw(field_str).ok()?;
            (field_str, raw_field)
        };

        let value_start_offset =
            field_str.as_ptr() as usize + field_str.len() - self.text.as_ptr() as usize;
        let next = lines.find_map(|line| -> Option<(&'a str, RawField<'a>)> {
            let field_str = line.trim();
            let raw_field = RawField::parse_raw(field_str).ok()?;
            Some((field_str, raw_field))
        });

        let Some((next_field_str, next_raw_field)) = next else {
            let value = self.text[value_start_offset..].trim_matches(['\n', '\r']);
            self.text = "";
            self.last = None;
            return Some((raw_field, value));
        };

        let value_end_offset = next_field_str.as_ptr() as usize - self.text.as_ptr() as usize;
        let value = self.text[value_start_offset..value_end_offset].trim_matches(['\n', '\r']);

        // prepare for the next call
        self.last = Some((next_field_str, next_raw_field));
        self.text = &self.text[value_end_offset..];

        Some((raw_field, value))
    }

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

impl<'a> QueryMut<'a> for MemoQuerier<'a> {
    fn query_raw_text_mut(&mut self, field: ParsedField) -> Option<&'a str> {
        if let Some(value) = self.cache.get(field.name()) {
            return value;
        }

        while let Some((raw_field, value)) = self.next_entry() {
            let Ok(parsed_field) = raw_field.to_parsed::<FieldName>() else {
                continue;
            };
            let value = if value.is_empty() { None } else { Some(value) };
            self.cache.add(&parsed_field, value);
            if parsed_field == field {
                return value;
            }
        }

        None
    }
}

macro_rules! def_cache {
    ($(
        $(#[$attrs:meta])*
        $field:ident $(,)? $(;)?
    )*) => {
        #[derive(Debug, Clone, Copy)]
        enum CacheErr {
            OccupiedWithNone,
            Unoccupied,
        }

        #[derive(Debug, Clone)]
        #[allow(non_snake_case, reason = "We don't access the field names directly, keep it simple.")]
        struct Cache<'a> {$(
            $(#[$attrs])*
            $field: Result<&'a str, CacheErr>, // Result<&str, CacheErr> uses less memory than Option<Option<&str>>
        )*}

        impl<'a> Cache<'a> {
            fn get(&self, field: &FieldName) -> Option<Option<&'a str>> {
                match field {$(
                    FieldName::$field => match self.$field {
                        Ok(value) => Some(Some(value)),
                        Err(CacheErr::OccupiedWithNone) => Some(None),
                        Err(CacheErr::Unoccupied) => None,
                    },
                )*}
            }

            fn add(&mut self, field: &FieldName, value: Option<&'a str>) {
                match (field, value) {$(
                    (FieldName::$field, Some(value)) => self.$field = Ok(value),
                    (FieldName::$field, None) => self.$field = Err(CacheErr::OccupiedWithNone),
                )*}
            }
        }

        impl<'a> Default for Cache<'a> {
            fn default() -> Self {
                Cache {$(
                    $field: Err(CacheErr::Unoccupied),
                )*}
            }
        }

        #[test]
        fn test_cache_fields() {$({
            use pretty_assertions::assert_eq;
            let field = &FieldName::$field;
            let mut cache = Cache::default();
            assert_eq!(cache.get(field), None);
            cache.add(field, None);
            assert_eq!(cache.get(field), Some(None));
            cache.add(field, Some("foo"));
            assert_eq!(cache.get(field), Some(Some("foo")));
        })*}
    };
}

def_cache!(
    FileName Name Base Version Description Groups
    CompressedSize InstalledSize Md5Checksum Sha256Checksum
    PgpSignature Url License Architecture BuildDate Packager
    Dependencies CheckDependencies MakeDependencies OptionalDependencies
    Provides Conflicts Replaces
);

#[cfg(feature = "parking_lot")]
mod parking_lot_ext;
#[cfg(feature = "std")]
mod std_ext;