Skip to content

Commit

Permalink
refactor: simplify several code definitions
Browse files Browse the repository at this point in the history
Besides minor code cleanups and deduplications, an explicit lifetime
could often be elided.
  • Loading branch information
dnaka91 committed Nov 16, 2023
1 parent a9e0800 commit b166664
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 60 deletions.
6 changes: 2 additions & 4 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn compile_schema(opts: &Opts, Schema { definitions, .. }: &Schema<'_>) -> T
}

fn compile_definition(opts: &Opts, definition: &Definition<'_>) -> TokenStream {
let definition = match definition {
match definition {
Definition::Module(m) => compile_module(opts, m),
Definition::Struct(s) => {
let def = compile_struct(opts, s);
Expand All @@ -48,9 +48,7 @@ fn compile_definition(opts: &Opts, definition: &Definition<'_>) -> TokenStream {
Definition::TypeAlias(a) => compile_alias(opts, a),
Definition::Const(c) => compile_const(c),
Definition::Import(i) => compile_import(i),
};

quote! { #definition }
}
}

fn compile_module(
Expand Down
13 changes: 5 additions & 8 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ fn compile_data_type(opts: &Opts, ty: &Type<'_>, name: TokenStream) -> TokenStre
DataType::I128 => quote! { ::stef::buf::encode_i128(w, #name) },
DataType::F32 => quote! { ::stef::buf::encode_f32(w, #name) },
DataType::F64 => quote! { ::stef::buf::encode_f64(w, #name) },
DataType::String | DataType::StringRef => quote! { ::stef::buf::encode_string(w, &#name) },
DataType::Bytes | DataType::BytesRef => match opts.bytes_type {
DataType::String | DataType::StringRef | DataType::BoxString => {
quote! { ::stef::buf::encode_string(w, &#name) }
}
DataType::Bytes | DataType::BytesRef | DataType::BoxBytes => match opts.bytes_type {
BytesType::VecU8 => quote! { ::stef::buf::encode_bytes_std(w, &#name) },
BytesType::Bytes => quote! { ::stef::buf::encode_bytes_bytes(w, &#name) },
},
Expand Down Expand Up @@ -352,9 +354,6 @@ fn compile_data_type(opts: &Opts, ty: &Type<'_>, name: TokenStream) -> TokenStre
| DataType::HashSet(_) => compile_data_type(opts, ty, quote! { #name.get() }),
ty => todo!("compiler should catch invalid {ty:?} type"),
},

DataType::BoxString => quote! { ::stef::buf::encode_string(w, &*#name) },
DataType::BoxBytes => quote! { ::stef::buf::encode_bytes_std(w, &*#name) },
DataType::Tuple(types) => match types.len() {
2..=12 => {
let types = types.iter().enumerate().map(|(idx, ty)| {
Expand All @@ -371,9 +370,7 @@ fn compile_data_type(opts: &Opts, ty: &Type<'_>, name: TokenStream) -> TokenStre
});
quote! { #(#types;)* }
}
0 => panic!("tuple with zero elements"),
1 => panic!("tuple with single element"),
_ => panic!("tuple with more than 12 elements"),
n => todo!("compiler should catch invalid tuple with {n} elements"),
},
DataType::Array(ty, _size) => {
let ty = compile_data_type(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ impl ::stef::Encode for Sample {
w,
18,
|w| {
::stef::buf::encode_string(w, &*self.f18);
::stef::buf::encode_string(w, &self.f18);
},
);
::stef::buf::encode_field(
w,
19,
|w| {
::stef::buf::encode_bytes_std(w, &*self.f19);
::stef::buf::encode_bytes_std(w, &self.f19);
},
);
::stef::buf::encode_field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl ::stef::Encode for Sample {
::stef::buf::encode_i64(w, *k);
},
|w, v| {
::stef::buf::encode_string(w, &*v);
::stef::buf::encode_string(w, &v);
},
);
},
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-compiler/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(crate) enum ResolvedImport<'a> {
},
}

impl<'a> Module<'a> {
impl Module<'_> {
fn resolve_local(&self, ty: &ExternalType<'_>) -> Result<(), ResolveLocal> {
let module = if ty.path.is_empty() {
self
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-compiler/tests/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> Wrapper<'a> {
}
}

impl<'a> Display for Wrapper<'a> {
impl Display for Wrapper<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.debug(self.1, f)
}
Expand Down
Loading

0 comments on commit b166664

Please sign in to comment.