use crate::val::Val;
use hashbrown::HashMap;
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use std::fmt;
use std::sync::Arc;
struct ValVisitor;
impl<'de> Visitor<'de> for ValVisitor {
type Value = Val;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON value of any type")
}
fn visit_bool<E>(self, value: bool) -> Result<Val, E> {
Ok(Val::Bool(value))
}
fn visit_i64<E>(self, value: i64) -> Result<Val, E> {
Ok(Val::Int(value))
}
fn visit_u64<E>(self, value: u64) -> Result<Val, E> {
if value <= i64::MAX as u64 {
Ok(Val::Int(value as i64))
} else {
Ok(Val::Float(value as f64))
}
}
fn visit_f64<E>(self, value: f64) -> Result<Val, E> {
Ok(Val::Float(value))
}
fn visit_str<E>(self, value: &str) -> Result<Val, E> {
Ok(Val::Str(Arc::from(value)))
}
fn visit_string<E>(self, value: String) -> Result<Val, E> {
Ok(Val::from(value))
}
fn visit_none<E>(self) -> Result<Val, E> {
Ok(Val::Nil)
}
fn visit_unit<E>(self) -> Result<Val, E> {
Ok(Val::Nil)
}
fn visit_seq<A>(self, mut seq: A) -> Result<Val, A::Error>
where
A: SeqAccess<'de>,
{
let size_hint = seq.size_hint().unwrap_or(0);
let mut elements = Vec::with_capacity(size_hint);
while let Some(elem) = seq.next_element::<Val>()? {
elements.push(elem);
}
Ok(Val::List(Arc::new(elements)))
}
fn visit_map<M>(self, mut map_access: M) -> Result<Val, M::Error>
where
M: MapAccess<'de>,
{
let size_hint = map_access.size_hint().unwrap_or(0);
let mut map = HashMap::with_capacity(size_hint);
while let Some((key, value)) = map_access.next_entry::<String, Val>()? {
map.insert(key, value);
}
Ok(Val::Map(Arc::new(map)))
}
}
impl<'de> Deserialize<'de> for Val {
fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(ValVisitor)
}
}
#[cfg(feature = "json")]
pub fn from_json_str(input: &str) -> crate::error::Result<Val> {
serde_json::from_str::<Val>(input).map_err(|e| crate::error::Error::Deserialize(e.to_string()))
}
#[cfg(feature = "yaml")]
pub fn from_yaml_str(input: &str) -> crate::error::Result<Val> {
serde_yaml::from_str::<Val>(input).map_err(|e| crate::error::Error::Deserialize(e.to_string()))
}
#[cfg(feature = "toml")]
pub fn from_toml_str(input: &str) -> crate::error::Result<Val> {
toml::from_str::<Val>(input).map_err(|e| crate::error::Error::Deserialize(e.to_string()))
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Format {
#[cfg(feature = "json")]
Json,
#[cfg(feature = "yaml")]
Yaml,
#[cfg(feature = "toml")]
Toml,
}
pub const fn default_format() -> Format {
#[cfg(feature = "json")]
{
Format::Json
}
#[cfg(all(feature = "yaml", not(feature = "json")))]
{
Format::Yaml
}
#[cfg(all(feature = "toml", not(feature = "json"), not(feature = "yaml")))]
{
Format::Toml
}
}
fn detect_format_confident(input: &str) -> Option<Format> {
let trimmed = input.trim();
if trimmed.is_empty() {
return Some(default_format());
}
#[cfg(feature = "json")]
if (trimmed.starts_with('{') && trimmed.ends_with('}')) || (trimmed.starts_with('[') && trimmed.ends_with(']')) {
return Some(Format::Json);
}
#[cfg(feature = "yaml")]
if has_yaml_document_markers(trimmed) || has_yaml_indicators(trimmed) {
return Some(Format::Yaml);
}
#[cfg(feature = "toml")]
if has_toml_indicators(trimmed) {
return Some(Format::Toml);
}
#[cfg(feature = "json")]
if looks_like_json_scalar(trimmed) {
return Some(Format::Json);
}
None
}
pub fn detect_format(input: &str) -> Format {
detect_format_confident(input).unwrap_or_else(default_format)
}
pub fn parse_auto(input: &str) -> crate::error::Result<(Format, Val)> {
let format = detect_format_confident(input).ok_or_else(|| {
crate::error::Error::Deserialize(
"Auto-detect requires explicit format selection or recognizable JSON/YAML/TOML markers".to_string(),
)
})?;
let value = match format {
#[cfg(feature = "json")]
Format::Json => from_json_str(input),
#[cfg(feature = "yaml")]
Format::Yaml => from_yaml_str(input),
#[cfg(feature = "toml")]
Format::Toml => from_toml_str(input),
}?;
Ok((format, value))
}
#[cfg(feature = "yaml")]
fn has_yaml_document_markers(input: &str) -> bool {
input.lines().any(|line| matches!(line.trim(), "---" | "..."))
}
#[cfg(feature = "yaml")]
pub fn has_yaml_indicators(input: &str) -> bool {
for line in input.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if trimmed.contains(':') && !trimmed.starts_with('"') && !trimmed.starts_with('{') {
if let Some(colon_pos) = trimmed.find(':') {
let key_part = &trimmed[..colon_pos];
let value_part = &trimmed[colon_pos + 1..];
if !key_part.starts_with('"')
&& !key_part.starts_with('\'')
&& (value_part.is_empty() || value_part.starts_with(' ') || value_part.starts_with('\t'))
{
return true;
}
}
}
if trimmed.starts_with("- ") || trimmed.starts_with("-\t") {
return true;
}
if trimmed.ends_with("|") || trimmed.ends_with(">") {
return true;
}
}
false
}
#[cfg(feature = "toml")]
pub fn has_toml_indicators(input: &str) -> bool {
for line in input.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if trimmed.starts_with('[') && trimmed.ends_with(']') && trimmed.len() > 2 {
return true;
}
if trimmed.starts_with("[[") && trimmed.ends_with("]]") && trimmed.len() > 4 {
return true;
}
if trimmed.contains(" = ") || trimmed.contains("=") {
if let Some(eq_pos) = trimmed.find('=') {
let key_part = trimmed[..eq_pos].trim();
let value_part = trimmed[eq_pos + 1..].trim();
if !key_part.is_empty() && !value_part.is_empty() {
if key_part
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '.')
|| (key_part.starts_with('"') && key_part.ends_with('"'))
|| (key_part.starts_with('\'') && key_part.ends_with('\''))
{
return true;
}
}
}
}
}
false
}
#[cfg(feature = "json")]
fn looks_like_json_scalar(input: &str) -> bool {
matches!(input, "null" | "true" | "false")
|| (input.starts_with('"') && input.ends_with('"'))
|| looks_like_json_number(input)
}
#[cfg(feature = "json")]
fn looks_like_json_number(input: &str) -> bool {
let bytes = input.as_bytes();
if bytes.is_empty() {
return false;
}
let mut i = 0usize;
if bytes[i] == b'-' {
i += 1;
if i == bytes.len() {
return false;
}
}
match bytes[i] {
b'0' => {
i += 1;
}
b'1'..=b'9' => {
i += 1;
while i < bytes.len() && bytes[i].is_ascii_digit() {
i += 1;
}
}
_ => return false,
}
if i < bytes.len() && bytes[i] == b'.' {
i += 1;
if i == bytes.len() || !bytes[i].is_ascii_digit() {
return false;
}
while i < bytes.len() && bytes[i].is_ascii_digit() {
i += 1;
}
}
if i < bytes.len() && matches!(bytes[i], b'e' | b'E') {
i += 1;
if i < bytes.len() && matches!(bytes[i], b'+' | b'-') {
i += 1;
}
if i == bytes.len() || !bytes[i].is_ascii_digit() {
return false;
}
while i < bytes.len() && bytes[i].is_ascii_digit() {
i += 1;
}
}
i == bytes.len()
}
pub fn parse_with_format(input: &str, format_override: Option<Format>) -> crate::error::Result<Val> {
if let Some(format) = format_override {
return match format {
#[cfg(feature = "json")]
Format::Json => from_json_str(input),
#[cfg(feature = "yaml")]
Format::Yaml => from_yaml_str(input),
#[cfg(feature = "toml")]
Format::Toml => from_toml_str(input),
};
}
#[cfg(feature = "json")]
{
from_json_str(input)
}
#[cfg(all(feature = "yaml", not(feature = "json")))]
{
from_yaml_str(input)
}
#[cfg(all(feature = "toml", not(feature = "json"), not(feature = "yaml")))]
{
from_toml_str(input)
}
}