xai_rust/models/
videos.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize)]
7pub struct VideoGenerationRequest {
8 pub model: String,
10 pub prompt: String,
12 #[serde(skip_serializing_if = "Option::is_none")]
14 pub duration_seconds: Option<u32>,
15 #[serde(skip_serializing_if = "Option::is_none")]
17 pub response_format: Option<VideoResponseFormat>,
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub n: Option<u32>,
21}
22
23impl VideoGenerationRequest {
24 pub fn new(model: impl Into<String>, prompt: impl Into<String>) -> Self {
26 Self {
27 model: model.into(),
28 prompt: prompt.into(),
29 duration_seconds: None,
30 response_format: None,
31 n: None,
32 }
33 }
34
35 pub fn duration_seconds(mut self, duration_seconds: u32) -> Self {
37 self.duration_seconds = Some(duration_seconds);
38 self
39 }
40
41 pub fn response_format(mut self, format: VideoResponseFormat) -> Self {
43 self.response_format = Some(format);
44 self
45 }
46
47 pub fn n(mut self, n: u32) -> Self {
49 self.n = Some(n);
50 self
51 }
52}
53
54#[derive(Debug, Clone, Serialize)]
56pub struct VideoEditRequest {
57 pub model: String,
59 pub video: String,
61 pub prompt: String,
63 #[serde(skip_serializing_if = "Option::is_none")]
65 pub duration_seconds: Option<u32>,
66 #[serde(skip_serializing_if = "Option::is_none")]
68 pub response_format: Option<VideoResponseFormat>,
69}
70
71impl VideoEditRequest {
72 pub fn new(
74 model: impl Into<String>,
75 video: impl Into<String>,
76 prompt: impl Into<String>,
77 ) -> Self {
78 Self {
79 model: model.into(),
80 video: video.into(),
81 prompt: prompt.into(),
82 duration_seconds: None,
83 response_format: None,
84 }
85 }
86
87 pub fn duration_seconds(mut self, duration_seconds: u32) -> Self {
89 self.duration_seconds = Some(duration_seconds);
90 self
91 }
92
93 pub fn response_format(mut self, format: VideoResponseFormat) -> Self {
95 self.response_format = Some(format);
96 self
97 }
98}
99
100#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
102#[serde(rename_all = "snake_case")]
103pub enum VideoResponseFormat {
104 #[default]
106 Url,
107 B64Json,
109}
110
111#[derive(Debug, Clone, Deserialize)]
113pub struct VideoGenerationResponse {
114 pub data: Vec<VideoData>,
116 #[serde(default)]
118 pub created: Option<i64>,
119}
120
121impl VideoGenerationResponse {
122 pub fn first_url(&self) -> Option<&str> {
124 self.data.first().and_then(|v| v.url.as_deref())
125 }
126
127 pub fn first_base64(&self) -> Option<&str> {
129 self.data.first().and_then(|v| v.b64_json.as_deref())
130 }
131}
132
133#[derive(Debug, Clone, Deserialize)]
135pub struct VideoData {
136 #[serde(default)]
138 pub url: Option<String>,
139 #[serde(default)]
141 pub b64_json: Option<String>,
142}
143
144#[derive(Debug, Clone, Deserialize)]
146pub struct Video {
147 pub id: String,
149 #[serde(default)]
151 pub status: Option<String>,
152 #[serde(default)]
154 pub url: Option<String>,
155 #[serde(default)]
157 pub object: String,
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn video_generation_request_helpers() {
166 let request = VideoGenerationRequest::new("grok-video", "A cat")
167 .duration_seconds(4)
168 .response_format(VideoResponseFormat::Url)
169 .n(2);
170
171 assert_eq!(request.model, "grok-video");
172 assert_eq!(request.duration_seconds, Some(4));
173 assert_eq!(request.n, Some(2));
174 assert_eq!(request.response_format, Some(VideoResponseFormat::Url));
175 }
176
177 #[test]
178 fn video_edit_request_helpers() {
179 let request = VideoEditRequest::new("grok-video", "video-id", "slow motion")
180 .duration_seconds(4)
181 .response_format(VideoResponseFormat::B64Json);
182 assert_eq!(request.video, "video-id");
183 assert_eq!(request.duration_seconds, Some(4));
184 }
185
186 #[test]
187 fn video_response_helpers() {
188 let response = VideoGenerationResponse {
189 created: Some(1),
190 data: vec![VideoData {
191 url: Some("https://example.com/video.mp4".to_string()),
192 b64_json: Some("b64".to_string()),
193 }],
194 };
195 assert_eq!(response.first_url(), Some("https://example.com/video.mp4"));
196 }
197}