-
Notifications
You must be signed in to change notification settings - Fork 262
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
Add 'Static' type and improve type substitution codegen to accept it #886
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1335d48
Add Static type which defers to Encode/Decode and impls EncodeAsType/…
jsdw c38e585
rename to static_type and impl Deref/Mut
jsdw 6d99062
Improve type substitution in codegen so that concrete types can be sw…
jsdw e8734db
A couple of comment tweaks and no need for a macro export
jsdw 8833795
Extend type substitution logic to work recursively on destination type
jsdw 0ae6ff1
cargo fmt
jsdw 0c3fca6
Merge branch 'master' into jsdw-static-type
jsdw b04a4a3
Fix a couple of comments
jsdw 5282b1e
update ui test outpuot
jsdw 2b4203b
Add docs and missing_docs lint
jsdw 0ff9dc7
Add test for replacing multiple of Ident
jsdw dffe435
Update codegen/src/error.rs
jsdw 7145211
update copyright year and fix ui test
jsdw ad02643
simplify another error
jsdw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Copyright 2019-2022 Parity Technologies (UK) Ltd. | ||
// Copyright 2019-2023 Parity Technologies (UK) Ltd. | ||
// This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
// see LICENSE for license details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2019-2023 Parity Technologies (UK) Ltd. | ||
// This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
// see LICENSE for license details. | ||
|
||
//! Errors that can be emitted from codegen. | ||
|
||
use proc_macro2::{Span, TokenStream as TokenStream2}; | ||
|
||
/// Error returned when the Codegen cannot generate the runtime API. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum CodegenError { | ||
/// The given metadata type could not be found. | ||
#[error("Could not find type with ID {0} in the type registry; please raise a support issue.")] | ||
TypeNotFound(u32), | ||
/// Cannot fetch the metadata bytes. | ||
#[error("Failed to fetch metadata, make sure that you're pointing at a node which is providing V14 metadata: {0}")] | ||
Fetch(#[from] FetchMetadataError), | ||
/// Failed IO for the metadata file. | ||
#[error("Failed IO for {0}, make sure that you are providing the correct file path for metadata V14: {1}")] | ||
Io(String, std::io::Error), | ||
/// Cannot decode the metadata bytes. | ||
#[error("Could not decode metadata, only V14 metadata is supported: {0}")] | ||
Decode(#[from] codec::Error), | ||
/// Out of line modules are not supported. | ||
#[error("Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}")] | ||
InvalidModule(Span), | ||
/// Expected named or unnamed fields. | ||
#[error("Fields should either be all named or all unnamed, make sure you are providing a valid metadata V14: {0}")] | ||
InvalidFields(String), | ||
/// Substitute types must have a valid path. | ||
#[error("Type substitution error: {0}")] | ||
TypeSubstitutionError(#[from] TypeSubstitutionError), | ||
/// Invalid type path. | ||
#[error("Invalid type path {0}: {1}")] | ||
InvalidTypePath(String, syn::Error), | ||
/// Metadata for constant could not be found. | ||
#[error("Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")] | ||
MissingConstantMetadata(String, String), | ||
/// Metadata for storage could not be found. | ||
#[error("Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")] | ||
MissingStorageMetadata(String, String), | ||
/// Metadata for call could not be found. | ||
#[error("Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")] | ||
MissingCallMetadata(String, String), | ||
/// Call variant must have all named fields. | ||
#[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid metadata V14")] | ||
InvalidCallVariant(u32), | ||
/// Type should be an variant/enum. | ||
#[error( | ||
"{0} type should be an variant/enum type. Make sure you are providing a valid metadata V14" | ||
)] | ||
InvalidType(String), | ||
} | ||
|
||
impl CodegenError { | ||
/// Fetch the location for this error. | ||
// Todo: Probably worth storing location outside of the variant, | ||
// so that there's a common way to set a location for some error. | ||
fn get_location(&self) -> Span { | ||
match self { | ||
Self::InvalidModule(span) => *span, | ||
Self::TypeSubstitutionError(err) => err.get_location(), | ||
Self::InvalidTypePath(_, err) => err.span(), | ||
_ => proc_macro2::Span::call_site(), | ||
} | ||
} | ||
/// Render the error as an invocation of syn::compile_error!. | ||
pub fn into_compile_error(self) -> TokenStream2 { | ||
let msg = self.to_string(); | ||
let span = self.get_location(); | ||
syn::Error::new(span, msg).into_compile_error() | ||
} | ||
} | ||
|
||
/// Error attempting to load metadata. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum FetchMetadataError { | ||
#[error("Cannot decode hex value: {0}")] | ||
DecodeError(#[from] hex::FromHexError), | ||
#[error("Request error: {0}")] | ||
RequestError(#[from] jsonrpsee::core::Error), | ||
#[error("'{0}' not supported, supported URI schemes are http, https, ws or wss.")] | ||
InvalidScheme(String), | ||
} | ||
|
||
/// Error attempting to do type substitution. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum TypeSubstitutionError { | ||
/// Substitute "to" type must be an absolute path. | ||
#[error("`substitute_type(with = <path>)` must be a path prefixed with 'crate::' or '::'")] | ||
ExpectedAbsolutePath(Span), | ||
/// Substitute types must have a valid path. | ||
#[error("Substitute types must have a valid path.")] | ||
EmptySubstitutePath(Span), | ||
/// From/To substitution types should use angle bracket generics. | ||
#[error("Expected the from/to type generics to have the form 'Foo<A,B,C..>'.")] | ||
ExpectedAngleBracketGenerics(Span), | ||
jsdw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Source substitute type must be an ident. | ||
#[error("Expected an ident like 'Foo' or 'A' to mark a type to be substituted.")] | ||
InvalidFromType(Span), | ||
/// Target type is invalid. | ||
#[error("Expected an ident like 'Foo' or an absolute concrete path like '::path::to::Bar' for the substitute type.")] | ||
InvalidToType(Span), | ||
/// Target ident doesn't correspond to any source type. | ||
#[error("Cannot find matching param on 'from' type.")] | ||
NoMatchingFromType(Span), | ||
} | ||
|
||
impl TypeSubstitutionError { | ||
/// Fetch the location for this error. | ||
// Todo: Probably worth storing location outside of the variant, | ||
// so that there's a common way to set a location for some error. | ||
fn get_location(&self) -> Span { | ||
match self { | ||
TypeSubstitutionError::ExpectedAbsolutePath(span) => *span, | ||
TypeSubstitutionError::EmptySubstitutePath(span) => *span, | ||
TypeSubstitutionError::ExpectedAngleBracketGenerics(span) => *span, | ||
TypeSubstitutionError::InvalidFromType(span) => *span, | ||
TypeSubstitutionError::InvalidToType(span) => *span, | ||
TypeSubstitutionError::NoMatchingFromType(span) => *span, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can you file an issue this?