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