Skip to main content

pdfkit/
action_reset_form.rs

1use std::ptr;
2
3use crate::action::{sealed, PdfActionLike};
4use crate::error::{PdfKitError, Result};
5use crate::ffi;
6use crate::handle::ObjectHandle;
7use crate::util::{c_string, parse_json, take_string};
8
9/// Wraps `PDFActionResetForm`.
10#[derive(Debug, Clone)]
11pub struct PdfActionResetForm {
12    handle: ObjectHandle,
13}
14
15impl PdfActionResetForm {
16    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
17        Self { handle }
18    }
19
20    /// Wraps `PDFActionResetForm()`.
21    pub fn new() -> Result<Self> {
22        let mut out_action = ptr::null_mut();
23        let mut out_error = ptr::null_mut();
24        let status = unsafe { ffi::pdf_action_reset_form_new(&mut out_action, &mut out_error) };
25        crate::util::status_result(status, out_error)?;
26        Ok(Self::from_handle(crate::util::required_handle(
27            out_action,
28            "PDFActionResetForm",
29        )?))
30    }
31
32    /// Wraps the corresponding `PDFActionResetForm` API.
33    pub fn fields(&self) -> Result<Vec<String>> {
34        parse_json(
35            unsafe { ffi::pdf_action_reset_form_fields_json(self.handle.as_ptr()) },
36            "PDFActionResetForm fields",
37        )
38    }
39
40    /// Wraps the corresponding `PDFActionResetForm` API.
41    pub fn set_fields<I, S>(&self, fields: I) -> Result<()>
42    where
43        I: IntoIterator<Item = S>,
44        S: AsRef<str>,
45    {
46        let fields = fields
47            .into_iter()
48            .map(|field| field.as_ref().to_string())
49            .collect::<Vec<_>>();
50        let fields_json = serde_json::to_string(&fields).map_err(|error| {
51            PdfKitError::new(
52                ffi::status::FRAMEWORK,
53                format!("failed to encode PDFActionResetForm fields: {error}"),
54            )
55        })?;
56        let fields_json = c_string(&fields_json)?;
57        let mut out_error = ptr::null_mut();
58        let status = unsafe {
59            ffi::pdf_action_reset_form_set_fields_json(
60                self.handle.as_ptr(),
61                fields_json.as_ptr(),
62                &mut out_error,
63            )
64        };
65        crate::util::status_result(status, out_error)
66    }
67
68    /// Wraps the corresponding `PDFActionResetForm` API.
69    pub fn clear_fields(&self) -> Result<()> {
70        let mut out_error = ptr::null_mut();
71        let status = unsafe {
72            ffi::pdf_action_reset_form_set_fields_json(
73                self.handle.as_ptr(),
74                ptr::null(),
75                &mut out_error,
76            )
77        };
78        crate::util::status_result(status, out_error)
79    }
80
81    /// Wraps the corresponding `PDFActionResetForm` API.
82    #[must_use]
83    pub fn fields_included_are_cleared(&self) -> bool {
84        unsafe { ffi::pdf_action_reset_form_fields_included_are_cleared(self.handle.as_ptr()) != 0 }
85    }
86
87    /// Wraps the corresponding `PDFActionResetForm` API.
88    pub fn set_fields_included_are_cleared(&self, value: bool) {
89        unsafe {
90            ffi::pdf_action_reset_form_set_fields_included_are_cleared(
91                self.handle.as_ptr(),
92                i32::from(value),
93            );
94        }
95    }
96
97    /// Wraps the corresponding `PDFActionResetForm` API.
98    #[must_use]
99    pub fn action_type(&self) -> Option<String> {
100        take_string(unsafe { ffi::pdf_action_reset_form_type_string(self.handle.as_ptr()) })
101    }
102
103    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
104        self.handle.as_ptr()
105    }
106}
107
108impl sealed::Sealed for PdfActionResetForm {}
109
110impl PdfActionLike for PdfActionResetForm {
111    fn as_action_handle_ptr(&self) -> *mut core::ffi::c_void {
112        self.as_handle_ptr()
113    }
114}