@@ -36,6 +36,17 @@ pub enum CssResult {
36
36
InvalidCacheSize ,
37
37
}
38
38
39
+ impl From < InlineError > for CssResult {
40
+ fn from ( value : InlineError ) -> Self {
41
+ match value {
42
+ InlineError :: IO ( _) => CssResult :: IoError ,
43
+ InlineError :: Network { .. } => CssResult :: RemoteStylesheetNotAvailable ,
44
+ InlineError :: ParseError ( _) => CssResult :: InternalSelectorParseError ,
45
+ InlineError :: MissingStyleSheet { .. } => CssResult :: MissingStylesheet ,
46
+ }
47
+ }
48
+ }
49
+
39
50
// must be public because the impl From<&CssInlinerOptions> for InlineOptions would leak this type
40
51
/// Error to convert to CssResult later
41
52
/// cbindgen:ignore
@@ -84,6 +95,29 @@ pub struct CssInlinerOptions {
84
95
pub preallocate_node_capacity : size_t ,
85
96
}
86
97
98
+ macro_rules! inliner {
99
+ ( $options: expr) => {
100
+ CSSInliner :: new(
101
+ match InlineOptions :: try_from( match $options. as_ref( ) {
102
+ Some ( ptr) => ptr,
103
+ None => return CssResult :: NullOptions ,
104
+ } ) {
105
+ Ok ( inline_options) => inline_options,
106
+ Err ( e) => return CssResult :: from( e) ,
107
+ } ,
108
+ )
109
+ } ;
110
+ }
111
+
112
+ macro_rules! to_str {
113
+ ( $input: expr) => {
114
+ match CStr :: from_ptr( $input) . to_str( ) {
115
+ Ok ( val) => val,
116
+ Err ( _) => return CssResult :: InvalidInputString ,
117
+ }
118
+ } ;
119
+ }
120
+
87
121
/// @brief Inline CSS from @p input & write the result to @p output with @p options.
88
122
/// @param options configuration for the inliner.
89
123
/// @param input html to inline.
@@ -99,27 +133,41 @@ pub unsafe extern "C" fn css_inline_to(
99
133
output : * mut c_char ,
100
134
output_size : size_t ,
101
135
) -> CssResult {
102
- let inliner = CSSInliner :: new (
103
- match InlineOptions :: try_from ( match options. as_ref ( ) {
104
- Some ( ptr) => ptr,
105
- None => return CssResult :: NullOptions ,
106
- } ) {
107
- Ok ( inline_options) => inline_options,
108
- Err ( e) => return CssResult :: from ( e) ,
109
- } ,
110
- ) ;
111
- let html = match CStr :: from_ptr ( input) . to_str ( ) {
112
- Ok ( val) => val,
113
- Err ( _) => return CssResult :: InvalidInputString ,
114
- } ;
136
+ let inliner = inliner ! ( options) ;
137
+ let html = to_str ! ( input) ;
115
138
let mut buffer = CBuffer :: new ( output, output_size) ;
116
139
if let Err ( e) = inliner. inline_to ( html, & mut buffer) {
117
- return match e {
118
- InlineError :: IO ( _) => CssResult :: IoError ,
119
- InlineError :: Network { .. } => CssResult :: RemoteStylesheetNotAvailable ,
120
- InlineError :: ParseError ( _) => CssResult :: InternalSelectorParseError ,
121
- InlineError :: MissingStyleSheet { .. } => CssResult :: MissingStylesheet ,
122
- } ;
140
+ return e. into ( ) ;
141
+ } ;
142
+ // Null terminate the pointer
143
+ let ptr: * mut c_char = buffer. buffer . add ( buffer. pos ) ;
144
+ * ptr = 0 ;
145
+ CssResult :: Ok
146
+ }
147
+
148
+ /// @brief Inline CSS @p fragment into @p input & write the result to @p output with @p options.
149
+ /// @param options configuration for the inliner.
150
+ /// @param input html to inline.
151
+ /// @param css css to inline.
152
+ /// @param output buffer to save the inlined CSS.
153
+ /// @param output_size size of @p output in bytes.
154
+ /// @return a CSS_RESULT enum variant regarding if the operation was a success or an error occurred
155
+ #[ allow( clippy:: missing_safety_doc) ]
156
+ #[ must_use]
157
+ #[ no_mangle]
158
+ pub unsafe extern "C" fn css_inline_fragment_to (
159
+ options : * const CssInlinerOptions ,
160
+ input : * const c_char ,
161
+ css : * const c_char ,
162
+ output : * mut c_char ,
163
+ output_size : size_t ,
164
+ ) -> CssResult {
165
+ let inliner = inliner ! ( options) ;
166
+ let html = to_str ! ( input) ;
167
+ let css = to_str ! ( css) ;
168
+ let mut buffer = CBuffer :: new ( output, output_size) ;
169
+ if let Err ( e) = inliner. inline_fragment_to ( html, css, & mut buffer) {
170
+ return e. into ( ) ;
123
171
} ;
124
172
// Null terminate the pointer
125
173
let ptr: * mut c_char = buffer. buffer . add ( buffer. pos ) ;
0 commit comments