Greatly simplify parsing using a specialized serde deserializer

..for the "service" field
This commit is contained in:
Armaël Guéneau 2024-11-05 16:47:00 +01:00
parent 53c83b8a3b
commit 5aa6746d4b
2 changed files with 51 additions and 120 deletions

View file

@ -4,7 +4,7 @@ use std::io::BufReader;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::ExitCode; use std::process::ExitCode;
use anyhow::{anyhow, Result}; use anyhow::Result;
mod parsing; mod parsing;
use parsing::*; use parsing::*;
@ -73,8 +73,8 @@ impl Ctx {
pub fn add_file(&mut self, file: &Path) -> anyhow::Result<()> { pub fn add_file(&mut self, file: &Path) -> anyhow::Result<()> {
let cfg: Config = { let cfg: Config = {
let file = File::open(file)?; let file = File::open(file)?;
let body: hcl::Body = hcl::from_reader(BufReader::new(file))?; let config: Config = hcl::from_reader(BufReader::new(file))?;
Config::from_body(&body).map_err(|s| anyhow!("{}", s))? config
}; };
for (jobname, job) in cfg.jobs { for (jobname, job) in cfg.jobs {

View file

@ -1,5 +1,10 @@
use serde::Deserialize; use serde::de::{
use hcl::{Structure, Body, Map, Expression}; value::{MapAccessDeserializer, SeqAccessDeserializer},
MapAccess, SeqAccess, Visitor,
};
use serde::{Deserialize, Deserializer};
use std::fmt;
use std::marker::PhantomData;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Config { pub struct Config {
@ -10,142 +15,68 @@ pub struct Config {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Job { pub struct Job {
pub datacenters: Vec<String>, pub datacenters: Vec<String>,
#[serde(rename = "group")] #[serde(rename = "group", default = "hcl::Map::new")]
pub groups: hcl::Map<String, Group>, pub groups: hcl::Map<String, Group>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Group { pub struct Group {
#[serde(rename = "task")] #[serde(rename = "task", default = "hcl::Map::new")]
pub tasks: hcl::Map<String, Task>, pub tasks: hcl::Map<String, Task>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Task { pub struct Task {
#[serde(rename = "service")] #[serde(rename = "service", deserialize_with = "vec_or_map", default = "Vec::new")]
pub services: Vec<Service>, pub services: Vec<Service>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Service { pub struct Service {
pub name: Option<String>, pub name: Option<String>,
#[serde(default = "Vec::new")]
pub tags: Vec<String>, pub tags: Vec<String>,
} }
// lots of manual boilerplate because I can't figure out how to use the // In .hcl files, a field can sometimes container either a record (for a single
// automated deserialization for blocks without labels (e.g. "service") that may // item), or a sequence of records (for several items). This is not something
// appear one or several times: // that the built-in hcl deserializer handles. Instead, the following
// https://github.com/martinohmann/hcl-rs/issues/380 // deserializer can be attached to fields that follow this schema with the
// "deserialize_with" serde attribute.
impl Config { //
pub fn from_body(b: &Body) -> Result<Self, String> { // Thanks to the hcl-rs maintener for the code snippet
Ok(Config { jobs: blocks_from_body(b, "job", Job::from_body)? }) // (https://github.com/martinohmann/hcl-rs/issues/380#issuecomment-2456546232)
} fn vec_or_map<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
} where
T: Deserialize<'de>,
impl Job { D: Deserializer<'de>,
fn from_body(b: &Body) -> Result<Self, String> {
let datacenters =
strings_from_expr(&attribute_from_body(b, "datacenters")?)?;
let groups = blocks_from_body(b, "group", Group::from_body)?;
Ok(Job { datacenters, groups })
}
}
impl Group {
fn from_body(b: &Body) -> Result<Self, String> {
Ok(Group { tasks: blocks_from_body(b, "task", Task::from_body)? })
}
}
impl Task {
fn from_body(b: &Body) -> Result<Self, String> {
Ok(Task { services: blocks_nolabel_from_body(b, "service", Service::from_body)? })
}
}
impl Service {
fn from_body(b: &Body) -> Result<Self, String> {
let name =
attribute_from_body(b, "name").ok()
.map(|e| string_from_expr(&e)).transpose()?;
let tags = match attribute_from_body(b, "tags") {
Err(_) => Vec::new(),
Ok(tags) => tags_from_expr(&tags)?,
};
Ok(Service { name, tags })
}
}
fn blocks_from_body<T, F>(b: &Body, id: &str, f: F) -> Result<Map<String, T>, String>
where F: Fn(&Body) -> Result<T, String>
{ {
let mut blocks = Map::new(); struct VecOrMap<T>(PhantomData<fn() -> T>);
for s in &b.0 {
if let Structure::Block(block) = s { impl<'de, T> Visitor<'de> for VecOrMap<T>
if block.identifier.as_str() == id { where
if block.labels.len() != 1 { T: Deserialize<'de>,
return Err(format!("{} block: unexpected number of labels", id)) {
} type Value = Vec<T>;
blocks.insert(block.labels[0].as_str().to_string(), f(&block.body)?);
} fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
} formatter.write_str("sequence or map")
}
Ok(blocks)
} }
fn blocks_nolabel_from_body<T, F>(b: &Body, id: &str, f: F) -> Result<Vec<T>, String> fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
where F: Fn(&Body) -> Result<T, String> where
A: SeqAccess<'de>,
{ {
let mut blocks = Vec::new(); Deserialize::deserialize(SeqAccessDeserializer::new(seq))
for s in &b.0 {
if let Structure::Block(block) = s {
if block.identifier.as_str() == id {
blocks.push(f(&block.body)?)
}
}
}
Ok(blocks)
} }
fn attribute_from_body(b: &Body, name: &str) -> Result<Expression, String> fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{ {
b.0.iter().find_map(|s| { Deserialize::deserialize(MapAccessDeserializer::new(map)).map(|value| vec![value])
if let Structure::Attribute(attr) = s {
if attr.key.as_str() == name {
return Some(attr.expr.clone())
}
}
return None
}).ok_or(format!("attribute {} not found", name))
}
fn string_from_expr(e: &Expression) -> Result<String, String>
{
match e {
Expression::String(s) => Ok(s.to_string()),
_ => Err(format!("string expected, got {:?}", &e)),
} }
} }
fn tags_from_expr(e: &Expression) -> Result<Vec<String>, String> deserializer.deserialize_any(VecOrMap(PhantomData))
{
match e {
Expression::Array(es) => {
Ok(es.into_iter().filter_map(|e| match string_from_expr(e) {
Ok(e) => Some(e),
Err(_) => { println!("note: ignoring tag {:?}", &e); None },
}).collect())
},
_ => Err(format!("array expected, got {:#?}", &e))
}
}
fn strings_from_expr(e: &Expression) -> Result<Vec<String>, String>
{
match e {
Expression::Array(es) =>
es.into_iter().map(string_from_expr).collect(),
_ => Err(format!("array expected, got {:#?}", &e))
}
} }