Skip to content

Commit ef5692a

Browse files
itsjunetimevinxv
authored andcommitted
Apply cleanups suggested by clippy and plug it into CI (messense#111)
1 parent 541b0d1 commit ef5692a

File tree

11 files changed

+53
-55
lines changed

11 files changed

+53
-55
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
fetch-depth: 500
2424
- uses: dtolnay/rust-toolchain@stable
2525
- run: sudo apt-get -y install libfontconfig1-dev
26-
- run: cargo check
26+
- run: cargo clippy --tests -- -D warnings
2727

2828
test:
2929
name: Test Suite

src/buffer.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::convert::TryFrom;
22
use std::ffi::CString;
33
use std::io;
44
use std::ptr;
5+
use std::str::FromStr;
56

67
use mupdf_sys::*;
78

@@ -15,6 +16,15 @@ pub struct Buffer {
1516
offset: usize,
1617
}
1718

19+
impl FromStr for Buffer {
20+
type Err = Error;
21+
fn from_str(s: &str) -> Result<Self, Self::Err> {
22+
let c_str = CString::new(s)?;
23+
let inner = unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) };
24+
Ok(Self { inner, offset: 0 })
25+
}
26+
}
27+
1828
impl Buffer {
1929
pub(crate) unsafe fn from_raw(ptr: *mut fz_buffer) -> Self {
2030
Self {
@@ -27,12 +37,6 @@ impl Buffer {
2737
Self::with_capacity(0)
2838
}
2939

30-
pub fn from_str(str: &str) -> Result<Self, Error> {
31-
let c_str = CString::new(str)?;
32-
let inner = unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) };
33-
Ok(Self { inner, offset: 0 })
34-
}
35-
3640
pub fn from_base64(str: &str) -> Result<Self, Error> {
3741
let c_str = CString::new(str)?;
3842
let inner = unsafe { ffi_try!(mupdf_buffer_from_base64(context(), c_str.as_ptr())) };
@@ -54,6 +58,10 @@ impl Buffer {
5458
unsafe { fz_buffer_storage(context(), self.inner, ptr::null_mut()) }
5559
}
5660

61+
pub fn is_empty(&self) -> bool {
62+
self.len() == 0
63+
}
64+
5765
pub fn into_inner(mut self) -> *mut fz_buffer {
5866
let inner = self.inner;
5967
self.inner = ptr::null_mut();
@@ -152,7 +160,10 @@ impl TryFrom<Vec<u8>> for Buffer {
152160
#[cfg(test)]
153161
mod test {
154162
use super::Buffer;
155-
use std::io::{Read, Write};
163+
use std::{
164+
io::{Read, Write},
165+
str::FromStr,
166+
};
156167

157168
#[test]
158169
fn test_buffer_len() {
@@ -167,7 +178,7 @@ mod test {
167178
assert_eq!(n, 3);
168179

169180
let mut output = [0; 3];
170-
buf.read(&mut output).unwrap();
181+
buf.read_exact(&mut output).unwrap();
171182
assert_eq!(output, [97, 98, 99]);
172183
}
173184

@@ -225,15 +236,15 @@ mod test {
225236
fn test_buffer_from_str() {
226237
let mut buf = Buffer::from_str("abc").unwrap();
227238
let mut output = [0; 3];
228-
buf.read(&mut output).unwrap();
239+
buf.read_exact(&mut output).unwrap();
229240
assert_eq!(output, [97, 98, 99]);
230241
}
231242

232243
#[test]
233244
fn test_buffer_from_base64() {
234245
let mut buf = Buffer::from_base64("YWJj").unwrap();
235246
let mut output = [0; 3];
236-
buf.read(&mut output).unwrap();
247+
buf.read_exact(&mut output).unwrap();
237248
assert_eq!(output, [97, 98, 99]);
238249
}
239250
}

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static BASE_CONTEXT: Lazy<Mutex<BaseContext>> = Lazy::new(|| {
2828
});
2929

3030
thread_local! {
31-
static LOCAL_CONTEXT: RefCell<RawContext> = RefCell::new(RawContext(ptr::null_mut()));
31+
static LOCAL_CONTEXT: RefCell<RawContext> = const { RefCell::new(RawContext(ptr::null_mut())) };
3232
}
3333

3434
#[derive(Debug)]

src/device.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Device {
7676
})
7777
}
7878

79+
#[allow(clippy::too_many_arguments)]
7980
pub fn fill_path(
8081
&self,
8182
path: &Path,
@@ -102,6 +103,7 @@ impl Device {
102103
Ok(())
103104
}
104105

106+
#[allow(clippy::too_many_arguments)]
105107
pub fn stroke_path(
106108
&self,
107109
path: &Path,
@@ -183,6 +185,7 @@ impl Device {
183185
Ok(())
184186
}
185187

188+
#[allow(clippy::too_many_arguments)]
186189
pub fn stroke_text(
187190
&self,
188191
text: &Text,

src/document.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Document {
6666
let c_magic = CString::new(magic)?;
6767
let len = bytes.len();
6868
let mut buf = Buffer::with_capacity(len);
69-
buf.write(bytes)?;
69+
buf.write_all(bytes)?;
7070
let inner = unsafe {
7171
ffi_try!(mupdf_open_document_from_bytes(
7272
context(),
@@ -313,7 +313,7 @@ pub struct PageIter<'a> {
313313
doc: &'a Document,
314314
}
315315

316-
impl<'a> Iterator for PageIter<'a> {
316+
impl Iterator for PageIter<'_> {
317317
type Item = Result<Page, Error>;
318318

319319
fn next(&mut self) -> Option<Self::Item> {

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ impl fmt::Display for MuPdfError {
2222

2323
impl std::error::Error for MuPdfError {}
2424

25+
/// # Safety
26+
///
27+
/// * `error` must point to a non-null, well-aligned initialized instance of `mupdf_error_t`.
28+
///
29+
/// * `(*error).message` must point to a null-terminated c-string.
2530
pub unsafe fn ffi_error(err: *mut mupdf_error_t) -> MuPdfError {
2631
use std::ffi::CStr;
2732

src/pdf/document.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl PdfDocument {
253253
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
254254
let len = bytes.len();
255255
let mut buf = Buffer::with_capacity(len);
256-
buf.write(bytes)?;
256+
buf.write_all(bytes)?;
257257
unsafe {
258258
let inner = ffi_try!(mupdf_pdf_open_document_from_bytes(context(), buf.inner));
259259
Ok(Self::from_raw(inner))
@@ -645,7 +645,7 @@ impl PdfDocument {
645645

646646
let mut refs = refs.into_iter();
647647
let first = refs.next().unwrap();
648-
let last = refs.last().unwrap_or_else(|| first.clone());
648+
let last = refs.next_back().unwrap_or_else(|| first.clone());
649649

650650
parent.dict_put("First", first)?;
651651
parent.dict_put("Last", last)?;

src/pdf/object.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ffi::{CStr, CString};
33
use std::fmt;
44
use std::io::{self, BufReader, Read, Write};
55
use std::slice;
6+
use std::str::FromStr;
67

78
use mupdf_sys::*;
89

@@ -317,6 +318,7 @@ impl PdfObject {
317318
Ok(Some(Self { inner }))
318319
}
319320

321+
#[allow(clippy::len_without_is_empty)]
320322
pub fn len(&self) -> Result<usize, Error> {
321323
let size = unsafe { ffi_try!(mupdf_pdf_array_len(context(), self.inner)) };
322324
Ok(size as usize)

src/stroke_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct StrokeState {
4141
}
4242

4343
impl StrokeState {
44+
#[allow(clippy::too_many_arguments)]
4445
pub fn new(
4546
start_cap: LineCap,
4647
dash_cap: LineCap,

src/system_font.rs

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::convert::TryFrom;
21
use std::ffi::CStr;
32
use std::os::raw::{c_char, c_int};
43
use std::ptr;
@@ -12,7 +11,7 @@ use num_enum::TryFromPrimitive;
1211
use crate::font::Font;
1312
use mupdf_sys::*;
1413

15-
pub unsafe extern "C" fn load_system_font(
14+
pub(crate) unsafe extern "C" fn load_system_font(
1615
ctx: *mut fz_context,
1716
name: *const c_char,
1817
bold: c_int,
@@ -57,17 +56,14 @@ pub unsafe extern "C" fn load_system_font(
5756
),
5857
Err(_) => return ptr::null_mut(),
5958
};
60-
match font {
61-
Ok(font) => {
62-
if needs_exact_metrics == 1
63-
&& ((bold == 1 && !font.is_bold()) || (italic == 1 && !font.is_italic()))
64-
{
65-
return ptr::null_mut();
66-
}
67-
fz_keep_font(ctx, font.inner);
68-
return font.inner;
59+
if let Ok(font) = font {
60+
if needs_exact_metrics == 1
61+
&& ((bold == 1 && !font.is_bold()) || (italic == 1 && !font.is_italic()))
62+
{
63+
return ptr::null_mut();
6964
}
70-
Err(_) => {}
65+
fz_keep_font(ctx, font.inner);
66+
return font.inner;
7167
}
7268
}
7369
}
@@ -76,6 +72,7 @@ pub unsafe extern "C" fn load_system_font(
7672

7773
#[derive(TryFromPrimitive)]
7874
#[repr(u32)]
75+
#[allow(clippy::enum_variant_names)]
7976
enum Ordering {
8077
AdobeCns = FZ_ADOBE_CNS as u32,
8178
AdobeGb = FZ_ADOBE_GB as u32,
@@ -146,38 +143,17 @@ pub unsafe extern "C" fn load_system_cjk_font(
146143
}
147144

148145
#[cfg(not(windows))]
149-
pub unsafe extern "C" fn load_system_cjk_font(
146+
pub(crate) unsafe extern "C" fn load_system_cjk_font(
150147
ctx: *mut fz_context,
151148
name: *const c_char,
152-
ordering: c_int,
153-
serif: c_int,
149+
_ordering: c_int,
150+
_serif: c_int,
154151
) -> *mut fz_font {
155152
// Try name first
156-
let font = load_system_font(ctx, name, 0, 0, 0);
157-
if !font.is_null() {
158-
return font;
159-
}
160-
if serif == 1 {
161-
match Ordering::try_from(ordering as u32) {
162-
Ok(Ordering::AdobeCns) => {}
163-
Ok(Ordering::AdobeGb) => {}
164-
Ok(Ordering::AdobeJapan) => {}
165-
Ok(Ordering::AdobeKorea) => {}
166-
Err(_) => {}
167-
}
168-
} else {
169-
match Ordering::try_from(ordering as u32) {
170-
Ok(Ordering::AdobeCns) => {}
171-
Ok(Ordering::AdobeGb) => {}
172-
Ok(Ordering::AdobeJapan) => {}
173-
Ok(Ordering::AdobeKorea) => {}
174-
Err(_) => {}
175-
}
176-
}
177-
ptr::null_mut()
153+
load_system_font(ctx, name, 0, 0, 0)
178154
}
179155

180-
pub unsafe extern "C" fn load_system_fallback_font(
156+
pub(crate) unsafe extern "C" fn load_system_fallback_font(
181157
_ctx: *mut fz_context,
182158
_script: c_int,
183159
_language: c_int,

0 commit comments

Comments
 (0)