Crates.io | uneval |
lib.rs | uneval |
version | 0.2.4 |
source | src |
created_at | 2020-04-30 15:05:44.393108 |
updated_at | 2022-03-30 14:16:10.330328 |
description | Serde serializer to embed data as Rust code |
homepage | |
repository | https://github.com/Cerber-Ursi/uneval |
max_upload_size | |
id | 235855 |
size | 38,527 |
Makes Serde serialize your data to the Rust source code.
This crate was inspired by the Stack Overflow question. In short, if you want to make some data in the human-readable format, such as JSON, to be deserialized at compile-time and build into the binary, as if it was written by hand, then this crate can possibly help you.
This crate is intended to be used from the build script. It will serialize anything you provide to it to any path you provide (or to the arbitrary io::Write
implementation, or into String
, if you want to). Then, you'll include!
the generated file wherever you want to use it.
See the crate documentation for details. In short, we use information provided by Serde to emit the code, which, when assigned to the variable of correct type, will provide all necessary conversions by using Into
and iterators.
Well... not. There are several limitations.
let value: MainType = {
use ::path::to::Type1;
// ...and other types
include!("path/to/file.rs")
}
or the similar construction using lazy_static
.
Serialize
.Serialize
to be used by this crate during the build-time of the second copy. (Isn't this a bit too complex?)If you find any other case where this doesn't work, feel free to open an issue - we'll either fix the code or document the newly-found limitation.
This crate uses batch_run
to run its tests.
The common structure of test cases is like following:
definition.rs
contains the necessary types.{test_name}-main.rs
includes definition.rs
as module. It contains the main
function, which creates an instance of some type from definition.rs
, generates the corresponding Rust code in generated.rs
and launches {test_name}-user.rs
through batch_run
.{test_name}-user.rs
includes definition.rs
as module and generated.rs
through call to include!
. It checks that the generated code indeed creates the data equal to what was created initially.Testing data itself is defined in [test_fixtures/data.toml], and is in the following format:
batch_run
's batch.main_type
corresponds to the type which serialization is being tested.support_types
as a comma-separated list. These, together with the main_type
, will be included in {test_name}-user.rs
as imports.definition
is literally copied into the definition.rs
. It's necessary to derive Debug
, Serialize
and PartialEq
on all the types there, since these traits are used during test entry run.value
is literally copied in two places: first, the {test_name}-main.rs
, where the code is generated; second, in {test_name}-user.rs
, where test checks two values for equality.MIT