murar8 / axum_typed_multipart

Type safe multipart/form-data handling for axum.
84 stars 14 forks source link

Implement `rename_all` rule and add `TryFromField` macro #41

Closed nerodono closed 1 year ago

nerodono commented 1 year ago

This pull requests implements rename_all rule for both TryFromMultipart and TryFromField (new) macros, for example:

TryFromMultipart

#[derive(TryFromMultipart)]
#[try_from_multipart(rename_all = "UPPERCASE")]
struct Person {
    name: String,
    age: u16,
}

it is equivalent to passing #[form_data(field_name = <uppercase name of the field>)]

TryFromField

New macro, that can be used to automatically derive TryFromField for arbitrary user-specified unit-enum:

use axum_typed_multipart::{TryFromField, TryFromMultipart};

#[derive(TryFromField)]
enum Sex {
    Male,
    Female,
}

#[derive(TryFromMultipart)]
struct Person {
    name: String,
    age: u16,
    sex: Sex,
}

Under the hood it just loads String using its TryFromField implementation and maps string to corresponding enum variant, renaming is also available through the #[try_from_field(rename_all = "case")] and for each individual field via #[field(rename = "name")].

Also I edited documentation to mention new functionality. Hoping it would be useful!

codecov[bot] commented 1 year ago

Codecov Report

Merging #41 (b7b383b) into main (b5b3eca) will decrease coverage by 7.52%. The diff coverage is 83.25%.

@@            Coverage Diff             @@
##             main      #41      +/-   ##
==========================================
- Coverage   96.73%   89.22%   -7.52%     
==========================================
  Files           8       11       +3     
  Lines         245      334      +89     
==========================================
+ Hits          237      298      +61     
- Misses          8       36      +28     
Files Changed Coverage Δ
macros/src/case_conversion.rs 25.00% <25.00%> (ø)
macros/src/impls/try_from_field.rs 89.13% <89.13%> (ø)
macros/src/impls/try_from_multipart.rs 93.33% <93.33%> (ø)
macros/src/lib.rs 100.00% <100.00%> (+5.17%) :arrow_up:
macros/src/util.rs 95.00% <100.00%> (+1.66%) :arrow_up:
murar8 commented 1 year ago

Don't mind the test coverage BTW, it will be fixed when the branch is rebased, same goes for the required checks.

murar8 commented 1 year ago

Thanks man will run the status checks and take a look but I think we are good to merge

nerodono commented 1 year ago

Looks good man we are only missing a unit test suite for the case_conversion module and I think we are good to merge. Let me know if you have time to add it otherwise I can merge and add the tests myself.

Yep, I'll do it right now

nerodono commented 1 year ago

Looks good man we are only missing a unit test suite for the case_conversion module and I think we are good to merge. Let me know if you have time to add it otherwise I can merge and add the tests myself.

Done

murar8 commented 1 year ago

I think the coverage is not picking up the tests in macros folder, will merge anyway and take a look.