44
55mod ffi;
66
7- use std:: io:: { Error , Result } ;
8- use std:: path:: Path ;
9-
107/// Compress.
11- pub fn compress ( data : & [ u8 ] , metadata : String , quality : usize , transform : bool ) -> Option < Vec < u8 > > {
12- let metadata_length = metadata. len ( ) ;
8+ pub fn compress < T > ( data : & [ u8 ] , quality : usize , metadata : T , transform : bool ) -> Option < Vec < u8 > >
9+ where
10+ T : Into < Vec < u8 > > ,
11+ {
1312 let metadata = match std:: ffi:: CString :: new ( metadata) {
1413 Ok ( metadata) => metadata,
1514 _ => return None ,
1615 } ;
16+ let metadata_size = metadata. count_bytes ( ) ;
1717 let size = unsafe {
1818 ffi:: ComputeTTFToWOFF2Size (
1919 data. as_ptr ( ) as * const _ ,
2020 data. len ( ) ,
2121 metadata. as_ptr ( ) as * const _ ,
22- metadata_length ,
22+ metadata_size ,
2323 )
2424 } ;
2525 let mut buffer = vec ! [ 0 ; size] ;
@@ -31,7 +31,7 @@ pub fn compress(data: &[u8], metadata: String, quality: usize, transform: bool)
3131 buffer. as_mut_ptr ( ) as * mut _ ,
3232 & mut size as * mut _ ,
3333 metadata. as_ptr ( ) as * const _ ,
34- metadata_length ,
34+ metadata_size ,
3535 quality as core:: ffi:: c_int ,
3636 transform as core:: ffi:: c_int ,
3737 )
@@ -63,79 +63,51 @@ pub fn decompress(data: &[u8]) -> Option<Vec<u8>> {
6363 buffer. into ( )
6464}
6565
66- /// Compress or decompress.
67- pub fn convert < T : AsRef < Path > > (
68- source : T ,
69- destination : T ,
70- metadata : Option < String > ,
71- quality : Option < usize > ,
72- transform : Option < bool > ,
73- ) -> Result < ( ) > {
74- let data = std:: fs:: read ( source) ?;
75- let destination = destination. as_ref ( ) ;
76- let data = if destination
77- . extension ( )
78- . and_then ( |value| value. to_str ( ) )
79- . map ( |value| value == "woff2" )
80- . unwrap_or ( false )
81- {
82- match compress (
83- & data,
84- metadata. unwrap_or_default ( ) ,
85- quality. unwrap_or ( 8 ) ,
86- transform. unwrap_or ( true ) ,
87- ) {
88- Some ( data) => data,
89- _ => return Err ( Error :: other ( "failed to compress" ) ) ,
90- }
91- } else {
92- match decompress ( & data) {
93- Some ( data) => data,
94- _ => return Err ( Error :: other ( "failed to decompress" ) ) ,
95- }
96- } ;
97- std:: fs:: write ( destination, data)
98- }
99-
10066#[ cfg( test) ]
10167mod tests {
68+ use std:: fs:: { read, write} ;
69+
70+ const DEFAULT_QUALITY : usize = 8 ;
71+ const DEFAULT_METADATA : & str = "" ;
72+ const DEFAULT_TRANSFORM : bool = true ;
73+
74+ macro_rules! ok( ( $result: expr) => ( $result. unwrap( ) ) ) ;
75+
10276 #[ test]
10377 fn otf ( ) {
104- super :: convert (
105- "tests/fixtures/Roboto-Regular.otf" ,
106- "tests/fixtures/Roboto-Regular.otf.woff2" ,
107- None ,
108- None ,
109- None ,
110- )
111- . unwrap ( ) ;
112- super :: convert (
78+ ok ! ( write(
11379 "tests/fixtures/Roboto-Regular.otf.woff2" ,
80+ ok!( super :: compress(
81+ & ok!( read( "tests/fixtures/Roboto-Regular.otf" ) ) ,
82+ DEFAULT_QUALITY ,
83+ DEFAULT_METADATA ,
84+ DEFAULT_TRANSFORM ,
85+ ) ) ,
86+ ) ) ;
87+ ok ! ( write(
11488 "tests/fixtures/Roboto-Regular.otf" ,
115- None ,
116- None ,
117- None ,
118- )
119- . unwrap ( ) ;
89+ ok!( super :: decompress( & ok!( read(
90+ "tests/fixtures/Roboto-Regular.otf.woff2"
91+ ) ) ) ) ,
92+ ) ) ;
12093 }
12194
12295 #[ test]
12396 fn ttf ( ) {
124- super :: convert (
125- "tests/fixtures/Roboto-Regular.ttf" ,
126- "tests/fixtures/Roboto-Regular.ttf.woff2" ,
127- None ,
128- None ,
129- None ,
130- )
131- . unwrap ( ) ;
132- super :: convert (
97+ ok ! ( write(
13398 "tests/fixtures/Roboto-Regular.ttf.woff2" ,
99+ ok!( super :: compress(
100+ & ok!( read( "tests/fixtures/Roboto-Regular.ttf" ) ) ,
101+ DEFAULT_QUALITY ,
102+ DEFAULT_METADATA ,
103+ DEFAULT_TRANSFORM ,
104+ ) ) ,
105+ ) ) ;
106+ ok ! ( write(
134107 "tests/fixtures/Roboto-Regular.ttf" ,
135- None ,
136- None ,
137- None ,
138- )
139- . unwrap ( ) ;
108+ ok!( super :: decompress( & ok!( read(
109+ "tests/fixtures/Roboto-Regular.ttf.woff2"
110+ ) ) ) ) ,
111+ ) ) ;
140112 }
141113}
0 commit comments