use std::{fmt, fs, path::Path};
use serde::{
Deserialize, Deserializer,
de::{MapAccess, SeqAccess, Visitor},
};
use crate::error::TaskError;
#[derive(Debug)]
pub(super) enum JsonNode {
Null,
Bool(bool),
Signed(i64),
Unsigned(u64),
Float(f64),
String(String),
Array(Vec<JsonNode>),
Object(Vec<(String, JsonNode)>),
}
impl JsonNode {
pub(super) fn display_value(&self) -> String {
match self {
Self::Null => "null".to_string(),
Self::Bool(value) => value.to_string(),
Self::Signed(value) => value.to_string(),
Self::Unsigned(value) => value.to_string(),
Self::Float(value) => value.to_string(),
Self::String(value) => value.clone(),
Self::Array(_) => "array".to_string(),
Self::Object(_) => "object".to_string(),
}
}
}
pub(super) fn read_json(path: &Path) -> Result<JsonNode, TaskError> {
let content = fs::read_to_string(path).map_err(|source| TaskError::ConfigIo {
path: path.to_path_buf(),
operation: "read",
source,
})?;
let mut deserializer = serde_json::Deserializer::from_str(&content);
let node =
JsonNode::deserialize(&mut deserializer).map_err(|error| TaskError::ConfigInvalidJson {
path: path.to_path_buf(),
line: error.line(),
column: error.column(),
message: error.to_string(),
})?;
deserializer
.end()
.map_err(|error| TaskError::ConfigInvalidJson {
path: path.to_path_buf(),
line: error.line(),
column: error.column(),
message: error.to_string(),
})?;
Ok(node)
}
impl<'de> Deserialize<'de> for JsonNode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(JsonNodeVisitor)
}
}
struct JsonNodeVisitor;
impl<'de> Visitor<'de> for JsonNodeVisitor {
type Value = JsonNode;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("any JSON value")
}
fn visit_unit<E>(self) -> Result<Self::Value, E> {
Ok(JsonNode::Null)
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E> {
Ok(JsonNode::Bool(value))
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> {
Ok(JsonNode::Signed(value))
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
Ok(JsonNode::Unsigned(value))
}
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E> {
Ok(JsonNode::Float(value))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(JsonNode::String(value.to_string()))
}
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> {
Ok(JsonNode::String(value))
}
fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut values = Vec::new();
while let Some(value) = sequence.next_element()? {
values.push(value);
}
Ok(JsonNode::Array(values))
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut values = Vec::new();
while let Some((key, value)) = map.next_entry()? {
values.push((key, value));
}
Ok(JsonNode::Object(values))
}
}