1 use std::collections::BTreeSet;
2 use std::iter::FromIterator;
3
4 use serde::ser::Serializer;
5 use serde::Serialize;
6 use serde_starlark::LineComment;
7
8 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
9 pub(crate) struct WithOriginalConfigurations<T> {
10 pub(crate) value: T,
11 pub(crate) original_configurations: BTreeSet<String>,
12 }
13
14 #[derive(Serialize)]
15 #[serde(rename = "selects.NO_MATCHING_PLATFORM_TRIPLES")]
16 pub(crate) struct NoMatchingPlatformTriples;
17
18 impl<T> Serialize for WithOriginalConfigurations<T>
19 where
20 T: Serialize,
21 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,22 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
23 where
24 S: Serializer,
25 {
26 let comment =
27 Vec::from_iter(self.original_configurations.iter().map(String::as_str)).join(", ");
28 LineComment::new(&self.value, &comment).serialize(serializer)
29 }
30 }
31
32 // We allow users to specify labels as keys to selects, but we need to identify when this is happening
33 // because we also allow things like "x86_64-unknown-linux-gnu" as keys, and these technically parse as labels
34 // (that parses as "//x86_64-unknown-linux-gnu:x86_64-unknown-linux-gnu").
35 //
36 // We don't expect any cfg-expressions or target triples to contain //,
37 // and all labels _can_ be written in a way that they contain //,
38 // so we use the presence of // as an indication something is a label.
looks_like_bazel_configuration_label(configuration: &str) -> bool39 pub(crate) fn looks_like_bazel_configuration_label(configuration: &str) -> bool {
40 configuration.contains("//")
41 }
42