18个版本

0.7.0 2024年1月5日
0.6.6 2023年5月8日
0.6.3 2022年3月27日
0.6.0 2021年5月9日
0.3.1 2020年3月23日

#5 in #格式转换

每月21次下载
用于 8 个crate(5个直接使用)

MPL-2.0 许可证

220KB
9K SLoC

使用Rust的Wolfram交换格式

设计

要支持格式转换,只需实现ToWolfram特性。

pub trait ToWolfram {
    fn to_wolfram(&self) -> WolframValue;
    fn to_wolfram_string(&self) -> String {self.to_wolfram().to_string()}
    fn to_wolfram_bytes(&self) -> Vec<u8> {self.to_wolfram().to_bytes()}
    fn to_wolfram_solid(&self) -> Vec<u8> {self.to_wolfram().to_compressed()}
}

WolframValue由以下合法元素组成

pub enum WolframValue {
    /// Function with name, args
    Boolean(bool),
    Function(Box<str>, Vec<WolframValue>),
    String(Box<str>),
    Bytes(Vec<u8>),
    Symbol(Box<str>),
    Integer8(i8),
    Integer16(i16),
    Integer32(i32),
    Integer64(i64),
    BigInteger(BigInt),
    /// Do not use `f64`, because partial order cannot be defined
    Decimal64([u8; 8]),
    BigDecimal(Box<str>),
    /// Need to optimize
    PackedArray(Vec<WolframValue>),
    /// Need to optimize
    NumericArray(Vec<WolframValue>),
    /// Record with key, rule, value
    Association(BTreeMap<WolframValue, (WolframValue, WolframValue)>),
    Rule,
    RuleDelayed,
}

扩展

以下是现在支持的常见格式

pub enum SupportedFormat {
    JSON, //json
    TOML, //toml
    YAML, //yaml, yml
    Pickle, //pkl
}

由于Rust不能同时在外部定义特性和结构体,支持新格式需要在项目中完成。

请参阅From Traits Extension了解如何支持新格式

工具

  • wex:一个可以将wxf格式进行转换的命令行工具

依赖项

~2–3MB
~61K SLoC