More flexible Literals-finding

This commit is contained in:
Tony Garnock-Jones 2021-09-15 15:07:46 +02:00
parent e56b62cfbb
commit 28249b19f7
6 changed files with 100 additions and 84 deletions

View File

@ -6,7 +6,6 @@ pub mod types;
pub mod unparsers;
use crate::*;
use crate::support::Parse;
use crate::compiler::context::*;
use crate::gen::Literals;
use crate::gen::schema;
@ -78,7 +77,7 @@ impl CompilerConfig {
let blob = reader.demand_next(false)?;
let literals = Literals::default();
if let Ok(s) = Schema::parse(&literals, &blob) {
if let Ok(s) = literals.parse(&blob) {
let prefix = i.file_stem().ok_or_else(
|| io::Error::new(io::ErrorKind::InvalidData,
format!("Bad schema file stem: {:?}", i)))?
@ -89,7 +88,7 @@ impl CompilerConfig {
continue;
}
if let Ok(Bundle { modules }) = Bundle::parse(&literals, &blob) {
if let Ok(Bundle { modules }) = literals.parse(&blob) {
for (ModulePath(k), v) in modules.0 {
self.bundle.insert(k, v);
}

View File

@ -27,7 +27,7 @@ pub fn gen_definition_parser(m: &mut ModuleContext, n: &str, d: &Definition) {
m.define_function(
n,
|mut ctxt| {
let mut body = vec![];
let mut body = vec![item("let _ctxt = _ctxt.into();")];
match d {
Definition::Or { pattern_0, pattern_1, pattern_n } => {
@ -74,9 +74,10 @@ pub fn gen_definition_parser(m: &mut ModuleContext, n: &str, d: &Definition) {
ctxt.m.any_type()], " for ",
names::render_constructor(n), ty.generic_arg(ctxt.m), " ",
block![
seq!["fn parse(_ctxt: &", ctxt.m.literals_type(),
", value: &", ctxt.m.any_type(), ") -> ",
"std::result::Result<Self, _support::ParseError> ",
seq!["fn parse<'a, _LL: Into<&'a ", ctxt.m.literals_type(), ">>",
"(_ctxt: _LL, value: &", ctxt.m.any_type(), ") ",
" -> std::result::Result<Self, _support::ParseError>",
" where ", ctxt.m.literals_type(), ": 'a ",
codeblock(body)]]])
});
}

View File

@ -66,7 +66,7 @@ pub fn gen_definition_unparser(m: &mut ModuleContext, n: &str, d: &Definition) {
m.define_function(
n,
|mut ctxt| {
let mut body = Vec::new();
let mut body = vec![item("let _ctxt = _ctxt.into();")];
match d {
Definition::Or { pattern_0, pattern_1, pattern_n } => {
@ -102,24 +102,11 @@ pub fn gen_definition_unparser(m: &mut ModuleContext, n: &str, d: &Definition) {
ctxt.m.any_type()], " for ",
names::render_constructor(n), ty.generic_arg(ctxt.m), " ",
block![
seq!["fn unparse(&self, _ctxt: &", ctxt.m.literals_type(),
") -> ", ctxt.m.any_type(), " ",
seq!["fn unparse<'a, _LL: Into<&'a ", ctxt.m.literals_type(), ">>",
"(&self, _ctxt: _LL) -> ", ctxt.m.any_type(),
" where ", ctxt.m.literals_type(), ": 'a ",
block(body)]]])
});
// m.define_function(
// n,
// |ctxt| {
// let ty_arg = item(seq![names::render_constructor(n), ty.generic_arg(ctxt.m)]);
// item(seq!["impl", ctxt.m.literals_generic_decls(),
// " std::convert::From", anglebrackets![seq!["&", ty_arg.clone()]],
// " for ", ctxt.m.any_type(), " ", block![
// seq!["fn from(value: &", ty_arg, ") -> Self ",
// codeblock![
// seq!["Self::unparse(",
// ctxt.m.literals_base_type(), "::default()",
// ", value)"]]]]])
// });
}
fn destruct(

View File

@ -164,7 +164,8 @@ fn parse_atom_kind_symbol<_Value: preserves::value::NestedValue>(_ctxt: &crate::
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for AtomKind {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_atom_kind_boolean(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_atom_kind_float(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_atom_kind_double(_ctxt, value) { return Ok(r); }
@ -177,7 +178,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for AtomKind {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
AtomKind::Boolean => (&_ctxt.LIT_0_BOOLEAN).clone(),
AtomKind::Float => (&_ctxt.LIT_1_FLOAT).clone(),
@ -224,7 +226,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Binding<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp0 = value.value().to_record(None)?;
if _tmp0.label() != &_ctxt.LIT_7_NAMED { return Err(_support::ParseError::conformance_error("schema.Binding")); }
let _tmp1 = ();
@ -236,7 +239,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Binding<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Binding {name: _tmp0, pattern: _tmp1} = self;
{
let mut _tmp2 = preserves::value::Record(vec![(&_ctxt.LIT_7_NAMED).clone()]);
@ -277,7 +281,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Bundle<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp0 = value.value().to_record(None)?;
if _tmp0.label() != &_ctxt.LIT_8_BUNDLE { return Err(_support::ParseError::conformance_error("schema.Bundle")); }
let _tmp1 = ();
@ -288,7 +293,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Bundle<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Bundle {modules: _tmp0} = self;
{
let mut _tmp1 = preserves::value::Record(vec![(&_ctxt.LIT_8_BUNDLE).clone()]);
@ -476,7 +482,8 @@ fn parse_compound_pattern_dict<_Value: preserves::value::NestedValue>(_ctxt: &cr
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for CompoundPattern<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_compound_pattern_rec(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_compound_pattern_tuple(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_compound_pattern_tuple_prefix(_ctxt, value) { return Ok(r); }
@ -486,7 +493,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for CompoundPattern<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
CompoundPattern::Rec {label: _tmp0, fields: _tmp1} => {
let mut _tmp2 = preserves::value::Record(vec![(&_ctxt.LIT_9_REC).clone()]);
@ -678,7 +686,8 @@ fn parse_definition_pattern<_Value: preserves::value::NestedValue>(_ctxt: &crate
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Definition<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_definition_or(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_definition_and(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_definition_pattern(_ctxt, value) { return Ok(r); }
@ -687,7 +696,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Definition<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
Definition::Or {pattern_0: _tmp0, pattern_1: _tmp1, pattern_n: _tmp2} => {
let mut _tmp3 = preserves::value::Record(vec![(&_ctxt.LIT_13_OR).clone()]);
@ -745,7 +755,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Definitions<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let mut _tmp0 = preserves::value::Map::new();
for (_tmp1, _tmp2) in value.value().to_dictionary()? {
let _tmp3 = _tmp1.value().to_symbol()?;
@ -757,7 +768,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Definitions<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Definitions(_tmp0) = self;
preserves::value::Value::Dictionary(_tmp0.iter().map(|(_tmp1, _tmp2)| (preserves::value::Value::symbol(_tmp1).wrap(), _tmp2.unparse(_ctxt))).collect()).wrap()
}
@ -788,7 +800,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for DictionaryEntries<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let mut _tmp0 = preserves::value::Map::new();
for (_tmp1, _tmp2) in value.value().to_dictionary()? {
let _tmp3 = _tmp1;
@ -800,7 +813,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for DictionaryEntries<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let DictionaryEntries(_tmp0) = self;
preserves::value::Value::Dictionary(_tmp0.iter().map(|(_tmp1, _tmp2)| (_tmp1.clone(), _tmp2.unparse(_ctxt))).collect()).wrap()
}
@ -852,7 +866,8 @@ fn parse_embedded_type_name_false<_Value: preserves::value::NestedValue>(_ctxt:
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for EmbeddedTypeName {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_embedded_type_name_ref(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_embedded_type_name_false(_ctxt, value) { return Ok(r); }
Err(_support::ParseError::conformance_error("schema.EmbeddedTypeName"))
@ -860,7 +875,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for EmbeddedTypeName {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
EmbeddedTypeName::Ref(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
EmbeddedTypeName::False => (&_ctxt.LIT_15_FALSE).clone(),
@ -890,7 +906,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for ModulePath {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp1 = value.value().to_sequence()?;
let mut _tmp0 = std::vec::Vec::new();
for _tmp2 in &_tmp1[0..] {let _tmp3 = _tmp2.value().to_symbol()?; _tmp0.push(_tmp3.clone());}
@ -899,7 +916,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for ModulePath {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let ModulePath(_tmp0) = self;
{
let mut _tmp1 = std::vec::Vec::new();
@ -934,7 +952,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Modules<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let mut _tmp0 = preserves::value::Map::new();
for (_tmp1, _tmp2) in value.value().to_dictionary()? {
let _tmp3 = ModulePath::parse(_ctxt, _tmp1)?;
@ -946,7 +965,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Modules<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Modules(_tmp0) = self;
preserves::value::Value::Dictionary(_tmp0.iter().map(|(_tmp1, _tmp2)| (_tmp1.unparse(_ctxt), _tmp2.unparse(_ctxt))).collect()).wrap()
}
@ -976,7 +996,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for NamedAlternative<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp0 = value.value().to_sequence()?;
if _tmp0.len() < 2 { return Err(_support::ParseError::conformance_error("schema.NamedAlternative")); }
let _tmp1 = (&_tmp0[0]).value().to_string()?;
@ -986,7 +1007,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for NamedAlternative<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let NamedAlternative {variant_label: _tmp0, pattern: _tmp1} = self;
{
let mut _tmp2 = std::vec::Vec::new();
@ -1035,7 +1057,8 @@ fn parse_named_pattern_anonymous<_Value: preserves::value::NestedValue>(_ctxt: &
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for NamedPattern<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_named_pattern_named(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_named_pattern_anonymous(_ctxt, value) { return Ok(r); }
Err(_support::ParseError::conformance_error("schema.NamedPattern"))
@ -1043,7 +1066,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for NamedPattern<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
NamedPattern::Named(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
NamedPattern::Anonymous(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
@ -1089,7 +1113,8 @@ fn parse_named_simple_pattern_anonymous<_Value: preserves::value::NestedValue>(_
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for NamedSimplePattern<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_named_simple_pattern_named(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_named_simple_pattern_anonymous(_ctxt, value) { return Ok(r); }
Err(_support::ParseError::conformance_error("schema.NamedSimplePattern"))
@ -1097,7 +1122,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for NamedSimplePattern<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
NamedSimplePattern::Named(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
NamedSimplePattern::Anonymous(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
@ -1143,7 +1169,8 @@ fn parse_pattern_compound_pattern<_Value: preserves::value::NestedValue>(_ctxt:
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Pattern<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_pattern_simple_pattern(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_pattern_compound_pattern(_ctxt, value) { return Ok(r); }
Err(_support::ParseError::conformance_error("schema.Pattern"))
@ -1151,7 +1178,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Pattern<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
Pattern::SimplePattern(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
Pattern::CompoundPattern(_tmp0) => _tmp0.as_ref().unparse(_ctxt),
@ -1193,7 +1221,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Ref {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp0 = value.value().to_record(None)?;
if _tmp0.label() != &_ctxt.LIT_16_REF { return Err(_support::ParseError::conformance_error("schema.Ref")); }
let _tmp1 = ();
@ -1205,7 +1234,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Ref {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Ref {module: _tmp0, name: _tmp1} = self;
{
let mut _tmp2 = preserves::value::Record(vec![(&_ctxt.LIT_16_REF).clone()]);
@ -1284,7 +1314,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Schema<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let _tmp0 = value.value().to_record(None)?;
if _tmp0.label() != &_ctxt.LIT_17_SCHEMA { return Err(_support::ParseError::conformance_error("schema.Schema")); }
let _tmp1 = ();
@ -1301,7 +1332,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Schema<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
let Schema {definitions: _tmp0, embedded_type: _tmp1, version: _tmp2} = self;
{
let mut _tmp3 = preserves::value::Record(vec![(&_ctxt.LIT_17_SCHEMA).clone()]);
@ -1568,7 +1600,8 @@ fn parse_simple_pattern_ref<_Value: preserves::value::NestedValue>(_ctxt: &crate
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for SimplePattern<_Value> {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if let Ok(r) = parse_simple_pattern_any(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_simple_pattern_atom(_ctxt, value) { return Ok(r); }
if let Ok(r) = parse_simple_pattern_embedded(_ctxt, value) { return Ok(r); }
@ -1582,7 +1615,8 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for SimplePattern<_Value> {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
match self {
SimplePattern::Any => (&_ctxt.LIT_21_ANY).clone(),
SimplePattern::Atom {atom_kind: _tmp0} => {
@ -1641,7 +1675,8 @@ impl<'de, _Value: preserves::value::NestedValue, R: _support::Reader<'de, _Value
}
impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals<_Value>, _Value> for Version {
fn parse(_ctxt: &crate::gen::Literals<_Value>, value: &_Value) -> std::result::Result<Self, _support::ParseError> {
fn parse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(_ctxt: _LL, value: &_Value) -> std::result::Result<Self, _support::ParseError> where crate::gen::Literals<_Value>: 'a {
let _ctxt = _ctxt.into();
if value != &_ctxt.LIT_28_1 { return Err(_support::ParseError::conformance_error("schema.Version")); }
let _tmp0 = ();
Ok(Version)
@ -1649,5 +1684,5 @@ impl<_Value: preserves::value::NestedValue> _support::Parse<crate::gen::Literals
}
impl<_Value: preserves::value::NestedValue> _support::Unparse<crate::gen::Literals<_Value>, _Value> for Version {
fn unparse(&self, _ctxt: &crate::gen::Literals<_Value>) -> _Value {let Version = self; (&_ctxt.LIT_28_1).clone()}
fn unparse<'a, _LL: Into<&'a crate::gen::Literals<_Value>>>(&self, _ctxt: _LL) -> _Value where crate::gen::Literals<_Value>: 'a {let _ctxt = _ctxt.into(); let Version = self; (&_ctxt.LIT_28_1).clone()}
}

View File

@ -5,9 +5,7 @@ pub mod gen;
pub use support::Codec;
pub use support::Deserialize;
pub use support::Parse;
pub use support::ParseError;
pub use support::Unparse;
#[cfg(test)]
mod tests {
@ -38,8 +36,8 @@ mod tests {
#[test]
fn metaschema_parsing() -> Result<(), std::io::Error> {
use preserves::value::{BinarySource, IOBinarySource, Reader};
use crate::Parse;
use crate::Unparse;
use crate::support::Parse;
use crate::support::Unparse;
use crate::gen::schema::*;
let mut f = std::fs::File::open("../../../schema/schema.bin")?;

View File

@ -12,52 +12,48 @@ use preserves::value::NoEmbeddedDomainCodec;
use preserves::value::Value;
use std::convert::From;
use std::convert::Into;
use std::convert::TryFrom;
use std::io;
use std::marker::PhantomData;
// use std::marker::PhantomData;
use std::sync::Arc;
use thiserror::Error;
pub trait Parse<L, Value: NestedValue>: Sized {
fn parse(literals: &L, value: &Value) -> Result<Self, ParseError>;
// fn parse(literals: &L, value: &Value) -> Result<Self, ParseError>;
fn parse<'a, LL: Into<&'a L>>(literals: LL, value: &Value) -> Result<Self, ParseError> where L: 'a;
}
impl<L, Value: NestedValue> Parse<L, Value> for Value {
fn parse(_literals: &L, value: &Value) -> Result<Self, ParseError> {
fn parse<'a, LL: Into<&'a L>>(_literals: LL, value: &Value) -> Result<Self, ParseError> where L: 'a {
Ok(value.clone())
}
}
pub trait Unparse<L, Value: NestedValue> {
fn unparse(&self, literals: &L) -> Value;
// fn unparse(&self, literals: &L) -> Value;
fn unparse<'a, LL: Into<&'a L>>(&self, literals: LL) -> Value where L: 'a;
}
impl<L, Value: NestedValue> Unparse<L, Value> for Value {
fn unparse(&self, _literals: &L) -> Value {
fn unparse<'a, LL: Into<&'a L>>(&self, _literals: LL) -> Value where L: 'a {
self.clone()
}
}
pub struct Codec<L: Default, N: NestedValue> {
pub literals: L,
phantom: PhantomData<N>,
pub trait Codec<L, N: NestedValue> {
fn parse<T: Parse<L, N>>(&self, value: &N) -> Result<T, ParseError>;
fn unparse<T: Unparse<L, N>>(&self, value: &T) -> N;
}
impl<L: Default, N: NestedValue> Codec<L, N> {
pub fn new() -> Self {
Codec {
literals: L::default(),
phantom: PhantomData,
}
impl<L, N: NestedValue, LL> Codec<L, N> for LL where for<'a> &'a LL: Into<&'a L> {
fn parse<T: Parse<L, N>>(&self, value: &N) -> Result<T, ParseError> {
T::parse(self, value)
}
pub fn parse<T: Parse<L, N>>(&self, value: &N) -> Result<T, ParseError> {
T::parse(&self.literals, value)
}
pub fn unparse<T: Unparse<L, N>>(&self, value: &T) -> N {
value.unparse(&self.literals)
fn unparse<T: Unparse<L, N>>(&self, value: &T) -> N {
value.unparse(self)
}
}