use allocative::Allocative; use starlark::values::StarlarkValue; use starlark_derive::{NoSerialize, ProvidesStaticType, starlark_value}; #[derive(Debug, Clone, Allocative, ProvidesStaticType, NoSerialize)] pub struct TarballSource { url: String, sha256: String, strip_components: u32, } impl std::fmt::Display for TarballSource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "tarball_source") } } starlark::starlark_simple_value!(TarballSource); #[starlark_value(type = "tarball_source")] impl<'v> StarlarkValue<'v> for TarballSource {} impl TarballSource { pub fn new(url: String, sha256: String, strip_components: u32) -> Self { Self { url, sha256, strip_components, } } pub fn url(&self) -> &str { &self.url } pub fn sha256(&self) -> &str { &self.sha256 } pub fn strip_components(&self) -> u32 { self.strip_components } } #[derive(Debug, Clone, Allocative, ProvidesStaticType, NoSerialize)] pub struct GitSource { url: String, commit: String, } impl std::fmt::Display for GitSource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "git_source") } } starlark::starlark_simple_value!(GitSource); #[starlark_value(type = "git_source")] impl<'v> StarlarkValue<'v> for GitSource {} impl GitSource { pub fn new(url: String, commit: String) -> Self { Self { url, commit } } pub fn url(&self) -> &str { &self.url } pub fn commit(&self) -> &str { &self.commit } } #[derive(Debug, Clone, Allocative, ProvidesStaticType, NoSerialize)] pub enum Source { Tarball(TarballSource), Git(GitSource), } impl std::fmt::Display for Source { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "source") } } starlark::starlark_simple_value!(Source); #[starlark_value(type = "source")] impl<'v> StarlarkValue<'v> for Source {} impl Source { pub fn url(&self) -> &str { match self { Self::Tarball(source) => source.url(), Self::Git(source) => source.url(), } } pub fn cache_key(&self) -> &str { match self { Self::Tarball(source) => source.sha256(), Self::Git(source) => source.commit(), } } pub fn is_unknown_cache_key(&self) -> bool { matches!(self.cache_key(), "?" | "???") } }