arch_pkg_text/parse/
desc.rs

1use super::{ParseWithIssues, PartialParse, PartialParseResult};
2use crate::desc::{
3    FieldName, ParseRawFieldError, ParsedField, Query, QueryMut, RawField,
4    misc::{ReuseAdvice, True},
5};
6use derive_more::{Display, Error};
7use lines_inclusive::{LinesInclusive, LinesInclusiveIter};
8use pipe_trait::Pipe;
9
10macro_rules! def_struct {
11    ($(
12        $(#[$attrs:meta])*
13        $field:ident $(,)? $(;)?
14    )*) => {
15        /// Parsed data of a `desc` file text.
16        ///
17        /// Every function call in [`Query`] and [`QueryMut`] is constant time.
18        #[derive(Debug, Default, Clone, Copy)]
19        #[allow(non_snake_case, reason = "We don't access the field names directly, keep it simple.")]
20        pub struct ParsedDesc<'a> {$(
21            $(#[$attrs])*
22            $field: Option<&'a str>,
23        )*}
24
25        impl<'a> ParsedDesc<'a> {
26            /// Get a raw value from the querier.
27            fn get_raw_value(&self, field_name: FieldName) -> Option<&'a str> {
28                match field_name {$(
29                    FieldName::$field => self.$field,
30                )*}
31            }
32
33            /// Add a raw value into the querier.
34            fn set_raw_value(&mut self, field_name: FieldName, raw_value: &'a str) {
35                match field_name {$(
36                    FieldName::$field => self.$field = Some(raw_value),
37                )*}
38            }
39        }
40    };
41}
42
43def_struct!(
44    FileName Name Base Version Description Groups
45    CompressedSize InstalledSize Md5Checksum Sha256Checksum
46    PgpSignature Url License Architecture BuildDate Packager
47    Dependencies CheckDependencies MakeDependencies OptionalDependencies
48    Provides Conflicts Replaces
49);
50
51/// Error type of [`ParsedDesc::parse`].
52#[derive(Debug, Display, Error, Clone, Copy)]
53pub enum DescParseError<'a> {
54    #[display("Input is empty")]
55    EmptyInput,
56    #[display("Receive a value without field: {_0:?}")]
57    ValueWithoutField(#[error(not(source))] &'a str),
58}
59
60/// Issue that may arise during parsing.
61#[derive(Debug, Clone, Copy)]
62pub enum DescParseIssue<'a> {
63    EmptyInput,
64    FirstLineIsNotAField(&'a str, ParseRawFieldError),
65    UnknownField(RawField<'a>),
66}
67
68impl<'a> DescParseIssue<'a> {
69    /// Return `Ok(())` if the issue was [`DescParseIssue::UnknownField`],
70    /// or return an `Err` of [`DescParseError`] otherwise.
71    fn ignore_unknown_field(self) -> Result<(), DescParseError<'a>> {
72        Err(match self {
73            DescParseIssue::EmptyInput => DescParseError::EmptyInput,
74            DescParseIssue::FirstLineIsNotAField(line, _) => {
75                DescParseError::ValueWithoutField(line)
76            }
77            DescParseIssue::UnknownField(_) => return Ok(()),
78        })
79    }
80}
81
82impl<'a> ParsedDesc<'a> {
83    /// Parse a `desc` file text, unknown fields are ignored.
84    pub fn parse(text: &'a str) -> Result<Self, DescParseError<'a>> {
85        ParsedDesc::partial_parse(text).try_into_complete()
86    }
87
88    /// Parse a `desc` file text with a callback that handle [parsing issues](DescParseIssue).
89    pub fn parse_with_issues<HandleIssue, Error>(
90        text: &'a str,
91        mut handle_issue: HandleIssue,
92    ) -> PartialParseResult<ParsedDesc<'a>, Error>
93    where
94        HandleIssue: FnMut(DescParseIssue<'a>) -> Result<(), Error>,
95    {
96        let mut parsed = ParsedDesc::default();
97        let mut lines = text.lines_inclusive();
98        let mut processed_length = 0;
99
100        macro_rules! return_or_continue {
101            ($issue:expr) => {
102                match handle_issue($issue) {
103                    Err(error) => return PartialParseResult::new_partial(parsed, error),
104                    Ok(()) => continue,
105                }
106            };
107        }
108
109        // parse the first field
110        let (first_line, first_field) = loop {
111            let Some(first_line) = lines.next() else {
112                return_or_continue!(DescParseIssue::EmptyInput);
113            };
114            let first_field = match first_line.trim().pipe(RawField::parse_raw) {
115                Ok(first_field) => first_field,
116                Err(error) => {
117                    return_or_continue!(DescParseIssue::FirstLineIsNotAField(first_line, error))
118                }
119            };
120            break (first_line, first_field);
121        };
122
123        // parse the remaining values and fields.
124        let mut current_field = Some((first_field, first_line));
125        while let Some((field, field_line)) = current_field {
126            let (value_length, next_field) = ParsedDesc::parse_next(&mut lines);
127            let value_start_offset = processed_length + field_line.len();
128            let value_end_offset = value_start_offset + value_length;
129            if let Ok(field) = field.to_parsed::<FieldName>() {
130                let value = text[value_start_offset..value_end_offset].trim();
131                parsed.set_raw_value(*field.name(), value);
132            } else {
133                return_or_continue!(DescParseIssue::UnknownField(field));
134            }
135            processed_length = value_end_offset;
136            current_field = next_field;
137        }
138
139        PartialParseResult::new_complete(parsed)
140    }
141
142    /// Parse a value until the end of input or when a [`RawField`] is found.
143    ///
144    /// This function returns a tuple of the length of the value and the next field.
145    fn parse_next(
146        remaining_lines: &mut LinesInclusiveIter<'a>,
147    ) -> (usize, Option<(RawField<'a>, &'a str)>) {
148        let mut value_length = 0;
149
150        for line in remaining_lines {
151            if let Ok(field) = line.trim().pipe(RawField::parse_raw) {
152                return (value_length, Some((field, line)));
153            }
154            value_length += line.len();
155        }
156
157        (value_length, None)
158    }
159}
160
161/// Try parsing a `desc` text, unknown fields are ignored, partial success means error.
162impl<'a> TryFrom<&'a str> for ParsedDesc<'a> {
163    /// Error that occurs when parsing fails or incomplete.
164    type Error = DescParseError<'a>;
165    /// Try parsing a `desc` text, unknown fields are ignored, partial success means error.
166    fn try_from(text: &'a str) -> Result<Self, Self::Error> {
167        ParsedDesc::parse(text)
168    }
169}
170
171impl<'a> PartialParse<&'a str> for ParsedDesc<'a> {
172    type Error = DescParseError<'a>;
173    fn partial_parse(input: &'a str) -> PartialParseResult<Self, Self::Error> {
174        ParsedDesc::parse_with_issues(input, DescParseIssue::ignore_unknown_field)
175    }
176}
177
178impl<'a, HandleIssue, Error> ParseWithIssues<&'a str, HandleIssue, Error> for ParsedDesc<'a>
179where
180    HandleIssue: FnMut(DescParseIssue<'a>) -> Result<(), Error>,
181{
182    fn parse_with_issues(
183        input: &'a str,
184        handle_issue: HandleIssue,
185    ) -> PartialParseResult<Self, Error> {
186        ParsedDesc::parse_with_issues(input, handle_issue)
187    }
188}
189
190impl<'a> Query<'a> for ParsedDesc<'a> {
191    fn query_raw_text(&self, field: ParsedField) -> Option<&'a str> {
192        self.get_raw_value(*field.name())
193    }
194}
195
196impl<'a> QueryMut<'a> for ParsedDesc<'a> {
197    fn query_raw_text_mut(&mut self, field: ParsedField) -> Option<&'a str> {
198        self.query_raw_text(field)
199    }
200}
201
202impl ReuseAdvice for ParsedDesc<'_> {
203    /// [`ParsedDesc`] costs O(n) time to construct (n being text length).
204    /// Performing a lookup on it costs O(1) time.
205    ///
206    /// This struct is designed to be reused.
207    type ShouldReuse = True;
208}