use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::path::PathBuf;
use serde::de::{self, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
use super::{
AssetTarget, Assets, AttachPoints, CommandField, DriverSpec, Generated, PackageSpec,
Prerelease, Publish,
};
struct CommandFieldVisitor;
impl<'de> Visitor<'de> for CommandFieldVisitor {
type Value = CommandField;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a mise task name string or a command argv sequence")
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(CommandField::Mise(v.to_owned()))
}
fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
Ok(CommandField::Mise(v))
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut argv = Vec::new();
while let Some(arg) = seq.next_element::<String>()? {
argv.push(arg);
}
Ok(CommandField::Argv(argv))
}
}
impl<'de> Deserialize<'de> for CommandField {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(CommandFieldVisitor)
}
}
fn assign_once<T, E: de::Error>(
slot: &mut Option<T>,
field: &'static str,
value: T,
) -> Result<(), E> {
if slot.is_some() {
return Err(E::duplicate_field(field));
}
*slot = Some(value);
Ok(())
}
struct PackageSpecVisitor;
impl<'de> Visitor<'de> for PackageSpecVisitor {
type Value = PackageSpec;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a package mapping")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let mut name: Option<String> = None;
let mut path: Option<PathBuf> = None;
let mut driver: Option<Option<String>> = None;
let mut publisher: Option<Option<String>> = None;
let mut noun: Option<Option<String>> = None;
let mut publish: Option<Option<Publish>> = None;
let mut registry: Option<Option<String>> = None;
let mut query: Option<Option<Vec<String>>> = None;
let mut changelog: Option<Option<PathBuf>> = None;
let mut first_minor: Option<bool> = None;
let mut prerelease: Option<Option<Prerelease>> = None;
let mut marker: Option<bool> = None;
let mut owners: Option<Option<Vec<String>>> = None;
let mut trusted_publishing_only: Option<Option<bool>> = None;
let mut ecosystem: Option<Option<String>> = None;
let mut lockfile: Option<Option<String>> = None;
let mut at: Option<AttachPoints> = None;
let mut format: Option<Option<String>> = None;
let mut keys: Option<Option<Vec<String>>> = None;
let mut read: Option<Option<CommandField>> = None;
let mut write: Option<Option<CommandField>> = None;
let mut after: Option<Option<String>> = None;
let mut unknown = BTreeSet::new();
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"name" => assign_once(&mut name, "name", map.next_value()?)?,
"path" => assign_once(&mut path, "path", map.next_value()?)?,
"driver" => assign_once(&mut driver, "driver", map.next_value()?)?,
"publisher" => assign_once(&mut publisher, "publisher", map.next_value()?)?,
"noun" => assign_once(&mut noun, "noun", map.next_value()?)?,
"publish" => assign_once(&mut publish, "publish", map.next_value()?)?,
"registry" => assign_once(&mut registry, "registry", map.next_value()?)?,
"query" => assign_once(&mut query, "query", map.next_value()?)?,
"changelog" => assign_once(&mut changelog, "changelog", map.next_value()?)?,
"first_minor" => assign_once(&mut first_minor, "first_minor", map.next_value()?)?,
"prerelease" => assign_once(&mut prerelease, "prerelease", map.next_value()?)?,
"marker" => assign_once(&mut marker, "marker", map.next_value()?)?,
"owners" => assign_once(&mut owners, "owners", map.next_value()?)?,
"trusted_publishing_only" => assign_once(
&mut trusted_publishing_only,
"trusted_publishing_only",
map.next_value()?,
)?,
"ecosystem" => assign_once(&mut ecosystem, "ecosystem", map.next_value()?)?,
"lockfile" => assign_once(&mut lockfile, "lockfile", map.next_value()?)?,
"at" => assign_once(&mut at, "at", map.next_value()?)?,
"format" => assign_once(&mut format, "format", map.next_value()?)?,
"keys" => assign_once(&mut keys, "keys", map.next_value()?)?,
"read" => assign_once(&mut read, "read", map.next_value()?)?,
"write" => assign_once(&mut write, "write", map.next_value()?)?,
"after" => assign_once(&mut after, "after", map.next_value()?)?,
_ => {
let _: IgnoredAny = map.next_value()?;
unknown.insert(key);
}
}
}
Ok(PackageSpec {
name: name.ok_or_else(|| de::Error::missing_field("name"))?,
path: path.ok_or_else(|| de::Error::missing_field("path"))?,
driver: driver.flatten(),
publisher: publisher.flatten(),
noun: noun.flatten(),
publish: publish.flatten(),
registry: registry.flatten(),
query: query.flatten(),
changelog: changelog.flatten(),
first_minor: first_minor.unwrap_or(false),
prerelease: prerelease.flatten(),
marker: marker.unwrap_or(false),
owners: owners.flatten(),
trusted_publishing_only: trusted_publishing_only.flatten(),
ecosystem: ecosystem.flatten(),
lockfile: lockfile.flatten(),
at: at.unwrap_or_default(),
spec: DriverSpec {
format: format.flatten(),
keys: keys.flatten(),
read: read.flatten(),
write: write.flatten(),
after: after.flatten(),
},
unknown,
})
}
}
impl<'de> Deserialize<'de> for PackageSpec {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(PackageSpecVisitor)
}
}
struct AssetsVisitor;
impl<'de> Visitor<'de> for AssetsVisitor {
type Value = Assets;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("an assets mapping")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let mut bin: Option<Option<String>> = None;
let mut prepare: Option<Option<Vec<String>>> = None;
let mut build: Option<Option<Vec<String>>> = None;
let mut binary: Option<Option<String>> = None;
let mut dir: Option<Option<PathBuf>> = None;
let mut files: Option<Vec<PathBuf>> = None;
let mut generated: Option<Vec<Generated>> = None;
let mut targets = BTreeMap::new();
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"bin" => assign_once(&mut bin, "bin", map.next_value()?)?,
"prepare" => assign_once(&mut prepare, "prepare", map.next_value()?)?,
"build" => assign_once(&mut build, "build", map.next_value()?)?,
"binary" => assign_once(&mut binary, "binary", map.next_value()?)?,
"dir" => assign_once(&mut dir, "dir", map.next_value()?)?,
"files" => assign_once(&mut files, "files", map.next_value()?)?,
"generated" => assign_once(&mut generated, "generated", map.next_value()?)?,
_ => {
let target: AssetTarget = map.next_value()?;
if targets.insert(key.clone(), target).is_some() {
return Err(de::Error::custom(format!("duplicate asset target {key:?}")));
}
}
}
}
Ok(Assets {
bin: bin.flatten(),
prepare: prepare.flatten(),
build: build.flatten(),
binary: binary.flatten(),
dir: dir.flatten(),
files: files.unwrap_or_default(),
generated: generated.unwrap_or_default(),
targets,
})
}
}
impl<'de> Deserialize<'de> for Assets {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(AssetsVisitor)
}
}