aboutsummaryrefslogtreecommitdiff
path: root/crate_universe/src/utils/starlark.rs
blob: f1045478b90f1fcfc025353071a5c76b8283081d (plain)
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
//! A module for representations of starlark constructs

mod glob;
mod label;
mod select;
mod select_dict;
mod select_list;
mod select_scalar;
mod select_set;
mod serialize;
mod target_compatible_with;

use std::collections::BTreeSet as Set;

use serde::{Serialize, Serializer};
use serde_starlark::{Error as StarlarkError, FunctionCall};

pub use glob::*;
pub use label::*;
pub use select::*;
pub use select_dict::*;
pub use select_list::*;
pub use select_scalar::*;
pub use select_set::*;
pub use target_compatible_with::*;

#[derive(Serialize)]
#[serde(untagged)]
pub enum Starlark {
    Load(Load),
    Package(Package),
    PackageInfo(PackageInfo),
    License(License),
    ExportsFiles(ExportsFiles),
    Filegroup(Filegroup),
    Alias(Alias),
    CargoBuildScript(CargoBuildScript),
    #[serde(serialize_with = "serialize::rust_proc_macro")]
    RustProcMacro(RustProcMacro),
    #[serde(serialize_with = "serialize::rust_library")]
    RustLibrary(RustLibrary),
    #[serde(serialize_with = "serialize::rust_binary")]
    RustBinary(RustBinary),

    #[serde(skip_serializing)]
    Verbatim(String),
}

pub struct Load {
    pub bzl: String,
    pub items: Set<String>,
}

pub struct Package {
    pub default_package_metadata: Set<Label>,
    pub default_visibility: Set<String>,
}

pub struct PackageInfo {
    pub name: String,
    pub package_name: String,
    pub package_url: String,
    pub package_version: String,
}

pub struct License {
    pub name: String,
    pub license_kinds: Set<String>,
    pub license_text: String,
}

pub struct ExportsFiles {
    pub paths: Set<String>,
    pub globs: Glob,
}

#[derive(Serialize)]
#[serde(rename = "filegroup")]
pub struct Filegroup {
    pub name: String,
    pub srcs: Glob,
}

pub struct Alias {
    pub rule: String,
    pub name: String,
    pub actual: Label,
    pub tags: Set<String>,
}

#[derive(Serialize)]
#[serde(rename = "cargo_build_script")]
pub struct CargoBuildScript {
    pub name: String,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub aliases: SelectDict<Label, String>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub build_script_env: SelectDict<String, String>,
    #[serde(skip_serializing_if = "Data::is_empty")]
    pub compile_data: Data,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub crate_features: SelectSet<String>,
    pub crate_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crate_root: Option<String>,
    #[serde(skip_serializing_if = "Data::is_empty")]
    pub data: Data,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub link_deps: SelectSet<Label>,
    pub edition: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub linker_script: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub links: Option<String>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub proc_macro_deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectScalar::is_empty")]
    pub rundir: SelectScalar<String>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub rustc_env: SelectDict<String, String>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub rustc_env_files: SelectSet<String>,
    #[serde(skip_serializing_if = "SelectList::is_empty")]
    pub rustc_flags: SelectList<String>,
    pub srcs: Glob,
    #[serde(skip_serializing_if = "Set::is_empty")]
    pub tags: Set<String>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub tools: SelectSet<Label>,
    #[serde(skip_serializing_if = "Set::is_empty")]
    pub toolchains: Set<Label>,
    pub version: String,
    pub visibility: Set<String>,
}

#[derive(Serialize)]
pub struct RustProcMacro {
    pub name: String,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub proc_macro_deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub aliases: SelectDict<Label, String>,
    #[serde(flatten)]
    pub common: CommonAttrs,
}

#[derive(Serialize)]
pub struct RustLibrary {
    pub name: String,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub proc_macro_deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub aliases: SelectDict<Label, String>,
    #[serde(flatten)]
    pub common: CommonAttrs,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub disable_pipelining: bool,
}

#[derive(Serialize)]
pub struct RustBinary {
    pub name: String,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub proc_macro_deps: SelectSet<Label>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub aliases: SelectDict<Label, String>,
    #[serde(flatten)]
    pub common: CommonAttrs,
}

#[derive(Serialize)]
pub struct CommonAttrs {
    #[serde(skip_serializing_if = "Data::is_empty")]
    pub compile_data: Data,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub crate_features: SelectSet<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crate_root: Option<String>,
    #[serde(skip_serializing_if = "Data::is_empty")]
    pub data: Data,
    pub edition: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub linker_script: Option<String>,
    #[serde(skip_serializing_if = "SelectDict::is_empty")]
    pub rustc_env: SelectDict<String, String>,
    #[serde(skip_serializing_if = "SelectSet::is_empty")]
    pub rustc_env_files: SelectSet<String>,
    #[serde(skip_serializing_if = "SelectList::is_empty")]
    pub rustc_flags: SelectList<String>,
    pub srcs: Glob,
    #[serde(skip_serializing_if = "Set::is_empty")]
    pub tags: Set<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_compatible_with: Option<TargetCompatibleWith>,
    pub version: String,
}

pub struct Data {
    pub glob: Glob,
    pub select: SelectSet<Label>,
}

impl Package {
    pub fn default_visibility_public(default_package_metadata: Set<Label>) -> Self {
        let mut default_visibility = Set::new();
        default_visibility.insert("//visibility:public".to_owned());
        Package {
            default_package_metadata,
            default_visibility,
        }
    }
}

impl Serialize for Alias {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Output looks like:
        //
        //     rule(
        //         name = "name",
        //         actual = "actual",
        //         tags = [
        //            "tag1",
        //            "tag2",
        //         ],
        //     )

        #[derive(Serialize)]
        struct AliasInner<'a> {
            pub name: &'a String,
            pub actual: &'a Label,
            pub tags: &'a Set<String>,
        }

        FunctionCall::new(
            &self.rule,
            AliasInner {
                name: &self.name,
                actual: &self.actual,
                tags: &self.tags,
            },
        )
        .serialize(serializer)
    }
}

pub fn serialize(starlark: &[Starlark]) -> Result<String, StarlarkError> {
    let mut content = String::new();
    for call in starlark {
        if !content.is_empty() {
            content.push('\n');
        }
        if let Starlark::Verbatim(comment) = call {
            content.push_str(comment);
        } else {
            content.push_str(&serde_starlark::to_string(call)?);
        }
    }
    Ok(content)
}