-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reimplement codegen using quote
#40
Conversation
bde9fc5
to
a9dc0d8
Compare
quote
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than rustfmt and prettyplease appearing to have very different opinions about certain formatting situations, the code before and after this looks equivalent to me -- except for the new automatically_generated
attribute and the nit below.
src/server.rs
Outdated
) -> Result<(), idol_runtime::RequestError<u16>> { | ||
#[allow(unused_imports)] | ||
use core::convert::TryInto; | ||
#[allow(unused_imports)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this unused_imports was not in the original and should not be required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...alternatively... it kind of looks like you've newly qualified all references to ClientError? In which case this use might be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, whoops, I didn't notice that; I can either remove the use
or change them back --- have you a preference?
PR oxidecomputer/idolatry#40 updates `idol`'s code generation to use the `quote` crate for generating Rust code, rather than representing it as a string. This commit updates Hubris' dependency on `idol` to oxidecomputer/idolatry@c5222fe.
PR oxidecomputer/idolatry#40 updates `idol`'s code generation to use the `quote` crate for generating Rust code, rather than representing it as a string. This commit updates Hubris' dependency on `idol` to oxidecomputer/idolatry@c5222fe.
Currently, all of
idol
s code generation is implemented by "manually"writing out Rust code as strings. The
quote
crate provides a macro forquasi-quoting Rust code inside of Rust code, as a
proc-_macro2::TokenStream
. This is generally the preferred way to docode generation in much of the Rust ecosystem.
Some reasons we might want to use
quote
include:prettyplease
, a crate that does rustfmt-likeformatting as a library dep, so we no longer need to invoke an external
rustfmt
as part of the build process in Hubrisquote
, we can interpolate things like types andidentifiers as their parsed representation from the
syn
crate. Thisallows us to actually parse these fragments from the interface
definition and validate that they're correct --- a
Name
can berepresented by a
syn::Ident
, rather than aString
, so we know thatit's a valid Rust identifier, and parsing the RON will fail if it
isn't. Similarly, we can parse types this way, allowing us to detect
!Sized
types much more accurately.quote!
looks like Rust code, rather than a bigstring literal. Therefore, it can benefit from an editor's brace
matching, syntax highlighting, indentation, etc.
This branch rewrites
idol
's code generation usingquote
.Testing
I've manually tested that the code generated is still correct by
comparing the output between this branch and
main
, using the exampleinterface definition from the README:
Generated client stubs:
main
:This branch:
Generated server stubs:
main
:this branch:
As you can (hopefully) tell, the generated code is the same, with the
exception of different formatting.