playwright_core/protocol/file_payload.rs
1// FilePayload protocol type
2//
3// Represents a file to be uploaded with explicit name, MIME type, and buffer.
4//
5// See: https://playwright.dev/docs/api/class-locator#locator-set-input-files
6
7/// FilePayload represents a file for advanced file uploads.
8///
9/// Allows explicit control over filename, MIME type, and file contents
10/// when uploading files to input elements.
11///
12/// # Example
13///
14/// ```ignore
15/// # use playwright_core::protocol::FilePayload;
16/// let file = FilePayload::builder()
17/// .name("document.pdf".to_string())
18/// .mime_type("application/pdf".to_string())
19/// .buffer(vec![/* PDF bytes */])
20/// .build();
21/// ```
22///
23/// See: <https://playwright.dev/docs/api/class-locator#locator-set-input-files>
24#[derive(Debug, Clone)]
25pub struct FilePayload {
26 /// File name
27 pub name: String,
28 /// MIME type
29 pub mime_type: String,
30 /// File contents as bytes
31 pub buffer: Vec<u8>,
32}
33
34impl FilePayload {
35 /// Creates a new builder for FilePayload
36 pub fn builder() -> FilePayloadBuilder {
37 FilePayloadBuilder::default()
38 }
39}
40
41/// Builder for FilePayload
42#[derive(Debug, Clone, Default)]
43pub struct FilePayloadBuilder {
44 name: Option<String>,
45 mime_type: Option<String>,
46 buffer: Option<Vec<u8>>,
47}
48
49impl FilePayloadBuilder {
50 /// Sets the file name
51 pub fn name(mut self, name: String) -> Self {
52 self.name = Some(name);
53 self
54 }
55
56 /// Sets the MIME type
57 pub fn mime_type(mut self, mime_type: String) -> Self {
58 self.mime_type = Some(mime_type);
59 self
60 }
61
62 /// Sets the file buffer (contents as bytes)
63 pub fn buffer(mut self, buffer: Vec<u8>) -> Self {
64 self.buffer = Some(buffer);
65 self
66 }
67
68 /// Builds the FilePayload
69 ///
70 /// # Panics
71 ///
72 /// Panics if any required field (name, mime_type, buffer) is missing
73 pub fn build(self) -> FilePayload {
74 FilePayload {
75 name: self.name.expect("name is required for FilePayload"),
76 mime_type: self
77 .mime_type
78 .expect("mime_type is required for FilePayload"),
79 buffer: self.buffer.expect("buffer is required for FilePayload"),
80 }
81 }
82}