use std::collections::BTreeMap;
use crate::error::SourceLocation;
#[derive(Debug, Clone, PartialEq, Default)]
pub enum Value {
#[default]
Null,
Bool(bool),
Integer(i64),
Float(f64),
String(String),
Array(Vec<Value>),
Table(BTreeMap<String, Value>),
}
impl Value {
pub fn is_null(&self) -> bool {
matches!(self, Value::Null)
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Bool(b) => Some(*b),
_ => None,
}
}
pub fn as_integer(&self) -> Option<i64> {
match self {
Value::Integer(i) => Some(*i),
_ => None,
}
}
pub fn as_float(&self) -> Option<f64> {
match self {
Value::Float(f) => Some(*f),
Value::Integer(i) => Some(*i as f64),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Value]> {
match self {
Value::Array(arr) => Some(arr),
_ => None,
}
}
pub fn as_table(&self) -> Option<&BTreeMap<String, Value>> {
match self {
Value::Table(table) => Some(table),
_ => None,
}
}
pub fn get_path(&self, path: &str) -> Option<&Value> {
let parts: Vec<&str> = path.split('.').collect();
self.get_path_parts(&parts)
}
fn get_path_parts(&self, parts: &[&str]) -> Option<&Value> {
if parts.is_empty() {
return Some(self);
}
match self {
Value::Table(table) => table
.get(parts[0])
.and_then(|v| v.get_path_parts(&parts[1..])),
_ => None,
}
}
pub fn type_name(&self) -> &'static str {
match self {
Value::Null => "null",
Value::Bool(_) => "boolean",
Value::Integer(_) => "integer",
Value::Float(_) => "float",
Value::String(_) => "string",
Value::Array(_) => "array",
Value::Table(_) => "table",
}
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
Value::Bool(b)
}
}
impl From<i64> for Value {
fn from(i: i64) -> Self {
Value::Integer(i)
}
}
impl From<i32> for Value {
fn from(i: i32) -> Self {
Value::Integer(i64::from(i))
}
}
impl From<f64> for Value {
fn from(f: f64) -> Self {
Value::Float(f)
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::String(s)
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::String(s.to_string())
}
}
impl<T: Into<Value>> From<Vec<T>> for Value {
fn from(v: Vec<T>) -> Self {
Value::Array(v.into_iter().map(Into::into).collect())
}
}
impl<T: Into<Value>> From<BTreeMap<String, T>> for Value {
fn from(m: BTreeMap<String, T>) -> Self {
Value::Table(m.into_iter().map(|(k, v)| (k, v.into())).collect())
}
}
#[derive(Debug, Clone)]
pub struct ConfigValue {
pub value: Value,
pub source: SourceLocation,
}
impl ConfigValue {
pub fn new(value: impl Into<Value>, source: SourceLocation) -> Self {
Self {
value: value.into(),
source,
}
}
pub fn anonymous(value: impl Into<Value>) -> Self {
Self {
value: value.into(),
source: SourceLocation::new("unknown"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_type_checks() {
assert!(Value::Null.is_null());
assert!(!Value::Bool(true).is_null());
assert_eq!(Value::Bool(true).as_bool(), Some(true));
assert_eq!(Value::Integer(42).as_integer(), Some(42));
assert_eq!(Value::Float(2.71).as_float(), Some(2.71));
assert_eq!(Value::Integer(42).as_float(), Some(42.0));
assert_eq!(Value::String("hello".to_string()).as_str(), Some("hello"));
}
#[test]
fn test_value_get_path() {
let mut inner = BTreeMap::new();
inner.insert("host".to_string(), Value::String("localhost".to_string()));
inner.insert("port".to_string(), Value::Integer(5432));
let mut root = BTreeMap::new();
root.insert("database".to_string(), Value::Table(inner));
let value = Value::Table(root);
assert_eq!(
value.get_path("database.host").and_then(|v| v.as_str()),
Some("localhost")
);
assert_eq!(
value.get_path("database.port").and_then(|v| v.as_integer()),
Some(5432)
);
assert!(value.get_path("database.password").is_none());
assert!(value.get_path("other").is_none());
}
#[test]
fn test_value_type_name() {
assert_eq!(Value::Null.type_name(), "null");
assert_eq!(Value::Bool(true).type_name(), "boolean");
assert_eq!(Value::Integer(42).type_name(), "integer");
assert_eq!(Value::Float(2.71).type_name(), "float");
assert_eq!(Value::String("test".to_string()).type_name(), "string");
assert_eq!(Value::Array(vec![]).type_name(), "array");
assert_eq!(Value::Table(BTreeMap::new()).type_name(), "table");
}
#[test]
fn test_value_from_conversions() {
let _: Value = true.into();
let _: Value = 42i64.into();
let _: Value = 42i32.into();
let _: Value = 2.71f64.into();
let _: Value = "hello".into();
let _: Value = String::from("hello").into();
let _: Value = vec![1i64, 2, 3].into();
}
#[test]
fn test_config_value() {
let cv = ConfigValue::new("localhost", SourceLocation::new("config.toml").with_line(5));
assert_eq!(cv.value.as_str(), Some("localhost"));
assert_eq!(cv.source.source, "config.toml");
assert_eq!(cv.source.line, Some(5));
}
}