upload_things/
record.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Deserialize, Serialize)]
pub struct Metadata {
    #[serde(rename = "uploadedBy")]
    uploaded_by: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct UtRecord {
    #[serde(rename = "fileKeys")]
    pub file_keys: Vec<String>,
    pub metadata: Metadata,
    #[serde(rename = "callbackUrl")]
    pub callback_url: String,
    #[serde(rename = "callbackSlug")]
    pub callback_slug: String,
    #[serde(rename = "awaitServerData")]
    pub await_server_data: bool,
    #[serde(rename = "isDev")]
    pub is_dev: bool,
}
impl Display for UtRecord {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap())
    }
}
impl Default for UtRecord {
    fn default() -> Self {
        Self {
            file_keys: Vec::new(),
            metadata: Metadata {
                uploaded_by: "anonymous".to_string(),
            },
            callback_url: std::env::var("CALLBACK_DOMAIN")
                .expect("CALLBACK_DOMAIN has not been set"),
            callback_slug: "imageUploader".to_string(),
            await_server_data: false,
            is_dev: false,
        }
    }
}
impl TryFrom<&web_sys::wasm_bindgen::JsValue> for UtRecord {
    type Error = web_sys::wasm_bindgen::JsValue;
    fn try_from(value: &web_sys::wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
        let js_str = web_sys::js_sys::JSON::stringify(value)?;
        let str = js_str
            .as_string()
            .ok_or_else(|| web_sys::wasm_bindgen::JsValue::from_str("Failed to stringify JSON"))?;
        let presigned_url: Self = serde_json::from_str(&str).map_err(|e| {
            web_sys::wasm_bindgen::JsValue::from_str(&format!("Failed to parse JSON: {}", e))
        })?;
        Ok(presigned_url)
    }
}
impl TryFrom<web_sys::wasm_bindgen::JsValue> for UtRecord {
    type Error = web_sys::wasm_bindgen::JsValue;
    fn try_from(value: web_sys::wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}