wolfram_wxf 0.7.0

Convert a value to the Wolfram Language WXF format.
Documentation
#![deny(rustdoc::missing_crate_level_docs)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![doc = include_str!("../Readme.md")]
#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/11549616")]
#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/11549616")]

use num::BigInt;
use ordered_float::OrderedFloat;
use std::collections::BTreeMap;

mod associations;
mod error;
mod extensions;
mod functions;
mod numbers;
mod packed_array;
mod traits;
mod utils;

pub use crate::{
    associations::{WolframPair, WolframRule},
    error::{Result, WolframError},
    extensions::*,
    functions::{WolframFunction, WolframSymbol},
    packed_array::WolframArray,
    traits::{object_builder::WolframSerializer, readable_writer::ReadableWriter},
    utils::*,
};

/// The `WolframLib` struct is the main entry point for the library.
pub trait ToWolfram {
    /// Converts the value to a [`WolframValue`] value.
    fn to_wolfram(&self) -> WolframValue;
    /// Converts the value to a [`WolframValue`] in string form.
    fn to_wolfram_string(&self) -> String {
        self.to_wolfram().to_string()
    }
    /// Converts the value to a [`WolframValue`] in bytes form.
    fn to_wolfram_bytes(&self) -> Vec<u8> {
        self.to_wolfram().to_bytes()
    }
    /// Converts the value to a [`WolframValue`] in compressed bytes form.
    fn to_wolfram_solid(&self) -> Vec<u8> {
        self.to_wolfram().to_compressed()
    }
}

/// A [`WolframValue`] is a value that can be converted to a [`WolframValue`]
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum WolframValue {
    /// A Empty value, roughly equivalent to `Nothing` in Wolfram Language
    Skip,
    /// A wolfram function, notice the head of the function can be any [`WolframValue`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use wolfram_wxf::WolframValue;
    /// WolframValue::function("Plus", vec![1, 2, 3]);
    /// ```
    Function(Box<WolframFunction>),
    /// A wolfram boolean, notice that `True` and `False` are symbols in Wolfram Language
    ///
    /// ```
    /// # use wolfram_wxf::WolframValue;
    /// WolframValue::function("Plus", vec![1, 2, 3]);
    /// ```
    Boolean(bool),
    /// A wolfram string
    ///
    /// ```
    /// # use wolfram_wxf::WolframValue;
    /// WolframValue::function("Plus", vec![1, 2, 3]);
    /// ```
    String(String),
    /// A wolfram bytes
    ///
    /// ```
    /// # use wolfram_wxf::WolframValue;
    /// WolframValue::function("Plus", vec![1, 2, 3]);
    /// ```
    Bytes(Vec<u8>),
    /// A wolfram symbol
    ///
    /// ```
    /// # use wolfram_wxf::WolframValue;
    /// WolframValue::function("Plus", vec![1, 2, 3]);
    /// ```
    Symbol(Box<WolframSymbol>),
    /// A wolfram number in range `[-128, 127]`
    Integer8(i8),
    /// A wolfram number in range `[-32768, 32767]`
    Integer16(i16),
    /// A wolfram number in range `[-2147483648, 2147483647]`
    Integer32(i32),
    /// A wolfram number in range `[-9223372036854775808, 9223372036854775807]`
    Integer64(i64),
    /// A wolfram integer in arbitrary precision
    BigInteger(BigInt),
    /// Do not use `f64`, because partial order cannot be defined
    Decimal64(OrderedFloat<f64>),
    /// A wolfram decimal in arbitrary precision
    BigDecimal(String),
    /// Need to optimize
    PackedArray(WolframArray),
    /// Need to optimize
    NumericArray(WolframArray),
    /// Record with key, rule, value
    Association(BTreeMap<WolframValue, (WolframRule, WolframValue)>),
}

impl Default for WolframValue {
    fn default() -> Self {
        Self::Skip
    }
}