1#![doc = concat!(
2 include_str!("../README.md"),
3 "# What is Preserves Schema?\n\n",
4 include_str!("../doc/what-is-preserves-schema.md"),
5 include_str!("../doc/example.md"),
6)]
7
8pub mod compiler;
9pub mod gen;
11pub mod support;
12pub mod syntax;
13
14pub use support::Codec;
15pub use support::Deserialize;
16pub use support::ParseError;
17
18#[cfg(test)]
19mod tests {
20 #[test]
21 fn can_access_preserves_core() {
22 use preserves::value::*;
23 assert_eq!(format!("{:?}", UnwrappedIOValue::from(3 + 4)), "7");
24 }
25
26 #[test]
27 fn simple_rendering() {
28 use crate::syntax::block::*;
29 use crate::*;
30
31 let code = semiblock![
32 seq!["f", parens!["a", "b", "c"]],
33 seq!["f", parens!["a", "b", "c"]],
34 seq!["f", parens!["a", "b", "c"]],
35 seq!["f", parens!["a", "b", "c"]],
36 seq!["f", parens!["a", "b", "c"]],
37 seq!["f", parens!["a", "b", "c"]],
38 seq!["g", parens![]],
39 parens![]
40 ];
41 println!("{}", Formatter::to_string(&code));
42 }
43
44 #[test]
45 fn metaschema_parsing() -> Result<(), std::io::Error> {
46 use crate::gen::schema::*;
47 use crate::support::Parse;
48 use crate::support::Unparse;
49 use preserves::value::{BinarySource, IOBinarySource, Reader};
50
51 let mut f = std::fs::File::open("./schema.bin")?;
52 let mut src = IOBinarySource::new(&mut f);
53 let mut reader = src.packed_iovalues();
54 let schema = reader.demand_next(false)?;
55 let language = crate::gen::Language::default();
56 let parsed = Schema::parse(&language, &schema).expect("successful parse");
57 assert_eq!(schema, parsed.unparse(&language));
58 Ok(())
59 }
60}
61
62#[macro_export]
63macro_rules! define_language {
64 ($fname:ident () : $lang:ident < $default_value:ty > { $($field:ident : $($type:ident)::+ ,)* }) => {
65 pub struct $lang<N: $crate::support::preserves::value::NestedValue> {
66 $(pub $field: std::sync::Arc<$($type)::*<N>>),*
67 }
68
69 $(impl<'a, N: $crate::support::preserves::value::NestedValue> From<&'a $lang<N>> for &'a $($type)::*<N> {
70 fn from(v: &'a $lang<N>) -> Self {
71 &v.$field
72 }
73 })*
74
75 impl<N: $crate::support::preserves::value::NestedValue> $crate::support::NestedValueCodec
76 for $lang<N> {}
77
78 mod $fname {
79 use super::*;
80 lazy_static::lazy_static! {
81 pub static ref GLOBAL_LANG: std::sync::Arc<$lang<$default_value>> =
82 std::sync::Arc::new($lang {
83 $($field: std::sync::Arc::new($($type)::*::default())),*
84 });
85 }
86 }
87
88 impl $lang<$default_value> {
89 pub fn arc() -> &'static std::sync::Arc<$lang<$default_value>> {
90 &*$fname::GLOBAL_LANG
91 }
92 }
93
94 pub fn $fname() -> &'static $lang<$default_value> {
95 &*$fname::GLOBAL_LANG
96 }
97 };
98}