A Rust framework for building context-sensitive type conversion.

Related tags

Miscellaneous xylem
Overview

xylem

GitHub actions crates.io crates.io docs.rs GitHub GitHub

Xylem is a stateful type conversion framework for Rust.

Concepts

Xylem provides the [Xylem] trait, which is similar to the [std::convert::TryFrom] trait, but with the following differences:

Stateful context

[Xylem::convert] passes a mutable [Context], enabling stateful operations throughout the conversion process. See [Context] documentation for details.

Fixed concersion source

Unlike [std::convert::TryFrom], [Xylem] takes the conversion source type as an associated type [Xylem::From] instead of a generic parameter. This means each type can only be converted from exactly one other specific type under a given [Schema].

Schemas

[Xylem] accepts a type parameter S ("schema"), which acts as a namespace defining the set of conversion rules. This allows different downstream crates to define their own conversion rules without conflicting with each other. For example, if crate foo wants to convert bool from String and crate bar wants to convert bool from i32, they can separately define schema types foo::Xylem and bar::Xylem, then separately implement

impl Xylem 
   for 
   bool {
    
   type 
   From = 
   String;
    
   // fn convert() omitted
}

   impl 
   Xylem
    
    for 
    bool {
    
    type 
    From = 
    i32;
    
    // fn convert() omitted
}
   
  

Furthermore, since foo::Schema and bar::Schema are declared in their own crates, Xylem is not considered a foreign trait, so implementing custom conversion rules for [std] types will not result in error E0220 or error E0119.

To use the default conversion rules defined by xylem, make the schema implement the [SchemaExt] trait. There is a convenience macro [declare_schema] for this:

xylem::declare_schema!(Schema: xylem::SchemaExt);

// we defined a schema type called `Schema`.

It is recommended to use Schema as the schema name and declare it at the crate level, because the [Xylem][xylem_codegen::Xylem] macro uses crate::Schema as the default schema type.

The Xylem macro

Xylem provides a [Xylem][xylem_codegen::Xylem] macro, which derives the corresponding [Xylem::From] type from a struct or enum by replacing each type with the corresponding [Xylem::From] type, as well as a [Xylem] implementation. See the [Xylem][xylem_codegen::Xylem] documentation for details.

Note that the order of fields matters because xylem type conversion is stateful, i.e. previous conversions may affect subsequent ones.

The id feature

With the id feature enabled, xylem provides the [Id] type, which is the motivational use case for xylem: Deserialize a config file that references other fields by string ID, replace each declaring ID with an integer storing its occurrence order, and replace each referencing ID with the occurrence order of the declaring ID.

The [Id] type takes two generic parameters, S and X. The type S is just the schema type, while the type X is the subject of identification. X must also implement the [Identifiable] trait, which has an associated type [Identifiable::Scope] used to provide a namespace for the ID. The declaring [Id] field must be declared under X, and X must occur as a (transitive) child of the scope. Further references to the ID of X must occur also as transitive children of the scope, because the scope is dropped when it completes parsing.

Declaring IDs are marked with the argument new = true. If the ID is to be cross-referenced after the scope drops, also mark track = true. Referencing IDs do not need to be marked, but if they serve to import a scope, they should be marked as import = true.

See tests/id.rs and tests/cross_id.rs for example usage.

Note that it is not a design goal for xylem to support lookahead IDs. Due to the stateful nature of xylem, IDs are only indexed when the declaration has been scanned. There is currently no plan to implement multiple passes to pre-index IDs.

You might also like...
Cassette A simple, single-future, non-blocking executor intended for building state machines.

Cassette A simple, single-future, non-blocking executor intended for building state machines. Designed to be no-std and embedded friendly. This execut

An aimless attempt at building a PC from scratch, in a vaguely eurorack/modular synth style.
An aimless attempt at building a PC from scratch, in a vaguely eurorack/modular synth style.

An aimless attempt at building a PC from scratch, in a vaguely eurorack/modular synth style.

Building a better screen reader for the Linux desktop, one step at a time.

Building a better screen reader for the Linux desktop, one step at a time.

Elegant, clean Rust development framework

Preview version, will not guarantee the stability of the API! Elegant, clean Rust development framework Core Features Relational database client for M

BlackBird is a framework for Rust Tokio
BlackBird is a framework for Rust Tokio

BlackBird Blackbird is framework like OTP for Erlang. Blackbird is not a Actor Framewrok, it's Behavior for around Tokio task This project currently p

simple lottery maker made with rust, just web framework

Toy project for fun It's just for fun! making plausible lottery numbers for Korea made with rust This lottery web framework generates and presents pla

⚡rustygram is a minimal and blazing fast telegram notification framework for Rust
⚡rustygram is a minimal and blazing fast telegram notification framework for Rust

⚡rustygram ⚡rustygram is a minimal and blazing fast telegram notification framework using Rust. Abstracts away the Telegram API complexity so your app

The 峨眉 (EMei) JIT/AOT backend framework.

emei The 峨眉 (EMei) JIT/AOT backend framework. Support Instructions x86_64 mov mov mov_zero_extend_bit8/16 mov_sign_extend_bit8/16/32 mov_rev movs(is m

OptFrame is an optimization framework focused in metaheuristic techniques

optframe-rust Welcome to OptFrame project in Rust. What is OptFrame? OptFrame is an optimization framework focused in metaheuristic techniques, develo

Owner
Jonathan Chan Kwan Yin
a.k.a. SOFe, @chankyin, @PEMapModder
Jonathan Chan Kwan Yin
Nimbus is a framework for building parachain consensus systems on cumulus-based parachains.

Cumulo -- Nimbus ⛈️ Nimbus is a framework for building parachain consensus systems on cumulus-based parachains. Given the regular six-second pulse-lik

null 36 Dec 14, 2022
Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when developing Radix blueprints.

Scrypto Static Types Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when

null 7 Aug 5, 2022
The never type (the true one!) in stable Rust.

::never-say-never The ! type. In stable Rust. Since 1.14.0. Better than an enum Never {} definition would be, since an instance of type ! automagicall

Daniel Henry-Mantilla 17 Jan 3, 2023
Simplistic complier~virtual-machine that transforms AST into a Rust function, with basic type checking

rast-jit-vm rast-jit-vm is a simplistic, proof-of-concept~ish compiler / virtual machine that transforms syntax tree into a Rust function, type-checki

Patryk Wychowaniec 4 Oct 23, 2022
This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you specified.

prae This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you spec

null 96 Dec 4, 2022
A fusion of OTP lib/dialyzer + lib/compiler for regular Erlang with type inference

Typed ERLC The Problem I have a dream, that one day there will be an Erlang compiler, which will generate high quality type-correct code from deduced

Dmytro Lytovchenko 35 Sep 5, 2022
A bidirectional type checker

A bidirectional type inference system loosely based on Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism.

Samuel Ainsworth 35 Sep 22, 2022
sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

Gabriel Correia 1 Dec 27, 2022
🐀 Building a federated alternative to reddit in rust

Lemmy A link aggregator / Reddit clone for the fediverse. Join Lemmy · Documentation · Report Bug · Request Feature · Releases · Code of Conduct About

LemmyNet 7.2k Jan 3, 2023
🐀 Building a federated link aggregator in rust

English | Español | Русский Lemmy A link aggregator / Reddit clone for the fediverse. Join Lemmy · Documentation · Report Bug · Request Feature · Rele

LemmyNet 7.2k Jan 2, 2023