meru_interface/
config.rs

1use schemars::{
2    gen::SchemaGenerator,
3    schema::{Schema, SchemaObject},
4    JsonSchema,
5};
6use std::path::{Path, PathBuf};
7
8#[cfg(not(target_arch = "wasm32"))]
9mod imp {
10    use serde::{Deserialize, Serialize};
11    use std::path::PathBuf;
12
13    #[derive(Clone, Serialize, Deserialize)]
14    #[serde(from = "String", into = "String")]
15    pub struct File {
16        pub(crate) path: PathBuf,
17    }
18
19    impl From<String> for File {
20        fn from(s: String) -> Self {
21            File {
22                path: PathBuf::from(s),
23            }
24        }
25    }
26
27    impl From<File> for String {
28        fn from(f: File) -> Self {
29            f.path.to_string_lossy().to_string()
30        }
31    }
32}
33
34#[cfg(target_arch = "wasm32")]
35mod imp {
36    use base64::STANDARD;
37    use serde::{Deserialize, Serialize};
38    use std::path::PathBuf;
39
40    #[derive(Clone, Serialize, Deserialize)]
41    pub struct File {
42        pub(crate) path: PathBuf,
43        #[serde(with = "Base64Standard")]
44        pub(crate) data: Vec<u8>,
45    }
46
47    base64_serde_type!(Base64Standard, STANDARD);
48}
49
50pub use imp::File;
51
52impl JsonSchema for File {
53    fn schema_name() -> String {
54        "File".to_string()
55    }
56
57    fn json_schema(gen: &mut SchemaGenerator) -> Schema {
58        let mut schema: SchemaObject = <String>::json_schema(gen).into();
59        schema.format = Some("file".to_owned());
60        schema.into()
61    }
62}
63
64impl File {
65    #[allow(unused_variables)]
66    pub fn new(path: PathBuf, data: Vec<u8>) -> Self {
67        File {
68            path,
69            #[cfg(target_arch = "wasm32")]
70            data,
71        }
72    }
73
74    pub fn path(&self) -> &Path {
75        &self.path
76    }
77
78    #[cfg(not(target_arch = "wasm32"))]
79    pub fn data(&self) -> Result<Vec<u8>, std::io::Error> {
80        std::fs::read(&self.path)
81    }
82
83    #[cfg(target_arch = "wasm32")]
84    pub fn data(&self) -> Result<Vec<u8>, std::io::Error> {
85        Ok(self.data.clone())
86    }
87}