Skip to content

Commit

Permalink
Add zero check
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 25, 2024
1 parent c90c014 commit 1312c00
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ fn expand_attributes(func: &mut ItemFn) -> syn::Result<TokenStream> {
}

let version: syn::LitInt = attribute.parse_args()?;
// Enforce that the version is in range of a u64
version.base10_parse::<u64>()?;
// Enforce that the version is a valid u64 and non-zero
if version.base10_parse::<u64>()? == 0 {
return Err(syn::Error::new_spanned(
version,
"please start versioning with 1",
));
}

let version = version.base10_digits();

stream = quote! {
Expand Down Expand Up @@ -193,7 +199,24 @@ mod test {
use crate::entry_point_impl;

#[test]
fn contract_state_version_on_non_migratee() {
fn contract_state_zero_not_allowed() {
let code = quote! {
#[state_version(0)]
fn migrate() -> Response {
// Logic here
}
};

let actual = entry_point_impl(TokenStream::new(), code);
let expected = quote! {
::core::compile_error! { "please start versioning with 1" }
};

assert_eq!(actual.to_string(), expected.to_string());
}

#[test]
fn contract_state_version_on_non_migrate() {
let code = quote! {
#[state_version(42)]
fn anything_else() -> Response {
Expand Down

0 comments on commit 1312c00

Please sign in to comment.