Skip to main content

pdfkit/
action_goto.rs

1use std::ptr;
2
3use crate::action::{sealed, PdfActionLike};
4use crate::destination::PdfDestination;
5use crate::error::Result;
6use crate::ffi;
7use crate::handle::ObjectHandle;
8use crate::util::take_string;
9
10/// Wraps `PDFActionGoTo`.
11#[derive(Debug, Clone)]
12pub struct PdfActionGoTo {
13    handle: ObjectHandle,
14}
15
16impl PdfActionGoTo {
17    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
18        Self { handle }
19    }
20
21    /// Wraps `PDFActionGoTo(destination:)`.
22    pub fn new(destination: &PdfDestination) -> Result<Self> {
23        let mut out_action = ptr::null_mut();
24        let mut out_error = ptr::null_mut();
25        let status = unsafe {
26            ffi::pdf_action_goto_new(destination.as_handle_ptr(), &mut out_action, &mut out_error)
27        };
28        crate::util::status_result(status, out_error)?;
29        Ok(Self::from_handle(crate::util::required_handle(
30            out_action,
31            "PDFActionGoTo",
32        )?))
33    }
34
35    /// Wraps the corresponding `PDFActionGoTo` API.
36    #[must_use]
37    pub fn destination(&self) -> Option<PdfDestination> {
38        let ptr = unsafe { ffi::pdf_action_goto_destination(self.handle.as_ptr()) };
39        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfDestination::from_handle)
40    }
41
42    /// Wraps the corresponding `PDFActionGoTo` API.
43    pub fn set_destination(&self, destination: &PdfDestination) -> Result<()> {
44        let mut out_error = ptr::null_mut();
45        let status = unsafe {
46            ffi::pdf_action_goto_set_destination(
47                self.handle.as_ptr(),
48                destination.as_handle_ptr(),
49                &mut out_error,
50            )
51        };
52        crate::util::status_result(status, out_error)
53    }
54
55    /// Wraps the corresponding `PDFActionGoTo` API.
56    #[must_use]
57    pub fn action_type(&self) -> Option<String> {
58        take_string(unsafe { ffi::pdf_action_goto_type_string(self.handle.as_ptr()) })
59    }
60
61    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
62        self.handle.as_ptr()
63    }
64}
65
66impl sealed::Sealed for PdfActionGoTo {}
67
68impl PdfActionLike for PdfActionGoTo {
69    fn as_action_handle_ptr(&self) -> *mut core::ffi::c_void {
70        self.as_handle_ptr()
71    }
72}