#starlark #serialization #build-system #serde #bazel #buck #generate

serde_starlark

Serde serializer for generating Starlark build targets

17 releases

0.1.16 Jan 2, 2024
0.1.15 Dec 20, 2023
0.1.14 Jul 15, 2023
0.1.12 Apr 23, 2023
0.0.0 Dec 23, 2022

#1495 in Encoding

Download history 6810/week @ 2024-01-01 8367/week @ 2024-01-08 8872/week @ 2024-01-15 10094/week @ 2024-01-22 11137/week @ 2024-01-29 12292/week @ 2024-02-05 13886/week @ 2024-02-12 12937/week @ 2024-02-19 11396/week @ 2024-02-26 11280/week @ 2024-03-04 10240/week @ 2024-03-11 12379/week @ 2024-03-18 13791/week @ 2024-03-25 15243/week @ 2024-04-01 11557/week @ 2024-04-08 11388/week @ 2024-04-15

52,505 downloads per month
Used in 2 crates

MIT/Apache

66KB
1.5K SLoC

serde_starlark

github crates.io docs.rs build status

Serde serializer for generating syntactically valid Starlark, the declarative format used for describing build targets in build systems including Bazel, Buck, Pants, and Please.


Example

The following example serializes a minimal Bazel target for the syn crate.

The tests/bazel.rs test in this repo has a somewhat more fleshed out example of this use case, including things like load(), package(default_visibility =), distinct include and exclude arguments to glob(), and select({}).

#[derive(Serialize)]
#[serde(rename = "rust_library")]
pub struct RustLibrary {
    pub name: String,
    pub srcs: Glob,
    pub crate_features: BTreeSet<String>,
    pub edition: u16,
    pub deps: BTreeSet<String>,
}

#[derive(Serialize)]
#[serde(rename = "glob")]
pub struct Glob(pub BTreeSet<String>);

fn main() {
    let rust_library = RustLibrary { ... };

    print!("{}", serde_starlark::to_string(&rust_library).unwrap());
}
rust_library(
    name = "syn",
    srcs = glob(["**/*.rs"]),
    crate_features = [
        "default",
        "full",
    ],
    edition = 2018,
    deps = [
        ":proc-macro2",
        ":quote",
        ":unicode-ident",
    ],
)

Data model

The primitive types (integers, boolean, string) serialize in the obvious way to Starlark.

Serde sequences serialize to Starlark arrays. Serde maps serialize to Starlark maps.

Rust structs with named fields serialize to Starlark "function calls" with named arguments:

#[derive(Serialize)]
#[serde(rename = "rust_library")]
pub struct RustLibrary {
    pub name: String,
    pub edition: u16,
}
rust_library(
    name = "syn",
    edition = 2018,
)

Rust newtype structs and tuple structs serialize to Starlark "function calls" with positional arguments:

#[derive(Serialize)]
#[serde(rename = "select")]
pub struct Select<T>(pub BTreeMap<String, T>);
select({
    "//conditions:default": [],
})

To make a newtype struct which does not appear as a function call, use the serde(transparent) attribute.

#[derive(Serialize)]
#[serde(transparent)]
pub struct Dependency(pub String);

Fields of type Option<T> serialize as either None or the value if present. Consider using serde(skip_serializing_if = "Option::is_none") to omit fields with value None from the serialized output.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~110–360KB