Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit a4e7c3f

Browse files
pdf: Return Result from most functions
Most of cairo_pdf_surface_*() can set the surface to an error state, due to their internal call to _extract_pdf_surface(), which checks a few conditions with the surface itself and the paginated surface.
1 parent b354658 commit a4e7c3f

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/pdf.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,28 @@ impl PdfSurface {
5959
vers_slice.iter().map(|v| PdfVersion::from(*v))
6060
}
6161

62-
pub fn restrict(&self, version: PdfVersion) {
62+
pub fn restrict(&self, version: PdfVersion) -> Result<(), Status> {
6363
unsafe {
6464
ffi::cairo_pdf_surface_restrict_to_version(self.inner.to_raw_none(), version.into());
6565
}
66+
match self.status() {
67+
Status::Success => Ok(()),
68+
s => Err(s),
69+
}
6670
}
6771

68-
pub fn set_size(&self, width: f64, height: f64) {
72+
pub fn set_size(&self, width: f64, height: f64) -> Result<(), Status> {
6973
unsafe {
7074
ffi::cairo_pdf_surface_set_size(self.inner.to_raw_none(), width, height);
7175
}
76+
match self.status() {
77+
Status::Success => Ok(()),
78+
s => Err(s),
79+
}
7280
}
7381

7482
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
75-
pub fn set_metadata(&self, metadata: PdfMetadata, value: &str) {
83+
pub fn set_metadata(&self, metadata: PdfMetadata, value: &str) -> Result<(), Status> {
7684
let value = CString::new(value).unwrap();
7785
unsafe {
7886
ffi::cairo_pdf_surface_set_metadata(
@@ -81,25 +89,37 @@ impl PdfSurface {
8189
value.as_ptr(),
8290
);
8391
}
92+
match self.status() {
93+
Status::Success => Ok(()),
94+
s => Err(s),
95+
}
8496
}
8597

8698
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
87-
pub fn set_page_label(&self, label: &str) {
99+
pub fn set_page_label(&self, label: &str) -> Result<(), Status> {
88100
let label = CString::new(label).unwrap();
89101
unsafe {
90102
ffi::cairo_pdf_surface_set_page_label(self.inner.to_raw_none(), label.as_ptr());
91103
}
104+
match self.status() {
105+
Status::Success => Ok(()),
106+
s => Err(s),
107+
}
92108
}
93109

94110
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
95-
pub fn set_thumbnail_size(&self, width: i32, height: i32) {
111+
pub fn set_thumbnail_size(&self, width: i32, height: i32) -> Result<(), Status> {
96112
unsafe {
97113
ffi::cairo_pdf_surface_set_thumbnail_size(
98114
self.inner.to_raw_none(),
99115
width as _,
100116
height as _,
101117
);
102118
}
119+
match self.status() {
120+
Status::Success => Ok(()),
121+
s => Err(s),
122+
}
103123
}
104124

105125
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
@@ -109,18 +129,23 @@ impl PdfSurface {
109129
name: &str,
110130
link_attribs: &str,
111131
flags: PdfOutline,
112-
) -> i32 {
132+
) -> Result<i32, Status> {
113133
let name = CString::new(name).unwrap();
114134
let link_attribs = CString::new(link_attribs).unwrap();
115135

116-
unsafe {
136+
let res = unsafe {
117137
ffi::cairo_pdf_surface_add_outline(
118138
self.inner.to_raw_none(),
119139
parent_id,
120140
name.as_ptr(),
121141
link_attribs.as_ptr(),
122142
flags.bits() as _,
123143
) as _
144+
};
145+
146+
match self.status() {
147+
Status::Success => Ok(res),
148+
s => Err(s),
124149
}
125150
}
126151
}
@@ -279,7 +304,7 @@ mod test {
279304
let custom_writer = CustomWriter(0);
280305

281306
let surface = PdfSurface::for_stream(20., 20., custom_writer).unwrap();
282-
surface.set_size(100., 100.);
307+
surface.set_size(100., 100.).unwrap();
283308
draw(&surface);
284309
let stream = surface.finish_output_stream().unwrap();
285310
let custom_writer = stream.downcast::<CustomWriter>().unwrap();

0 commit comments

Comments
 (0)