aboutsummaryrefslogtreecommitdiff
path: root/crate_universe/src/utils/starlark/select.rs
blob: 43c57582d0fa30a9fceea887525ffd3cf49e3f9b (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
use std::collections::BTreeSet;
use std::iter::FromIterator;

use serde::ser::Serializer;
use serde::Serialize;
use serde_starlark::LineComment;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct WithOriginalConfigurations<T> {
    pub value: T,
    pub original_configurations: BTreeSet<String>,
}

#[derive(Serialize)]
#[serde(rename = "selects.NO_MATCHING_PLATFORM_TRIPLES")]
pub struct NoMatchingPlatformTriples;

impl<T> Serialize for WithOriginalConfigurations<T>
where
    T: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let comment =
            Vec::from_iter(self.original_configurations.iter().map(String::as_str)).join(", ");
        LineComment::new(&self.value, &comment).serialize(serializer)
    }
}

// We allow users to specify labels as keys to selects, but we need to identify when this is happening
// because we also allow things like "x86_64-unknown-linux-gnu" as keys, and these technically parse as labels
// (that parses as "//x86_64-unknown-linux-gnu:x86_64-unknown-linux-gnu").
//
// We don't expect any cfg-expressions or target triples to contain //,
// and all labels _can_ be written in a way that they contain //,
// so we use the presence of // as an indication something is a label.
pub fn looks_like_bazel_configuration_label(configuration: &str) -> bool {
    configuration.contains("//")
}