#form-data #derive #web #multipart #multer

multer-derive

提供从 multer Multipart 构建类型的 derive。

2 个版本

0.1.1-alpha2023 年 5 月 5 日
0.1.0-alpha2023 年 5 月 3 日

#1045 in Rust 模式

Download history 16/week @ 2024-03-16 32/week @ 2024-03-23 71/week @ 2024-03-30 23/week @ 2024-04-06 15/week @ 2024-04-13 18/week @ 2024-04-20 12/week @ 2024-04-27 14/week @ 2024-05-04 15/week @ 2024-05-11 20/week @ 2024-05-18 20/week @ 2024-05-25 18/week @ 2024-06-01 12/week @ 2024-06-08 15/week @ 2024-06-15 25/week @ 2024-06-22 4/week @ 2024-06-29

60 每月下载量
用于 8 个 crate (2 直接)

MIT 许可证

48KB
1K SLoC

multer-derive

CI-badge Latest Version Docs Badge

提供从 multer::Multipart 构建类型的 FromMultipart derive。

使用方法

use multer_derive::{FormFile, FromMultipart, Multipart, MultipartForm};

#[derive(FormMultipart)]
struct Person {
    name: String,
    email: String,
    age: u8,
    married: bool,
    photo: FormFile
}

const FORM_DATA : &str = "--boundary_string\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nJohn Smith\r\n--boundary_string\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\n[email protected]\r\n--boundary_string\r\nContent-Disposition: form-data; name=\"age\"\r\n\r\n25\r\n--boundary_string\r\nContent-Disposition: form-data; name=\"married\"\r\n\r\ntrue\r\n--boundary_string\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"example.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n[Binary data]\r\n--boundary_string--\r\n";

let reader = FORM_DATA.as_bytes();
let multipart = Multipart::with_reader(reader, "boundary_string");

let form = MultipartForm::with_multipart(multipart).await.unwrap();
let person = Person::from_multipart(form).unwrap();

assert_eq!(person.name, "John Smith");
assert_eq!(person.email, "[email protected]");
assert_eq!(person.age, 25);
assert_eq!(person.married, true);

let str = String::from_utf8(person.photo.bytes().to_vec()).unwrap();
assert_eq!(str, "[Binary data]");

属性

multer-derive 还支持以下属性来装饰您的字段

  • 要重命名目标,可以使用 #[multer(rename = "new_field_name")]
    • 这将使 multer 使用给定的名称解析字段。

示例

use multer_derive::FromMultipart;

#[derive(FromMultipart)]
struct MyStruct {
    #[multer(rename = "active")]
    is_active: bool
}
  • 要使用函数解析,可以使用 #[multer(with = "path::to::function")]

    • 为此,您应该提供一个具有以下签名的函数
    fn from_multipart(multipart: &MultipartForm, ctx: FormContext<'_>) -> Result<YourType, Error> {
      todo!()
    }
    

示例

use multer_derive::{FromMultipart, MultipartForm, FormContext, Error};

#[derive(FromMultipart)]
struct MyStruct {
    #[multer(with = "text_from_multipart")]
    name: Text
}

struct Text(String);

fn text_from_multipart(
    multipart: &MultipartForm,
    ctx: FormContext<'_>,
) -> Result<Text, Error> {
    // This is safe, the `field_name` is always passed
    let field_name = ctx.field_name.unwrap();

    // We search the field in the source multipart
    let field = multipart
        .get_by_name(field_name)
        .ok_or(Error::new(format!(
            "`{field_name}` form field was not found"
        )))?;

    // Parse the value using `String`
    let s = String::from_field(field)?;
    Ok(Text(s))
}

依赖关系

~7–10MB
~233K SLoC