arch_pkg_text/parse/
srcinfo.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
mod checksums;
mod data;

use super::PartialParseResult;
use crate::{
    srcinfo::{
        utils::{non_blank_trimmed_lines, parse_line},
        Field, FieldName, ParsedField, RawField, Section,
    },
    value,
};
use derive_more::{Display, Error};
use indexmap::IndexMap;
use pipe_trait::Pipe;

pub use data::{
    ParsedSrcinfoBaseSection, ParsedSrcinfoBaseUniqueFieldDuplicationError,
    ParsedSrcinfoDerivativeSection, ParsedSrcinfoDerivativeUniqueFieldDuplicationError,
};

/// Parsed information of `.SRCINFO`.
#[derive(Debug, Default, Clone)]
pub struct ParsedSrcinfo<'a> {
    /// The section under `pkgbase`.
    pub base: ParsedSrcinfoBaseSection<'a>,
    /// The sections under `pkgname`.
    pub derivatives: IndexMap<value::Name<'a>, ParsedSrcinfoDerivativeSection<'a>>,
}

/// Write cursor of the sections in [`ParsedSrcinfo`].
enum ParsedSrcinfoSectionMut<'a, 'r> {
    /// Write to the `pkgbase` section.
    Base(&'r mut ParsedSrcinfoBaseSection<'a>),
    /// Write to a `pkgname` section.
    Derivative(ParsedSrcinfoDerivativeSectionEntryMut<'a, 'r>),
}

impl<'a> ParsedSrcinfo<'a> {
    /// Get a section or create one if didn't exist.
    fn get_or_insert(&mut self, section: Section<'a>) -> ParsedSrcinfoSectionMut<'a, '_> {
        match section {
            Section::Base => self.base.pipe_mut(ParsedSrcinfoSectionMut::Base),
            Section::Derivative(name) => self
                .derivatives
                .entry(name)
                .or_default()
                .pipe(|data| ParsedSrcinfoDerivativeSectionEntryMut::new(name, data))
                .pipe(ParsedSrcinfoSectionMut::Derivative),
        }
    }
}

/// Private error type for control flow.
enum AddFailure<'a> {
    /// Meet an entry with field `pkgname`.
    MeetHeader(value::Name<'a>),
    /// Meet an issue.
    Issue(SrcinfoParseIssue<'a>),
}

/// Create an [`Err`] of an [`AddFailure::Issue`] of a [`SrcinfoParseIssue::UnknownField`] of a [`ParsedField`].
fn unknown_field_from_parsed(field: ParsedField<&str>) -> Result<(), AddFailure<'_>> {
    Field::blank()
        .with_name(field.name_str())
        .with_architecture(field.architecture_str())
        .pipe(SrcinfoParseIssue::UnknownField)
        .pipe(AddFailure::Issue)
        .pipe(Err)
}

impl<'a> ParsedSrcinfoSectionMut<'a, '_> {
    /// Add an entry to a `pkgbase` or `pkgname` section.
    fn add(&mut self, field: ParsedField<&'a str>, value: &'a str) -> Result<(), AddFailure<'a>> {
        match self {
            ParsedSrcinfoSectionMut::Base(section) => section.add(field, value),
            ParsedSrcinfoSectionMut::Derivative(section) => section.add(field, value),
        }
    }

    /// Shrink all internal containers' capacities to fit.
    fn shrink_to_fit(&mut self) {
        match self {
            ParsedSrcinfoSectionMut::Base(section) => section.shrink_to_fit(),
            ParsedSrcinfoSectionMut::Derivative(section) => section.shrink_to_fit(),
        }
    }
}

impl<'a> ParsedSrcinfoBaseSection<'a> {
    /// Add a value to a unique entry.
    fn add_value_to_option<Value: Copy>(
        target: &mut Option<Value>,
        value: &'a str,
        make_value: impl FnOnce(&'a str) -> Value,
        make_error: impl FnOnce(Value) -> ParsedSrcinfoBaseUniqueFieldDuplicationError<'a>,
    ) -> Result<(), AddFailure<'a>> {
        let Some(old_value) = target else {
            *target = Some(make_value(value));
            return Ok(());
        };
        (*old_value)
            .pipe(make_error)
            .pipe(SrcinfoParseIssue::BaseUniqueFieldDuplication)
            .pipe(AddFailure::Issue)
            .pipe(Err)
    }
}

/// A pair of [`value::Name`] and [`ParsedSrcinfoDerivativeSection`].
struct ParsedSrcinfoDerivativeSectionEntryMut<'a, 'r> {
    name: value::Name<'a>,
    data: &'r mut ParsedSrcinfoDerivativeSection<'a>,
}

impl<'a, 'r> ParsedSrcinfoDerivativeSectionEntryMut<'a, 'r> {
    /// Create a new pair.
    fn new(name: value::Name<'a>, data: &'r mut ParsedSrcinfoDerivativeSection<'a>) -> Self {
        ParsedSrcinfoDerivativeSectionEntryMut { name, data }
    }

    /// Add a value to a unique entry.
    fn add_value_to_option<Value: Copy>(
        name: value::Name<'a>,
        target: &mut Option<Value>,
        value: &'a str,
        make_value: impl FnOnce(&'a str) -> Value,
        make_error: impl FnOnce(Value) -> ParsedSrcinfoDerivativeUniqueFieldDuplicationError<'a>,
    ) -> Result<(), AddFailure<'a>> {
        let Some(old_value) = target else {
            *target = Some(make_value(value));
            return Ok(());
        };
        (*old_value)
            .pipe(make_error)
            .pipe(move |error| SrcinfoParseIssue::DerivativeUniqueFieldDuplication(name, error))
            .pipe(AddFailure::Issue)
            .pipe(Err)
    }

    /// Shrink all internal containers' capacities to fit.
    fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit()
    }
}

/// Error type of [`ParsedSrcinfo::parse`].
#[derive(Debug, Display, Error, Clone, Copy)]
pub enum SrcinfoParseError<'a> {
    #[display("Failed to insert value to the pkgbase section: {_0}")]
    BaseUniqueFieldDuplication(
        #[error(not(source))] ParsedSrcinfoBaseUniqueFieldDuplicationError<'a>,
    ),
    #[display("Failed to insert value to the pkgname section named {_0}: {_1}")]
    DerivativeUniqueFieldDuplication(
        value::Name<'a>,
        ParsedSrcinfoDerivativeUniqueFieldDuplicationError<'a>,
    ),
    #[display("Invalid line: {_0:?}")]
    InvalidLine(#[error(not(source))] &'a str),
}

/// Return type of [`ParsedSrcinfo::parse`].
pub type SrcinfoParseReturn<'a> = PartialParseResult<ParsedSrcinfo<'a>, SrcinfoParseError<'a>>;

/// Issue that may arise during parsing.
#[derive(Debug, Clone, Copy)]
pub enum SrcinfoParseIssue<'a> {
    UnknownField(RawField<'a>),
    BaseUniqueFieldDuplication(ParsedSrcinfoBaseUniqueFieldDuplicationError<'a>),
    DerivativeUniqueFieldDuplication(
        value::Name<'a>,
        ParsedSrcinfoDerivativeUniqueFieldDuplicationError<'a>,
    ),
    InvalidLine(&'a str),
}

impl<'a> SrcinfoParseIssue<'a> {
    /// Return `Ok(())` if the issue was [`SrcinfoParseIssue::UnknownField`],
    /// or return an `Err` of [`SrcinfoParseError`] otherwise.
    fn ignore_unknown_field(self) -> Result<(), SrcinfoParseError<'a>> {
        Err(match self {
            SrcinfoParseIssue::UnknownField(_) => return Ok(()),
            SrcinfoParseIssue::BaseUniqueFieldDuplication(error) => {
                SrcinfoParseError::BaseUniqueFieldDuplication(error)
            }
            SrcinfoParseIssue::DerivativeUniqueFieldDuplication(name, error) => {
                SrcinfoParseError::DerivativeUniqueFieldDuplication(name, error)
            }
            SrcinfoParseIssue::InvalidLine(line) => SrcinfoParseError::InvalidLine(line),
        })
    }
}

impl<'a> ParsedSrcinfo<'a> {
    /// Parse `.SRCINFO` text, unknown fields are ignored.
    pub fn parse(text: &'a str) -> SrcinfoParseReturn<'a> {
        ParsedSrcinfo::parse_with_issues(text, SrcinfoParseIssue::ignore_unknown_field)
    }

    /// Parse `.SRCINFO` with a callback that handles [parsing issues](SrcinfoParseIssue).
    pub fn parse_with_issues<HandleIssue, Error>(
        text: &'a str,
        mut handle_issue: HandleIssue,
    ) -> PartialParseResult<ParsedSrcinfo<'a>, Error>
    where
        HandleIssue: FnMut(SrcinfoParseIssue<'a>) -> Result<(), Error>,
    {
        let mut parsed = ParsedSrcinfo::default();
        let lines = non_blank_trimmed_lines(text);
        let mut section_mut = parsed.get_or_insert(Section::Base);

        macro_rules! return_or_continue {
            ($issue:expr) => {
                match handle_issue($issue) {
                    Err(error) => return PartialParseResult::new_partial(parsed, error),
                    Ok(()) => continue,
                }
            };
        }

        for line in lines {
            let Some((field, value)) = parse_line(line) else {
                return_or_continue!(SrcinfoParseIssue::InvalidLine(line));
            };
            let Ok(field) = field.to_parsed::<FieldName, &str>() else {
                return_or_continue!(SrcinfoParseIssue::UnknownField(field));
            };
            if value.is_empty() {
                continue;
            }
            match section_mut.add(field, value) {
                Ok(()) => {}
                Err(AddFailure::MeetHeader(name)) => {
                    section_mut.shrink_to_fit();
                    section_mut = parsed.get_or_insert(Section::Derivative(name));
                }
                Err(AddFailure::Issue(issue)) => {
                    return_or_continue!(issue);
                }
            }
        }

        PartialParseResult::new_complete(parsed)
    }
}

/// Try parsing a `.SRCINFO` text, unknown fields are ignored, partial success means error.
impl<'a> TryFrom<&'a str> for ParsedSrcinfo<'a> {
    /// Error that occurs when parsing fails or incomplete.
    type Error = SrcinfoParseError<'a>;
    /// Try parsing a `.SRCINFO` text, unknown fields are ignored, partial success means error.
    fn try_from(text: &'a str) -> Result<Self, Self::Error> {
        ParsedSrcinfo::parse(text).try_into_complete()
    }
}