diff options
Diffstat (limited to 'crate_universe/src/utils/starlark/glob.rs')
-rw-r--r-- | crate_universe/src/utils/starlark/glob.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/crate_universe/src/utils/starlark/glob.rs b/crate_universe/src/utils/starlark/glob.rs index a7bcebbb..c8bf4bf5 100644 --- a/crate_universe/src/utils/starlark/glob.rs +++ b/crate_universe/src/utils/starlark/glob.rs @@ -6,20 +6,22 @@ use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; use serde::ser::{Serialize, SerializeStruct, Serializer}; #[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone)] -pub struct Glob { - pub include: BTreeSet<String>, - pub exclude: BTreeSet<String>, +pub(crate) struct Glob { + pub(crate) allow_empty: bool, + pub(crate) include: BTreeSet<String>, + pub(crate) exclude: BTreeSet<String>, } impl Glob { - pub fn new_rust_srcs() -> Self { + pub(crate) fn new_rust_srcs() -> Self { Self { + allow_empty: false, include: BTreeSet::from(["**/*.rs".to_owned()]), exclude: BTreeSet::new(), } } - pub fn is_empty(&self) -> bool { + pub(crate) fn has_any_include(&self) -> bool { self.include.is_empty() // Note: self.exclude intentionally not considered. A glob is empty if // there are no included globs. A glob cannot have only excludes. @@ -31,16 +33,17 @@ impl Serialize for Glob { where S: Serializer, { - if self.exclude.is_empty() { - // Serialize as glob([...]). - serializer.serialize_newtype_struct("glob", &self.include) - } else { - // Serialize as glob(include = [...], exclude = [...]). - let mut call = serializer.serialize_struct("glob", 2)?; - call.serialize_field("include", &self.include)?; + let has_exclude = !self.exclude.is_empty(); + let len = 2 + if has_exclude { 1 } else { 0 }; + + // Serialize as glob(allow_empty = False, include = [...], exclude = [...]). + let mut call = serializer.serialize_struct("glob", len)?; + call.serialize_field("allow_empty", &self.allow_empty)?; + call.serialize_field("include", &self.include)?; + if has_exclude { call.serialize_field("exclude", &self.exclude)?; - call.end() } + call.end() } } @@ -68,6 +71,9 @@ impl<'de> Visitor<'de> for GlobVisitor { A: SeqAccess<'de>, { Ok(Glob { + // At time of writing the default value of allow_empty is true. + // We may want to change this if the default changes in Bazel. + allow_empty: true, include: BTreeSet::deserialize(SeqAccessDeserializer::new(seq))?, exclude: BTreeSet::new(), }) @@ -78,14 +84,22 @@ impl<'de> Visitor<'de> for GlobVisitor { where A: MapAccess<'de>, { + fn default_true() -> bool { + true + } + #[derive(serde::Deserialize)] struct GlobMap { + #[serde(default = "default_true")] + allow_empty: bool, include: BTreeSet<String>, + #[serde(default)] exclude: BTreeSet<String>, } let glob_map = GlobMap::deserialize(MapAccessDeserializer::new(map))?; Ok(Glob { + allow_empty: glob_map.allow_empty, include: glob_map.include, exclude: glob_map.exclude, }) |