I’m implementing a unified parameter extractor Params for axum that can handle parameters from Path/Query/Form/Multipart. I have a custom DOM-like type that stores both JSON data and uploaded files:
pub struct UploadFile {
pub file_name: String,
pub temp_file: Arc<NamedTempFile>,
}
pub enum ParamsValue {
Object(HashMap<String, ParamsValue>),
Array(Vec<ParamsValue>),
Json(serde_json::Value),
UploadFile(Box<UploadFile>),
}
I need to deserialize ParamsValue into a struct like this:
#[derive(Deserialize)]
struct FileUploadParams {
title: String,
file: UploadFile,
}
I know it can’t be serialize to and deserialize from bytes likes JSON, I just wan’t use it to ser/de between DOM and struct.
The challenge is that the UploadFile field(or just temp_file
field) needs to be assigned directly from ParamsValue::UploadFile variant rather than being deserialized. I’ve tried implementing Deserialize/Deserializer traits for both ParamsValue and FileUpload, but can’t get it working.
Any suggestions on how to implement this custom deserialization properly?
1