pub enum Value {
Uninitialized,
Number(f64),
String(String),
NumericString(String, f64),
}Expand description
AWK value type with dynamic typing and automatic coercion
AWK has a unique type system where values can be strings, numbers, or “numeric strings” (strings that look like numbers). This enum captures all three cases.
§Examples
use awk_rs::Value;
// Numbers
let num = Value::Number(42.0);
assert_eq!(num.to_number(), 42.0);
assert_eq!(num.to_string_val(), "42");
// Strings
let s = Value::from_string("hello".to_string());
assert_eq!(s.to_string_val(), "hello");
assert_eq!(s.to_number(), 0.0); // Non-numeric string coerces to 0
// Numeric strings
let ns = Value::from_string("123".to_string());
assert_eq!(ns.to_number(), 123.0);
assert_eq!(ns.to_string_val(), "123");
// Truthiness
assert!(Value::Number(1.0).is_truthy());
assert!(!Value::Number(0.0).is_truthy());
assert!(Value::from_string("hello".to_string()).is_truthy());
assert!(!Value::from_string("".to_string()).is_truthy());Variants§
Uninitialized
Uninitialized value - coerces to “” or 0 depending on context
Number(f64)
Numeric value
String(String)
String value
NumericString(String, f64)
Numeric string - a string that looks like a number (used for comparison semantics)
Implementations§
Source§impl Value
impl Value
Sourcepub fn from_string(s: String) -> Self
pub fn from_string(s: String) -> Self
Create a new string value, detecting if it’s a numeric string
Sourcepub fn from_number(n: f64) -> Self
pub fn from_number(n: f64) -> Self
Create a numeric value
Sourcepub fn is_truthy(&self) -> bool
pub fn is_truthy(&self) -> bool
Check if this value is “true” in boolean context
- Uninitialized is false
- Number 0 is false
- Empty string is false
- Everything else is true
Sourcepub fn to_string_val(&self) -> String
pub fn to_string_val(&self) -> String
Coerce to string value
Sourcepub fn to_string_with_format(&self, format: &str) -> String
pub fn to_string_with_format(&self, format: &str) -> String
Coerce to string with specific format (for OFMT/CONVFMT)
Sourcepub fn is_numeric(&self) -> bool
pub fn is_numeric(&self) -> bool
Check if this value is definitely numeric
Sourcepub fn is_numeric_string(&self) -> bool
pub fn is_numeric_string(&self) -> bool
Check if this value is a numeric string
Sourcepub fn compares_as_number(&self) -> bool
pub fn compares_as_number(&self) -> bool
Check if this value should compare as a number
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more