Skip to main content

openai_oxide/types/
file.rs

1// File types — mirrors openai-python types/file_object.py
2
3use serde::Deserialize;
4
5/// A file object from the API.
6#[derive(Debug, Clone, Deserialize)]
7pub struct FileObject {
8    pub id: String,
9    pub bytes: i64,
10    pub created_at: i64,
11    pub filename: String,
12    pub object: String,
13    pub purpose: String,
14    pub status: String,
15    #[serde(default)]
16    pub status_details: Option<String>,
17    #[serde(default)]
18    pub expires_at: Option<i64>,
19}
20
21/// Response from listing files.
22#[derive(Debug, Clone, Deserialize)]
23pub struct FileList {
24    pub object: String,
25    pub data: Vec<FileObject>,
26}
27
28/// Response from deleting a file.
29#[derive(Debug, Clone, Deserialize)]
30pub struct FileDeleted {
31    pub id: String,
32    pub deleted: bool,
33    pub object: String,
34}
35
36/// Parameters for file upload (multipart).
37#[derive(Debug)]
38pub struct FileUploadParams {
39    pub file: Vec<u8>,
40    pub filename: String,
41    pub purpose: String,
42}
43
44impl FileUploadParams {
45    pub fn new(file: Vec<u8>, filename: impl Into<String>, purpose: impl Into<String>) -> Self {
46        Self {
47            file,
48            filename: filename.into(),
49            purpose: purpose.into(),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_deserialize_file_object() {
60        let json = r#"{
61            "id": "file-abc123",
62            "object": "file",
63            "bytes": 120000,
64            "created_at": 1677610602,
65            "filename": "data.jsonl",
66            "purpose": "fine-tune",
67            "status": "processed"
68        }"#;
69        let file: FileObject = serde_json::from_str(json).unwrap();
70        assert_eq!(file.id, "file-abc123");
71        assert_eq!(file.bytes, 120000);
72        assert_eq!(file.purpose, "fine-tune");
73        assert_eq!(file.status, "processed");
74    }
75
76    #[test]
77    fn test_deserialize_file_deleted() {
78        let json = r#"{"id": "file-abc123", "object": "file", "deleted": true}"#;
79        let deleted: FileDeleted = serde_json::from_str(json).unwrap();
80        assert!(deleted.deleted);
81    }
82}