use std::collections::BTreeSet;
use crate::{QuotedString, ToBazelDefinition};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Rule {
RustLibrary,
RustProcMacro,
RustTest,
RustDocTest,
RustBinary,
CargoBuildScript,
}
impl Rule {
pub fn module(&self) -> Module {
match self {
Rule::RustLibrary
| Rule::RustProcMacro
| Rule::RustTest
| Rule::RustDocTest
| Rule::RustBinary => Module::Rust,
Rule::CargoBuildScript => Module::Cargo,
}
}
}
impl ToBazelDefinition for Rule {
fn format(&self, writer: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> {
let s = match self {
Rule::RustLibrary => "rust_library",
Rule::RustProcMacro => "rust_proc_macro",
Rule::RustTest => "rust_test",
Rule::RustDocTest => "rust_doc_test",
Rule::RustBinary => "rust_binary",
Rule::CargoBuildScript => "cargo_build_script",
};
let s = QuotedString::new(s);
s.format(writer)
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Module {
Rust,
Cargo,
}
impl ToBazelDefinition for Module {
fn format(&self, writer: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> {
let s = match self {
Module::Rust => "rust",
Module::Cargo => "cargo",
};
write!(writer, "{s}")
}
}
#[derive(Debug)]
pub struct LoadStatement {
module: Module,
rules: BTreeSet<Rule>,
}
impl ToBazelDefinition for LoadStatement {
fn format(&self, writer: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> {
write!(
writer,
"load(\"@rules_rust//{}:defs.bzl\"",
self.module.to_bazel_definition()
)?;
for rule in &self.rules {
write!(writer, ", {}", rule.to_bazel_definition())?;
}
write!(writer, ")")?;
Ok(())
}
}
impl From<(Module, Vec<Rule>)> for LoadStatement {
fn from(value: (Module, Vec<Rule>)) -> Self {
let rules = value.1.into_iter().collect();
LoadStatement {
module: value.0,
rules,
}
}
}