Stub implementation
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "corona"
|
||||
version = "0.1.0"
|
||||
authors = ["k.bagrov <k.bagrov@g.nsu.ru>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0"
|
||||
@@ -1,2 +1,27 @@
|
||||
# corona
|
||||
Compiled Rust Object Notation - serializing to source code
|
||||
|
||||
Makes Serde serialize your data to the Rust source code.
|
||||
|
||||
### Why?
|
||||
|
||||
This crate was inspired by the [Stack Overflow question](https://stackoverflow.com/questions/58359340/deserialize-file-using-serde-json-at-compile-time). 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.
|
||||
|
||||
### How to?
|
||||
|
||||
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`](https://doc.rust-lang.org/stable/std/io/trait.Write.html) implementation, if you want to). Then, you'll [`include!`](https://doc.rust-lang.org/stable/std/macro.include.html) the generated file wherever you want to use it.
|
||||
|
||||
### Is it really that simple?
|
||||
|
||||
Well... not. There are several limitations.
|
||||
|
||||
1. All the types used in the serialized struct must be in scope on the include site. Serde doesn't provide the qualified name (i.e. path) to the serializer, only the direct name. The probably easiest way is to use the serialized data as following:
|
||||
```rust
|
||||
let value = {
|
||||
use ::path::to::Type1;
|
||||
// ...and other types
|
||||
include!("path/to/file.rs")
|
||||
}
|
||||
```
|
||||
or the similar construction using [`lazy_static`](http://crates.io/crates/lazy_static).
|
||||
2. As a consequence, all the types used by the serialized one must have distinct names (or they'll clash with each other).
|
||||
3. Deserializer isn't implemented. This is intentional, since this crate isn't really intended for runtime usage.
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
use serde::ser;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct Corona<W: Write>(W);
|
||||
|
||||
impl<W: Write> ser::Serializer for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
type SerializeSeq = Corona<W>;
|
||||
type SerializeTuple = Corona<W>;
|
||||
type SerializeTupleStruct = Corona<W>;
|
||||
type SerializeTupleVariant = Corona<W>;
|
||||
type SerializeMap = Corona<W>;
|
||||
type SerializeStruct = Corona<W>;
|
||||
type SerializeStructVariant = Corona<W>;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
|
||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error> { todo!() }
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> { todo!() }
|
||||
|
||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: std::fmt::Display { todo!() }
|
||||
|
||||
}
|
||||
|
||||
impl<W: Write> ser::SerializeSeq for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeTuple for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeTupleStruct for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeTupleVariant for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeMap for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeStruct for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
impl<W: Write> ser::SerializeStructVariant for Corona<W> {
|
||||
type Ok = ();
|
||||
type Error = CoronaError;
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: serde::Serialize { todo!() }
|
||||
fn end(self) -> Result<Self::Ok, Self::Error> { todo!() }
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CoronaError(String);
|
||||
|
||||
impl std::fmt::Display for CoronaError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() }
|
||||
}
|
||||
|
||||
impl std::error::Error for CoronaError {
|
||||
|
||||
}
|
||||
impl ser::Error for CoronaError {
|
||||
fn custom<T>(msg:T)->Self where T: std::fmt::Display {
|
||||
Self(msg.to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user