]> git.proxmox.com Git - rustc.git/blob - src/tools/build-manifest/src/manifest.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / src / tools / build-manifest / src / manifest.rs
1 use crate::Builder;
2 use serde::{Serialize, Serializer};
3 use std::collections::BTreeMap;
4 use std::path::{Path, PathBuf};
5
6 #[derive(Serialize)]
7 #[serde(rename_all = "kebab-case")]
8 pub(crate) struct Manifest {
9 pub(crate) manifest_version: String,
10 pub(crate) date: String,
11 pub(crate) pkg: BTreeMap<String, Package>,
12 pub(crate) renames: BTreeMap<String, Rename>,
13 pub(crate) profiles: BTreeMap<String, Vec<String>>,
14 }
15
16 #[derive(Serialize)]
17 pub(crate) struct Package {
18 pub(crate) version: String,
19 pub(crate) git_commit_hash: Option<String>,
20 pub(crate) target: BTreeMap<String, Target>,
21 }
22
23 #[derive(Serialize)]
24 pub(crate) struct Rename {
25 pub(crate) to: String,
26 }
27
28 #[derive(Serialize, Default)]
29 pub(crate) struct Target {
30 pub(crate) available: bool,
31 pub(crate) url: Option<String>,
32 pub(crate) hash: Option<FileHash>,
33 pub(crate) xz_url: Option<String>,
34 pub(crate) xz_hash: Option<FileHash>,
35 pub(crate) components: Option<Vec<Component>>,
36 pub(crate) extensions: Option<Vec<Component>>,
37 }
38
39 impl Target {
40 pub(crate) fn from_compressed_tar(builder: &Builder, base_path: &str) -> Self {
41 let base_path = builder.input.join(base_path);
42 let gz = Self::tarball_variant(&base_path, "gz");
43 let xz = Self::tarball_variant(&base_path, "xz");
44
45 if gz.is_none() {
46 return Self::unavailable();
47 }
48
49 Self {
50 available: true,
51 components: None,
52 extensions: None,
53 // .gz
54 url: gz.as_ref().map(|path| builder.url(path)),
55 hash: gz.map(FileHash::Missing),
56 // .xz
57 xz_url: xz.as_ref().map(|path| builder.url(path)),
58 xz_hash: xz.map(FileHash::Missing),
59 }
60 }
61
62 fn tarball_variant(base: &Path, ext: &str) -> Option<PathBuf> {
63 let mut path = base.to_path_buf();
64 path.set_extension(ext);
65 if path.is_file() { Some(path) } else { None }
66 }
67
68 pub(crate) fn unavailable() -> Self {
69 Self::default()
70 }
71 }
72
73 #[derive(Serialize)]
74 pub(crate) struct Component {
75 pub(crate) pkg: String,
76 pub(crate) target: String,
77 }
78
79 impl Component {
80 pub(crate) fn from_str(pkg: &str, target: &str) -> Self {
81 Self { pkg: pkg.to_string(), target: target.to_string() }
82 }
83 }
84
85 #[allow(unused)]
86 pub(crate) enum FileHash {
87 Missing(PathBuf),
88 Present(String),
89 }
90
91 impl Serialize for FileHash {
92 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
93 match self {
94 FileHash::Missing(path) => Err(serde::ser::Error::custom(format!(
95 "can't serialize a missing hash for file {}",
96 path.display()
97 ))),
98 FileHash::Present(inner) => inner.serialize(serializer),
99 }
100 }
101 }
102
103 pub(crate) fn visit_file_hashes(manifest: &mut Manifest, mut f: impl FnMut(&mut FileHash)) {
104 for pkg in manifest.pkg.values_mut() {
105 for target in pkg.target.values_mut() {
106 if let Some(hash) = &mut target.hash {
107 f(hash);
108 }
109 if let Some(hash) = &mut target.xz_hash {
110 f(hash);
111 }
112 }
113 }
114 }