parse_rs/file.rs
1use serde::{Deserialize, Serialize};
2
3/// Represents a file field as stored in a ParseObject.
4/// This struct is used for serialization and deserialization
5/// when a ParseFile is part of a ParseObject.
6#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
7pub struct FileField {
8 pub name: String,
9 pub url: String,
10 #[serde(rename = "__type")]
11 pub _type: String, // Should always be "File"
12}
13
14impl FileField {
15 // Constructor for internal use, ensuring _type is always "File"
16 pub fn new(name: String, url: String) -> Self {
17 FileField {
18 name,
19 url,
20 _type: "File".to_string(),
21 }
22 }
23}
24
25/// Represents a file to be uploaded to Parse Server or a file
26/// that has already been uploaded.
27#[derive(Debug, Clone)]
28pub struct ParseFile {
29 /// The name of the file. This could be the original filename
30 /// or the name assigned by Parse Server upon upload.
31 pub name: String,
32 /// The URL of the file after it has been uploaded to Parse Server.
33 /// This will be `None` for a file that hasn't been uploaded yet.
34 pub url: Option<String>,
35 /// The MIME type of the file (e.g., "image/jpeg", "text/plain").
36 pub mime_type: String,
37 /// The raw byte data of the file.
38 pub data: Vec<u8>,
39}
40
41impl ParseFile {
42 /// Creates a new `ParseFile` instance with the given name, data, and MIME type.
43 /// The URL will be `None` initially and should be populated after uploading.
44 pub fn new(name: String, data: Vec<u8>, mime_type: String) -> Self {
45 ParseFile {
46 name,
47 url: None,
48 mime_type,
49 data,
50 }
51 }
52
53 /// Converts this `ParseFile` into a `FileField` suitable for embedding
54 /// within a `ParseObject`. Returns `None` if the file has not been uploaded
55 /// (i.e., if `url` is `None`).
56 pub fn to_field(&self) -> Option<FileField> {
57 self.url.as_ref().map(|u| FileField {
58 name: self.name.clone(),
59 url: u.clone(),
60 _type: "File".to_string(),
61 })
62 }
63}