2727) ]
2828use css_inline as rust_inline;
2929use magnus:: {
30- class , define_module , function, method,
30+ function, method,
3131 prelude:: * ,
3232 scan_args:: { get_kwargs, scan_args, Args } ,
3333 typed_data:: Obj ,
34- DataTypeFunctions , RHash , Symbol , TryConvert , TypedData , Value ,
34+ DataTypeFunctions , RHash , Ruby , TryConvert , TypedData , Value ,
3535} ;
3636use rayon:: prelude:: * ;
3737use std:: {
@@ -73,19 +73,20 @@ struct Options {
7373impl TryConvert for Options {
7474 fn try_convert ( v : Value ) -> Result < Self , magnus:: Error > {
7575 let h = RHash :: try_convert ( v) ?;
76+ let ruby = Ruby :: get_with ( v) ;
7677 Ok ( Self {
77- inline_style_tags : h. aref :: < _ , Option < bool > > ( Symbol :: new ( "inline_style_tags" ) ) ?,
78- keep_style_tags : h. aref :: < _ , Option < bool > > ( Symbol :: new ( "keep_style_tags" ) ) ?,
79- keep_link_tags : h. aref :: < _ , Option < bool > > ( Symbol :: new ( "keep_link_tags" ) ) ?,
80- keep_at_rules : h. aref :: < _ , Option < bool > > ( Symbol :: new ( "keep_at_rules" ) ) ?,
81- minify_css : h. aref :: < _ , Option < bool > > ( Symbol :: new ( "minify_css" ) ) ?,
82- base_url : h. aref :: < _ , Option < String > > ( Symbol :: new ( "base_url" ) ) ?,
78+ inline_style_tags : h. aref :: < _ , Option < bool > > ( ruby . to_symbol ( "inline_style_tags" ) ) ?,
79+ keep_style_tags : h. aref :: < _ , Option < bool > > ( ruby . to_symbol ( "keep_style_tags" ) ) ?,
80+ keep_link_tags : h. aref :: < _ , Option < bool > > ( ruby . to_symbol ( "keep_link_tags" ) ) ?,
81+ keep_at_rules : h. aref :: < _ , Option < bool > > ( ruby . to_symbol ( "keep_at_rules" ) ) ?,
82+ minify_css : h. aref :: < _ , Option < bool > > ( ruby . to_symbol ( "minify_css" ) ) ?,
83+ base_url : h. aref :: < _ , Option < String > > ( ruby . to_symbol ( "base_url" ) ) ?,
8384 load_remote_stylesheets : h
84- . aref :: < _ , Option < bool > > ( Symbol :: new ( "load_remote_stylesheets" ) ) ?,
85- cache : h. aref :: < _ , Option < Obj < StylesheetCache > > > ( Symbol :: new ( "cache" ) ) ?,
86- extra_css : h. aref :: < _ , Option < String > > ( Symbol :: new ( "extra_css" ) ) ?,
85+ . aref :: < _ , Option < bool > > ( ruby . to_symbol ( "load_remote_stylesheets" ) ) ?,
86+ cache : h. aref :: < _ , Option < Obj < StylesheetCache > > > ( ruby . to_symbol ( "cache" ) ) ?,
87+ extra_css : h. aref :: < _ , Option < String > > ( ruby . to_symbol ( "extra_css" ) ) ?,
8788 preallocate_node_capacity : h
88- . aref :: < _ , Option < usize > > ( Symbol :: new ( "preallocate_node_capacity" ) ) ?,
89+ . aref :: < _ , Option < usize > > ( ruby . to_symbol ( "preallocate_node_capacity" ) ) ?,
8990 } )
9091 }
9192}
@@ -120,8 +121,9 @@ struct StylesheetCache {
120121impl StylesheetCache {
121122 fn new ( args : & [ Value ] ) -> RubyResult < StylesheetCache > {
122123 fn error ( ) -> magnus:: Error {
124+ let ruby = Ruby :: get ( ) . expect ( "Always called from a Ruby thread" ) ;
123125 magnus:: Error :: new (
124- magnus :: exception :: arg_error ( ) ,
126+ ruby . exception_arg_error ( ) ,
125127 "Cache size must be an integer greater than zero" ,
126128 )
127129 }
@@ -143,19 +145,19 @@ struct InlineErrorWrapper(rust_inline::InlineError);
143145
144146impl From < InlineErrorWrapper > for magnus:: Error {
145147 fn from ( error : InlineErrorWrapper ) -> Self {
148+ let ruby = Ruby :: get ( ) . expect ( "Always called from a Ruby thread" ) ;
146149 match error. 0 {
147150 rust_inline:: InlineError :: IO ( error) => {
148- magnus:: Error :: new ( magnus:: exception:: arg_error ( ) , error. to_string ( ) )
151+ magnus:: Error :: new ( ruby. exception_arg_error ( ) , error. to_string ( ) )
152+ }
153+ rust_inline:: InlineError :: Network { error, location } => {
154+ magnus:: Error :: new ( ruby. exception_arg_error ( ) , format ! ( "{error}: {location}" ) )
149155 }
150- rust_inline:: InlineError :: Network { error, location } => magnus:: Error :: new (
151- magnus:: exception:: arg_error ( ) ,
152- format ! ( "{error}: {location}" ) ,
153- ) ,
154156 rust_inline:: InlineError :: ParseError ( message) => {
155- magnus:: Error :: new ( magnus :: exception :: arg_error ( ) , message. to_string ( ) )
157+ magnus:: Error :: new ( ruby . exception_arg_error ( ) , message. to_string ( ) )
156158 }
157159 rust_inline:: InlineError :: MissingStyleSheet { .. } => {
158- magnus:: Error :: new ( magnus :: exception :: arg_error ( ) , error. 0 . to_string ( ) )
160+ magnus:: Error :: new ( ruby . exception_arg_error ( ) , error. 0 . to_string ( ) )
159161 }
160162 }
161163 }
@@ -168,8 +170,9 @@ struct UrlError {
168170
169171impl From < UrlError > for magnus:: Error {
170172 fn from ( error : UrlError ) -> magnus:: Error {
173+ let ruby = Ruby :: get ( ) . expect ( "Always called from a Ruby thread" ) ;
171174 magnus:: Error :: new (
172- magnus :: exception :: arg_error ( ) ,
175+ ruby . exception_arg_error ( ) ,
173176 format ! ( "{}: {}" , error. error, error. url) ,
174177 )
175178 }
@@ -275,8 +278,8 @@ fn inline_many_fragments_impl(
275278}
276279
277280#[ magnus:: init( name = "css_inline" ) ]
278- fn init ( ) -> RubyResult < ( ) > {
279- let module = define_module ( "CSSInline" ) ?;
281+ fn init ( ruby : & Ruby ) -> RubyResult < ( ) > {
282+ let module = ruby . define_module ( "CSSInline" ) ?;
280283
281284 module. define_module_function ( "inline" , function ! ( inline, -1 ) ) ?;
282285 module. define_module_function ( "inline_fragment" , function ! ( inline_fragment, -1 ) ) ?;
@@ -286,7 +289,7 @@ fn init() -> RubyResult<()> {
286289 function ! ( inline_many_fragments, -1 ) ,
287290 ) ?;
288291
289- let class = module. define_class ( "CSSInliner" , class :: object ( ) ) ?;
292+ let class = module. define_class ( "CSSInliner" , ruby . class_object ( ) ) ?;
290293 class. define_singleton_method ( "new" , function ! ( CSSInliner :: new, -1 ) ) ?;
291294 class. define_method ( "inline" , method ! ( CSSInliner :: inline, 1 ) ) ?;
292295 class. define_method ( "inline_fragment" , method ! ( CSSInliner :: inline_fragment, 2 ) ) ?;
@@ -296,7 +299,7 @@ fn init() -> RubyResult<()> {
296299 method ! ( CSSInliner :: inline_many_fragments, 2 ) ,
297300 ) ?;
298301
299- let class = module. define_class ( "StylesheetCache" , class :: object ( ) ) ?;
302+ let class = module. define_class ( "StylesheetCache" , ruby . class_object ( ) ) ?;
300303 class. define_singleton_method ( "new" , function ! ( StylesheetCache :: new, -1 ) ) ?;
301304 Ok ( ( ) )
302305}
0 commit comments