use allocative::Allocative; use starlark::{ environment::GlobalsBuilder, starlark_module, starlark_simple_value, values::StarlarkValue, }; use starlark_derive::{NoSerialize, ProvidesStaticType, starlark_value}; use crate::recipe::Source; #[derive(Debug, Clone, Allocative, NoSerialize, ProvidesStaticType)] 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") } } starlark_simple_value!(TarballSource); #[starlark_value(type = "tarball")] impl<'v> StarlarkValue<'v> for TarballSource {} impl Source for TarballSource {} #[derive(Debug, Clone, Allocative, NoSerialize, ProvidesStaticType)] pub struct Metadata { maintainer: Option, description: Option, license: Option, website: Option, } impl std::fmt::Display for Metadata { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "metadata") } } starlark_simple_value!(Metadata); #[starlark_value(type = "metadata")] impl<'v> StarlarkValue<'v> for Metadata {} #[starlark_module] pub fn recipe_globals(b: &mut GlobalsBuilder) { fn tarball( #[starlark(require = named)] url: &str, #[starlark(require = named)] sha256: &str, #[starlark(require = named, default = 0)] strip_components: u32, ) -> anyhow::Result { Ok(TarballSource { url: url.to_string(), sha256: sha256.to_string(), strip_components, }) } fn meta( #[starlark(require = named)] maintainer: Option<&str>, #[starlark(require = named)] description: Option<&str>, #[starlark(require = named)] license: Option<&str>, #[starlark(require = named)] website: Option<&str>, ) -> anyhow::Result { Ok(Metadata { maintainer: maintainer.map(|x| x.to_string()), description: description.map(|x| x.to_string()), license: license.map(|x| x.to_string()), website: website.map(|x| x.to_string()), }) } }