alpm_buildinfo/
buildinfo_v2.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::FromStr;

use alpm_types::digests::Sha256;
use alpm_types::Architecture;
use alpm_types::BuildDate;
use alpm_types::BuildDir;
use alpm_types::BuildEnv;
use alpm_types::BuildTool;
use alpm_types::BuildToolVersion;
use alpm_types::Checksum;
use alpm_types::InstalledPackage;
use alpm_types::Name;
use alpm_types::PackageOption;
use alpm_types::Packager;
use alpm_types::SchemaVersion;
use alpm_types::StartDir;
use alpm_types::Version;
use serde_with::serde_as;
use serde_with::DisplayFromStr;

use crate::buildinfo_v1::generate_buildinfo;
use crate::schema::Schema;
use crate::Error;

generate_buildinfo! {
    /// BUILDINFO version 2
    ///
    /// `BuildInfoV2` is (exclusively) compatible with data following the v2 specification of the
    /// BUILDINFO file.
    ///
    /// ## Examples
    ///
    /// ```
    /// use std::str::FromStr;
    ///
    /// use alpm_buildinfo::BuildInfoV2;
    ///
    /// # fn main() -> Result<(), alpm_buildinfo::Error> {
    /// let buildinfo_data = r#"format = 2
    /// pkgname = foo
    /// pkgbase = foo
    /// pkgver = 1:1.0.0-1
    /// pkgarch = any
    /// pkgbuild_sha256sum = b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
    /// packager = Foobar McFooface <foobar@mcfooface.org>
    /// builddate = 1
    /// builddir = /build
    /// startdir = /startdir/
    /// buildtool = devtools
    /// buildtoolver = 1:1.2.1-1-any
    /// buildenv = envfoo
    /// buildenv = envbar
    /// options = some_option
    /// options = !other_option
    /// installed = bar-1.2.3-1-any
    /// installed = beh-2.2.3-4-any
    /// "#;
    ///
    /// let buildinfo = BuildInfoV2::from_str(buildinfo_data)?;
    /// assert_eq!(buildinfo.to_string(), buildinfo_data);
    /// # Ok(())
    /// # }
    /// ```
    BuildInfoV2 {
        #[serde_as(as = "DisplayFromStr")]
        startdir: StartDir,

        #[serde_as(as = "DisplayFromStr")]
        buildtool: BuildTool,

        #[serde_as(as = "DisplayFromStr")]
        buildtoolver: BuildToolVersion,
    }
}

impl BuildInfoV2 {
    /// Create a new BuildInfoV2 from all required components
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        builddate: BuildDate,
        builddir: BuildDir,
        startdir: StartDir,
        buildtool: BuildTool,
        buildtoolver: BuildToolVersion,
        buildenv: Vec<BuildEnv>,
        format: SchemaVersion,
        installed: Vec<InstalledPackage>,
        options: Vec<PackageOption>,
        packager: Packager,
        pkgarch: Architecture,
        pkgbase: Name,
        pkgbuild_sha256sum: Checksum<Sha256>,
        pkgname: Name,
        pkgver: Version,
    ) -> Result<Self, Error> {
        if format.inner().major != 2 {
            return Err(Error::WrongSchemaVersion(format));
        }
        Ok(BuildInfoV2 {
            builddate,
            builddir,
            buildenv,
            format: format.try_into()?,
            installed,
            options,
            packager,
            pkgarch,
            pkgbase,
            pkgbuild_sha256sum,
            pkgname,
            pkgver,
            startdir,
            buildtool,
            buildtoolver,
        })
    }

    /// Returns the start directory of the build process.
    pub fn startdir(&self) -> &StartDir {
        &self.startdir
    }

    /// Returns the tool used for building the package.
    pub fn buildtool(&self) -> &BuildTool {
        &self.buildtool
    }

    /// Returns the version of the build tool.
    pub fn buildtoolver(&self) -> &BuildToolVersion {
        &self.buildtoolver
    }
}

impl FromStr for BuildInfoV2 {
    type Err = Error;
    /// Create a BuildInfoV2 from a &str
    ///
    /// ## Errors
    ///
    /// Returns an `Error` if any of the fields in `input` can not be validated according to
    /// `BuildInfoV2` or their respective own specification.
    fn from_str(input: &str) -> Result<BuildInfoV2, Self::Err> {
        let buildinfo: BuildInfoV2 = alpm_parsers::custom_ini::from_str(input)?;
        if buildinfo.format().inner().major != 2 {
            return Err(Error::WrongSchemaVersion(buildinfo.format().clone()));
        }
        Ok(buildinfo)
    }
}

impl Display for BuildInfoV2 {
    fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
        write!(
            fmt,
            "format = {}\n\
            pkgname = {}\n\
            pkgbase = {}\n\
            pkgver = {}\n\
            pkgarch = {}\n\
            pkgbuild_sha256sum = {}\n\
            packager = {}\n\
            builddate = {}\n\
            builddir = {}\n\
            startdir = {}\n\
            buildtool = {}\n\
            buildtoolver = {}\n\
            {}\n\
            {}\n\
            {}\n\
            ",
            self.format().inner().major,
            self.pkgname(),
            self.pkgbase(),
            self.pkgver(),
            self.pkgarch(),
            self.pkgbuild_sha256sum(),
            self.packager(),
            self.builddate(),
            self.builddir(),
            self.startdir(),
            self.buildtool(),
            self.buildtoolver(),
            self.buildenv()
                .iter()
                .map(|v| format!("buildenv = {v}"))
                .collect::<Vec<String>>()
                .join("\n"),
            self.options()
                .iter()
                .map(|v| format!("options = {v}"))
                .collect::<Vec<String>>()
                .join("\n"),
            self.installed()
                .iter()
                .map(|v| format!("installed = {v}"))
                .collect::<Vec<String>>()
                .join("\n"),
        )
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;
    use testresult::TestResult;

    use super::*;

    // Test data
    const VALID_BUILDINFOV2_CASE1: &str = r#"
builddate = 1
builddir = /build
startdir = /startdir/
buildtool = devtools
buildtoolver = 1:1.2.1-1-any
buildenv = envfoo
buildenv = envbar
format = 2
installed = bar-1.2.3-1-any
installed = beh-2.2.3-4-any
options = some_option
options = !other_option
packager = Foobar McFooface <foobar@mcfooface.org>
pkgarch = any
pkgbase = foo
pkgbuild_sha256sum = b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
pkgname = foo
pkgver = 1:1.0.0-1
"#;

    // Test data without multiple values
    const VALID_BUILDINFOV2_CASE2: &str = r#"
builddate = 1
builddir = /build
startdir = /startdir/
buildtool = devtools
buildtoolver = 1:1.2.1-1-any
buildenv = envfoo
format = 2
installed = bar-1.2.3-1-any
options = some_option
packager = Foobar McFooface <foobar@mcfooface.org>
pkgarch = any
pkgbase = foo
pkgbuild_sha256sum = b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
pkgname = foo
pkgver = 1:1.0.0-1
"#;

    #[rstest]
    #[case(VALID_BUILDINFOV2_CASE1)]
    #[case(VALID_BUILDINFOV2_CASE2)]
    fn buildinfov2_from_str(#[case] buildinfo: &str) -> TestResult {
        BuildInfoV2::from_str(buildinfo)?;
        Ok(())
    }

    #[rstest]
    fn buildinfov2() -> TestResult {
        BuildInfoV2::new(
            1,
            BuildDir::from_str("/build")?,
            StartDir::from_str("/startdir/")?,
            BuildTool::from_str("devtools")?,
            BuildToolVersion::from_str("1:1.2.1-1-any")?,
            vec![BuildEnv::new("some")?],
            SchemaVersion::from_str("2")?,
            vec![InstalledPackage::from_str("bar-1:1.0.0-2-any")?],
            vec![PackageOption::new("buildoption")?],
            Packager::from_str("Foobar McFooface <foobar@mcfooface.org>")?,
            Architecture::Any,
            Name::new("foo".to_string())?,
            Checksum::<Sha256>::calculate_from("foo"),
            Name::new("foo".to_string())?,
            Version::from_str("1:1.0.0-1")?,
        )?;
        Ok(())
    }

    #[rstest]
    fn buildinfov2_invalid_schemaversion() -> TestResult {
        assert!(BuildInfoV2::new(
            1,
            BuildDir::from_str("/build")?,
            StartDir::from_str("/startdir/")?,
            BuildTool::from_str("devtools")?,
            BuildToolVersion::from_str("1:1.2.1-1-any")?,
            vec![BuildEnv::new("some")?],
            SchemaVersion::from_str("1")?,
            vec![InstalledPackage::from_str("bar-1:1.0.0-2-any")?],
            vec![PackageOption::new("buildoption")?],
            Packager::from_str("Foobar McFooface <foobar@mcfooface.org>")?,
            Architecture::Any,
            Name::new("foo".to_string())?,
            Checksum::<Sha256>::calculate_from("foo"),
            Name::new("foo".to_string())?,
            Version::from_str("1:1.0.0-1")?,
        )
        .is_err());
        Ok(())
    }

    #[rstest]
    #[case("builddate = 2")]
    #[case("builddir = /build2")]
    #[case("startdir = /startdir2/")]
    #[case("buildtool = devtools2")]
    #[case("buildtoolver = 1:1.2.1-2-any")]
    #[case("format = 1")]
    #[case("packager = Foobar McFooface <foobar@mcfooface.org>")]
    #[case("pkgarch = any")]
    #[case("pkgbase = foo")]
    #[case("pkgbuild_sha256sum = b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c")]
    #[case("pkgname = foo")]
    #[case("pkgver = 1:1.0.0-1")]
    fn buildinfov2_from_str_duplicate_fail(#[case] duplicate: &str) {
        let mut buildinfov2 = VALID_BUILDINFOV2_CASE1.to_string();
        buildinfov2.push_str(duplicate);
        assert!(BuildInfoV2::from_str(&buildinfov2).is_err());
    }
}