Skip to main content

pdfkit/
action_url.rs

1use std::ptr;
2
3use crate::action::{sealed, PdfActionLike};
4use crate::error::Result;
5use crate::ffi;
6use crate::handle::ObjectHandle;
7use crate::util::{c_string, take_string};
8
9#[derive(Debug, Clone)]
10pub struct PdfActionUrl {
11    handle: ObjectHandle,
12}
13
14impl PdfActionUrl {
15    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
16        Self { handle }
17    }
18
19    pub fn new(url: &str) -> Result<Self> {
20        let url = c_string(url)?;
21        let mut out_action = ptr::null_mut();
22        let mut out_error = ptr::null_mut();
23        let status =
24            unsafe { ffi::pdf_action_url_new(url.as_ptr(), &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            "PDFActionURL",
29        )?))
30    }
31
32    #[must_use]
33    pub fn url(&self) -> Option<String> {
34        take_string(unsafe { ffi::pdf_action_url_string(self.handle.as_ptr()) })
35    }
36
37    pub fn set_url(&self, url: &str) -> Result<()> {
38        let url = c_string(url)?;
39        let mut out_error = ptr::null_mut();
40        let status = unsafe {
41            ffi::pdf_action_url_set_url(self.handle.as_ptr(), url.as_ptr(), &mut out_error)
42        };
43        crate::util::status_result(status, out_error)
44    }
45
46    #[must_use]
47    pub fn action_type(&self) -> Option<String> {
48        take_string(unsafe { ffi::pdf_action_url_type_string(self.handle.as_ptr()) })
49    }
50
51    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
52        self.handle.as_ptr()
53    }
54}
55
56impl sealed::Sealed for PdfActionUrl {}
57
58impl PdfActionLike for PdfActionUrl {
59    fn as_action_handle_ptr(&self) -> *mut core::ffi::c_void {
60        self.as_handle_ptr()
61    }
62}