Skip to content

Commit

Permalink
[schema] #1897: Removed usize/isize from serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <[email protected]>
  • Loading branch information
SamHSmith committed Mar 24, 2022
1 parent 3bc52c2 commit ec59b12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 15 additions & 2 deletions schema/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::quote;
use quote::{quote, ToTokens};
use syn::{
parse::Parse, parse_macro_input, parse_quote, spanned::Spanned, Attribute, Data, DataEnum,
DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed, FieldsUnnamed, GenericParam,
Expand All @@ -27,7 +27,20 @@ pub fn schema_derive(input: TokenStream) -> TokenStream {
.push(parse_quote! {iroha_schema::IntoSchema})
});

impl_schema(&input).into()
let mut output: TokenStream = impl_schema(&input).into();

let input_string = format!("{}", input.to_token_stream());
if input_string.contains("usize") {
let to_add_tokens: TokenStream =
quote!(compile_error!("You may not use usize with IntoSchema");).into();
output.extend(to_add_tokens);
}
if input_string.contains("isize") {
let to_add_tokens: TokenStream =
quote!(compile_error!("You may not use isize with IntoSchema");).into();
output.extend(to_add_tokens);
}
output
}

fn impl_schema(input: &DeriveInput) -> TokenStream2 {
Expand Down
10 changes: 5 additions & 5 deletions schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait IntoSchema {
/// Applicable for types that represents decimal place of fixed point
pub trait DecimalPlacesAware {
/// decimal places of fixed point
fn decimal_places() -> usize;
fn decimal_places() -> u32;
}

/// Metadata
Expand Down Expand Up @@ -85,7 +85,7 @@ pub struct ArrayMeta {
/// Type
pub ty: String,
/// Length
pub len: usize,
pub len: u64,
}

/// Named fields
Expand Down Expand Up @@ -167,7 +167,7 @@ pub struct Compact<T>(T);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct FixedMeta {
base: String,
decimal_places: usize,
decimal_places: u32,
}

macro_rules! impl_schema_int {
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<I: IntoSchema, P: DecimalPlacesAware> IntoSchema for fixnum::FixedPoint<I,
}

impl DecimalPlacesAware for fixnum::typenum::U9 {
fn decimal_places() -> usize {
fn decimal_places() -> u32 {
9
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ impl<T: IntoSchema, const L: usize> IntoSchema for [T; L] {
let _ = map.entry(Self::type_name()).or_insert_with(|| {
Metadata::Array(ArrayMeta {
ty: T::type_name(),
len: L,
len: L.try_into().expect("usize should always fit in u64"),
})
});
if !map.contains_key(&T::type_name()) {
Expand Down

0 comments on commit ec59b12

Please sign in to comment.