This is a sister crate of flexpiler
, a crate to provide customized deserialization (and eventually serialization).
This project provides a #[derive]
macro. This macro generates code such that the struct / enum being derived gains an impl of flexpiler::Deserialize
. Intended usage of the crate is by adding a dependency to the flexpiler
crate and using the macro as re-exported from it like so:
```rust
struct MyPoint { x: i32, y: i32, } ```
Last updated: version 0.1.1
For above example, the crate generates the following code:
``` pub struct MyPointflexpilerContext { xoption : Option < i32 >, yoption : Option < i32 >, }
pub struct MyPointflexpilerDeserializer ;
impl Default for MyPointflexpilerContext { fn default() -> Self { MyPointflexpilerContext { xoption : None, yoption : None, } } }
impl std :: convert :: TryInto < MyPoint > for MyPointflexpilerContext { type Error = flexpiler :: Error ; fn tryinto(self) -> std :: result :: Result < MyPoint, flexpiler :: Error > { use flexpiler :: error :: Trait as ErrorTrait ; let x = match self . xoption { Some(value) => value, None => { let missingstructfield = flexpiler :: error :: MissingStructField { structdeclarationfound : std :: string :: String :: from("MyPoint"), fielddeclarationexpected : std :: string :: String :: from("x"), } ; let error = flexpiler :: Error :: gen(missingstructfield) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; } } ; let y = match self . yoption { Some(value) => value, None => { let missingstructfield = flexpiler :: error :: MissingStructField { structdeclarationfound : std :: string :: String :: from("MyPoint"), fielddeclarationexpected : std :: string :: String :: from("y"), } ; let error = flexpiler :: Error :: gen(missingstructfield) . propagate(MyPointflexpilerDeserializer :: context_general()) ; return Err(error) ; } } ; Ok(MyPoint { x, y, }) } }
impl MyPointflexpilerDeserializer { fn contextfield(fieldname : & str) -> flexpiler :: error :: Context { let mut trace = std :: string :: String :: from("MyPoint") ; trace . pushstr("[") ; trace . pushstr(fieldname) ; trace . pushstr("]") ; flexpiler :: error :: Context { trace, } } fn context_general() -> flexpiler :: error :: Context { flexpiler :: error :: Context { trace : std :: string :: String :: from("MyPoint"), } } }
impl flexpiler :: deserializer :: Trait < MyPoint, flexpiler :: common :: rustc :: deserializer :: Context > for MyPointflexpilerDeserializer { fn deserialize < ReaderType > (readermutref : & mut ReaderType) -> std :: result :: Result < flexpiler :: deserializer :: Result < MyPoint, flexpiler :: common :: rustc :: deserializer :: Context >, flexpiler :: Error > where ReaderType : flexpiler :: reader :: Trait { use std :: convert :: TryInto ; use flexpiler :: Deserialization ; use flexpiler :: deserializer :: Trait as DeserializerTrait ; use flexpiler :: error :: Trait as ErrorTrait ; use flexpiler :: parser :: Parse ; let structdeclarationstring = match flexpiler :: common :: rustc :: block :: IdentifierWithDataStartFinish :: parse(readermutref) { Err(parsererror) => { let error = flexpiler :: Error :: gen(parsererror) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; }, Ok(flexpiler :: common :: rustc :: block :: identifierwithdatastartfinish :: Result :: DataStartFinish(declaration)) => { declaration }, Ok(flexpiler :: common :: rustc :: block :: identifierwithdatastartfinish :: Result :: Freestanding(declaration)) => { match flexpiler :: common :: rustc :: block :: DataStart :: parse(readermutref) { Err(parsererror) => { let error = flexpiler :: Error :: gen(parsererror) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; }, Ok() => { }, } declaration }, } ; if structdeclarationstring . asstr() != "MyPoint" { let incompatiblestructdeclaration = flexpiler :: error :: IncompatibleStructDeclaration { structdeclarationexpected : String :: from("MyPoint"), structdeclarationfound : structdeclarationstring, } ; let error = flexpiler :: Error :: gen(incompatiblestructdeclaration) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; } let mut structcontext = MyPointflexpilerContext :: default() ; loop { let fielddeclarationstring = match flexpiler :: common :: rustc :: block :: DeclarationOrDataEnd :: parse(readermutref) { Err(parsererror) => { let error = flexpiler :: error :: Error :: gen(parsererror) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; }, Ok(flexpiler :: common :: rustc :: block :: declarationordataend :: Result :: DataEnd()) => { break ; }, Ok(flexpiler :: common :: rustc :: block :: declarationordataend :: Result :: Declaration(declaration)) => { declaration }, } ; let context = match fielddeclarationstring . asstr() { "x" => { let result = < i32 as flexpiler :: Deserialization < flexpiler :: common :: rustc :: Format >> :: Deserializer :: deserialize(readermutref) ; match result { Ok(value) => { structcontext . xoption = Some(value . data) ; value . context } Err(error) => { let error = error . propagate(MyPointflexpilerDeserializer :: contextfield("x")) ; return Err(error) ; } } } "y" => { let result = < i32 as flexpiler :: Deserialization < flexpiler :: common :: rustc :: Format >> :: Deserializer :: deserialize(readermutref) ; match result { Ok(value) => { structcontext . yoption = Some(value . data) ; value . context } Err(error) => { let error = error . propagate(MyPointflexpilerDeserializer :: contextfield("y")) ; return Err(error) ; } } } _ => { let unrecognizedfield = flexpiler :: error :: UnrecognizedFieldDeclaration { fielddeclarationfound : fielddeclarationstring, fielddeclarationexpectedentries : flexpiler :: error :: ExpectedEntries :: from(vec ! [String :: from("x"), String :: from("y"),]), } ; let error = flexpiler :: Error :: gen(unrecognizedfield) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; } } ; match context { flexpiler :: common :: rustc :: deserializer :: Context :: Freestanding => { match flexpiler :: common :: rustc :: block :: DataEndOrSeparator :: parse(readermutref) { Ok(flexpiler :: common :: rustc :: block :: dataendorseparator :: Result :: DataEnd) => { break ; }, Ok(flexpiler :: common :: rustc :: block :: dataendorseparator :: Result :: Separator) => { }, Err(parsererror) => { let error = flexpiler :: error :: Error :: gen(parsererror) . propagate(MyPointflexpilerDeserializer :: contextfield(fielddeclarationstring . asstr())) ; return Err(error) ; }, } }, flexpiler :: common :: rustc :: deserializer :: Context :: DataEnd => { break ; }, flexpiler :: common :: rustc :: deserializer :: Context :: Separator => { }, _ => { let unexpectedentryfinishcontext = flexpiler :: error :: UnexpectedEntryFinishContext { entrydeclaration : fielddeclarationstring, contextexpected : flexpiler :: error :: ExpectedEntries :: from(vec ! [flexpiler :: common :: rustc :: deserializer :: Context :: DataEnd, flexpiler :: common :: rustc :: deserializer :: Context :: Separator,]), contextfound : context, } ; let error = flexpiler :: Error :: gen(unexpectedentryfinishcontext) . propagate(MyPointflexpilerDeserializer :: contextgeneral()) ; return Err(error) ; } } } return match structcontext . tryinto() { Ok(data) => { Ok(flexpiler :: deserializer :: Result { context : flexpiler :: common :: rustc :: deserializer :: Context :: Freestanding, data, }) }, Err(error) => { Err(error) }, } } }
impl flexpiler :: Deserialization < flexpiler :: common :: rustc :: Format > for MyPoint { type Deserializer = MyPointflexpilerDeserializer; }
```