use std::collections::HashMap;
use crate::types::{DataSize, Duration, IntSeq, PklRegex};
#[derive(Debug, Clone, PartialEq)]
pub enum ObjectMember {
Property { name: String, value: PklValue },
Entry { key: PklValue, value: PklValue },
Element { index: usize, value: PklValue },
}
#[derive(Debug, Clone, PartialEq)]
pub enum PklValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
Object {
class_name: String,
module_uri: String,
members: Vec<ObjectMember>,
},
Map(Vec<(PklValue, PklValue)>),
List(Vec<PklValue>),
Set(Vec<PklValue>),
Duration(Duration),
DataSize(DataSize),
Pair(Box<PklValue>, Box<PklValue>),
IntSeq(IntSeq),
Regex(PklRegex),
Class {
class_name: String,
module_uri: String,
},
TypeAlias {
name: String,
module_uri: String,
},
Function,
Bytes(Vec<u8>),
}
impl PklValue {
pub fn as_properties(&self) -> Option<HashMap<&str, &PklValue>> {
match self {
PklValue::Object { members, .. } => {
let mut map = HashMap::new();
for member in members {
if let ObjectMember::Property { name, value } = member {
map.insert(name.as_str(), value);
}
}
Some(map)
}
_ => None,
}
}
pub fn is_null(&self) -> bool {
matches!(self, PklValue::Null)
}
pub fn as_bool(&self) -> Option<bool> {
match self {
PklValue::Bool(b) => Some(*b),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match self {
PklValue::Int(i) => Some(*i),
_ => None,
}
}
pub fn as_f64(&self) -> Option<f64> {
match self {
PklValue::Float(f) => Some(*f),
PklValue::Int(i) => Some(*i as f64),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
PklValue::String(s) => Some(s),
_ => None,
}
}
}