///|
/// HCL value types with decoration
///
/// ```mbt check
/// test {
/// let v = HCLValue::int(42)
/// assert_true(v.is_number())
/// assert_eq(v.as_int().unwrap(), 42)
/// }
/// ```
pub(all) enum HCLValue {
/// Null value with decoration
Null(Decor)
/// Boolean value with decoration
Bool(Bool, Decor)
/// Number value with decoration (int, int64, or float)
Number(Number, Decor)
/// String value with decoration
String(String, Decor)
/// Array of values with decoration
Array(Array[HCLValue], Decor)
/// Object (ordered map of string to value) with decoration
Object(HCLObject, Decor)
/// Unknown value (Terraform plan-stage) with decoration
Unknown(Decor)
} derive(Debug, Eq)
///|
/// Get decoration from HCLValue
///
/// ```mbt check
/// test {
/// let v = HCLValue::string("x")
/// assert_eq(v.get_decor(), Decor::empty())
/// }
/// ```
pub fn HCLValue::get_decor(self : HCLValue) -> Decor {
match self {
Null(decor) => decor
Bool(_, decor) => decor
Number(_, decor) => decor
String(_, decor) => decor
Array(_, decor) => decor
Object(_, decor) => decor
Unknown(decor) => decor
}
}
///|
/// Set decoration on HCLValue
///
/// ```mbt check
/// test {
/// let v = HCLValue::int(1).set_decor(Decor::new(Some(" "), None))
/// assert_eq(v.get_decor().get_prefix(), Some(" "))
/// }
/// ```
pub fn HCLValue::set_decor(self : HCLValue, decor : Decor) -> HCLValue {
match self {
Null(_) => Null(decor)
Bool(b, _) => Bool(b, decor)
Number(n, _) => Number(n, decor)
String(s, _) => String(s, decor)
Array(a, _) => Array(a, decor)
Object(o, _) => Object(o, decor)
Unknown(_) => Unknown(decor)
}
}
///|
/// Check if value is null
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::null().is_null(), true)
/// assert_eq(HCLValue::int(1).is_null(), false)
/// }
/// ```
pub fn HCLValue::is_null(self : HCLValue) -> Bool {
match self {
Null(_) => true
_ => false
}
}
///|
/// Check if value is a boolean
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::bool(true).is_bool(), true)
/// assert_eq(HCLValue::int(1).is_bool(), false)
/// }
/// ```
pub fn HCLValue::is_bool(self : HCLValue) -> Bool {
match self {
Bool(_, _) => true
_ => false
}
}
///|
/// Check if value is a number
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(1).is_number(), true)
/// assert_eq(HCLValue::string("x").is_number(), false)
/// }
/// ```
pub fn HCLValue::is_number(self : HCLValue) -> Bool {
match self {
Number(_, _) => true
_ => false
}
}
///|
/// Check if value is a string
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::string("x").is_string(), true)
/// }
/// ```
pub fn HCLValue::is_string(self : HCLValue) -> Bool {
match self {
String(_, _) => true
_ => false
}
}
///|
/// Check if value is an array
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::array([hcl_int(1)]).is_array(), true)
/// }
/// ```
pub fn HCLValue::is_array(self : HCLValue) -> Bool {
match self {
Array(_, _) => true
_ => false
}
}
///|
/// Check if value is an object
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::object({ "a": hcl_int(1) }).is_object(), true)
/// }
/// ```
pub fn HCLValue::is_object(self : HCLValue) -> Bool {
match self {
Object(_, _) => true
_ => false
}
}
///|
/// Check if value is unknown (Terraform plan-stage)
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::unknown().is_unknown(), true)
/// assert_eq(HCLValue::int(1).is_unknown(), false)
/// }
/// ```
pub fn HCLValue::is_unknown(self : HCLValue) -> Bool {
match self {
Unknown(_) => true
_ => false
}
}
///|
/// Get type name as string
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(1).type_name(), "number")
/// assert_eq(HCLValue::string("x").type_name(), "string")
/// }
/// ```
pub fn HCLValue::type_name(self : HCLValue) -> String {
match self {
Null(_) => "null"
Bool(_, _) => "bool"
Number(_, _) => "number"
String(_, _) => "string"
Array(_, _) => "array"
Object(_, _) => "object"
Unknown(_) => "unknown"
}
}
///|
/// Get the boolean value, or None if not a bool
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::bool(true).as_bool().unwrap(), true)
/// assert_eq(HCLValue::int(1).as_bool(), None)
/// }
/// ```
pub fn HCLValue::as_bool(self : HCLValue) -> Bool? {
match self {
Bool(b, _) => Some(b)
_ => None
}
}
///|
/// Get the Number value, or None if not a number
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(1).as_number().unwrap().as_int().unwrap(), 1)
/// match HCLValue::bool(true).as_number() {
/// None => ()
/// Some(_) => panic()
/// }
/// }
/// ```
pub fn HCLValue::as_number(self : HCLValue) -> Number? {
match self {
Number(n, _) => Some(n)
_ => None
}
}
///|
/// Get the int value, or None if not a number representable as Int
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(42).as_int().unwrap(), 42)
/// assert_eq(HCLValue::string("x").as_int(), None)
/// }
/// ```
pub fn HCLValue::as_int(self : HCLValue) -> Int? {
match self {
Number(n, _) => n.as_int()
_ => None
}
}
///|
/// Get the int64 value, or None if not a number representable as Int64
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int64(7L).as_int64().unwrap(), 7L)
/// }
/// ```
pub fn HCLValue::as_int64(self : HCLValue) -> Int64? {
match self {
Number(n, _) => n.as_int64()
_ => None
}
}
///|
/// Get the float value, or None if not a number
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::float(1.5).as_float().unwrap(), 1.5)
/// }
/// ```
pub fn HCLValue::as_float(self : HCLValue) -> Double? {
match self {
Number(n, _) => Some(n.as_float())
_ => None
}
}
///|
/// Get the string value, or None if not a string
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::string("hi").as_string().unwrap(), "hi")
/// assert_eq(HCLValue::int(1).as_string(), None)
/// }
/// ```
pub fn HCLValue::as_string(self : HCLValue) -> String? {
match self {
String(s, _) => Some(s)
_ => None
}
}
///|
/// Get the array value, or None if not an array
///
/// ```mbt check
/// test {
/// let a = HCLValue::array([hcl_int(1), hcl_int(2)])
/// assert_eq(a.as_array().unwrap().length(), 2)
/// }
/// ```
pub fn HCLValue::as_array(self : HCLValue) -> Array[HCLValue]? {
match self {
Array(a, _) => Some(a)
_ => None
}
}
///|
/// Get the object value, or None if not an object
///
/// ```mbt check
/// test {
/// let o = HCLValue::object({ "a": hcl_int(1) })
/// assert_eq(o.as_object().unwrap()["a"].as_int().unwrap(), 1)
/// }
/// ```
pub fn HCLValue::as_object(self : HCLValue) -> HCLObject? {
match self {
Object(o, _) => Some(o)
_ => None
}
}
///|
/// Create null value with empty decor
///
/// ```mbt check
/// test {
/// assert_true(HCLValue::null().is_null())
/// }
/// ```
pub fn HCLValue::null() -> HCLValue {
Null(Decor::empty())
}
///|
/// Create bool value with empty decor
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::bool(false).as_bool().unwrap(), false)
/// }
/// ```
pub fn HCLValue::bool(b : Bool) -> HCLValue {
Bool(b, Decor::empty())
}
///|
/// Create int value with empty decor
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(7).as_int().unwrap(), 7)
/// }
/// ```
pub fn HCLValue::int(i : Int) -> HCLValue {
Number(Number::from_int(i), Decor::empty())
}
///|
/// Create int64 value with empty decor
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int64(7L).as_int64().unwrap(), 7L)
/// }
/// ```
pub fn HCLValue::int64(i : Int64) -> HCLValue {
Number(Number::from_int64(i), Decor::empty())
}
///|
/// Create float value with empty decor
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::float(2.5).as_float().unwrap(), 2.5)
/// }
/// ```
pub fn HCLValue::float(f : Double) -> HCLValue {
Number(Number::from_finite_f64(f), Decor::empty())
}
///|
/// Create string value with empty decor
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::string("hi").as_string().unwrap(), "hi")
/// }
/// ```
pub fn HCLValue::string(s : String) -> HCLValue {
String(s, Decor::empty())
}
///|
/// Create array value with empty decor
///
/// ```mbt check
/// test {
/// let a = HCLValue::array([hcl_int(1)])
/// assert_eq(a.as_array().unwrap().length(), 1)
/// }
/// ```
pub fn HCLValue::array(arr : Array[HCLValue]) -> HCLValue {
Array(arr, Decor::empty())
}
///|
/// Create object value with empty decor
///
/// ```mbt check
/// test {
/// let o = HCLValue::object({ "k": hcl_str("v") })
/// assert_eq(o.as_object().unwrap()["k"].as_string().unwrap(), "v")
/// }
/// ```
pub fn HCLValue::object(obj : HCLObject) -> HCLValue {
Object(obj, Decor::empty())
}
///|
/// Create unknown value with empty decor (Terraform plan-stage)
///
/// ```mbt check
/// test {
/// assert_true(HCLValue::unknown().is_unknown())
/// }
/// ```
pub fn HCLValue::unknown() -> HCLValue {
Unknown(Decor::empty())
}
///|
/// Convert HCLValue to JSON-compatible string representation
///
/// ```mbt check
/// test {
/// assert_eq(HCLValue::int(42).to_json_string(), "42")
/// assert_eq(HCLValue::string("hi").to_json_string(), "\"hi\"")
/// assert_eq(HCLValue::bool(true).to_json_string(), "true")
/// }
/// ```
pub fn HCLValue::to_json_string(self : HCLValue) -> String {
match self {
Null(_) => "null"
Unknown(_) => "null"
Bool(b, _) => if b { "true" } else { "false" }
Number(n, _) => n.to_string()
String(s, _) => escape_string(s)
Array(arr, _) =>
if arr.is_empty() {
"[]"
} else {
let items = arr.map(fn(v) { v.to_json_string() })
"[" + items.join(", ") + "]"
}
Object(obj, _) =>
if obj.is_empty() {
"{}"
} else {
let items = []
for k, v in obj {
items.push(escape_string(k) + ": " + v.to_json_string())
}
"{" + items.join(", ") + "}"
}
}
}
///|
/// Escape a string for JSON
fn escape_string(s : String) -> String {
let mut result = "\""
for c in s {
match c {
'"' => result = result + "\\\""
'\\' => result = result + "\\\\"
'\n' => result = result + "\\n"
'\r' => result = result + "\\r"
'\t' => result = result + "\\t"
'\b' => result = result + "\\b"
'\f' => result = result + "\\f"
_ => result = result + c.to_string()
}
}
result + "\""
}