Value

Enum Value 

Source
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

Source

pub fn from_string(s: String) -> Self

Create a new string value, detecting if it’s a numeric string

Source

pub fn from_number(n: f64) -> Self

Create a numeric value

Source

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
Source

pub fn to_number(&self) -> f64

Coerce to numeric value

Source

pub fn to_string_val(&self) -> String

Coerce to string value

Source

pub fn as_str(&self) -> Cow<'_, str>

Get string as Cow to avoid allocation when possible

Source

pub fn to_string_with_format(&self, format: &str) -> String

Coerce to string with specific format (for OFMT/CONVFMT)

Source

pub fn is_numeric(&self) -> bool

Check if this value is definitely numeric

Source

pub fn is_numeric_string(&self) -> bool

Check if this value is a numeric string

Source

pub fn compares_as_number(&self) -> bool

Check if this value should compare as a number

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Value

Source§

fn default() -> Value

Returns the “default value” for a type. Read more
Source§

impl Display for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.