1use crate::error::HkError;
2use indexmap::IndexMap;
3
4pub type HkConfig = IndexMap<String, HkValue>;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum HkValue {
11 String(String),
12 Number(f64),
13 Bool(bool),
14 Array(Vec<HkValue>),
15 Map(IndexMap<String, HkValue>),
16}
17
18impl HkValue {
19 pub fn as_string(&self) -> Result<String, HkError> {
20 match self {
21 Self::String(s) => Ok(s.clone()),
22 Self::Number(n) => Ok(n.to_string()),
23 Self::Bool(b) => Ok(b.to_string()),
24 _ => Err(HkError::TypeMismatch {
25 expected: "string".to_string(),
26 found: format!("{:?}", self),
27 }),
28 }
29 }
30
31 pub fn as_number(&self) -> Result<f64, HkError> {
32 if let Self::Number(n) = self {
33 Ok(*n)
34 } else {
35 Err(HkError::TypeMismatch {
36 expected: "number".to_string(),
37 found: format!("{:?}", self),
38 })
39 }
40 }
41
42 pub fn as_bool(&self) -> Result<bool, HkError> {
43 if let Self::Bool(b) = self {
44 Ok(*b)
45 } else {
46 Err(HkError::TypeMismatch {
47 expected: "bool".to_string(),
48 found: format!("{:?}", self),
49 })
50 }
51 }
52
53 pub fn as_array(&self) -> Result<&Vec<HkValue>, HkError> {
54 if let Self::Array(a) = self {
55 Ok(a)
56 } else {
57 Err(HkError::TypeMismatch {
58 expected: "array".to_string(),
59 found: format!("{:?}", self),
60 })
61 }
62 }
63
64 pub fn as_map(&self) -> Result<&IndexMap<String, HkValue>, HkError> {
65 if let Self::Map(m) = self {
66 Ok(m)
67 } else {
68 Err(HkError::TypeMismatch {
69 expected: "map".to_string(),
70 found: format!("{:?}", self),
71 })
72 }
73 }
74}