logsmash/nix/data-mod.nix
2025-08-20 23:53:32 +02:00

46 lines
1.7 KiB
Nix

{
packages,
lib,
writeTextDir,
}: let
inherit (builtins) map head attrNames concatStringsSep replaceStrings;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten last;
module_name = name: version: "${name}_${replaceStrings ["."] ["_"] version}";
matches_for = name: mapAttrsToList (version: data: ''("${name}", "${version}") => Some(("${data.prefix or ""}", ${module_name name version}::STATEMENTS)),'');
imports_for = name: mapAttrsToList (version: _: ''mod ${module_name name version};'');
routes_for = name: mapAttrsToList (version: _: "${module_name name version}::ROUTES");
matches = flatten (mapAttrsToList matches_for packages);
imports = flatten (mapAttrsToList imports_for packages);
routes = flatten (mapAttrsToList routes_for packages);
min_versions = mapAttrsToList (name: versions: ''("${name}", "${head (attrNames versions)}"),'') packages;
max_versions = mapAttrsToList (name: versions: ''("${name}", "${last (attrNames versions)}"),'') packages;
code = ''
use crate::LoggingStatement;
use crate::Route;
${concatStringsSep "\n" imports}
pub const MIN_VERSIONS: &[(&str, &str)] = &[
${concatStringsSep "\n " min_versions}
];
pub const MAX_VERSIONS: &[(&str, &str)] = &[
${concatStringsSep "\n " max_versions}
];
pub fn get_statements_for(name: &str, version: &str) -> Option<(&'static str, &'static [LoggingStatement])> {
match (name, version) {
${concatStringsSep "\n " matches}
_ => None,
}
}
pub fn all_routes() -> impl Iterator<Item = &'static Route> {
[
${concatStringsSep ",\n " routes}
].into_iter().flatten()
}
'';
in
writeTextDir "mod.rs" code