11use std:: collections:: HashMap ;
22
3- use pyo3:: exceptions:: PyRuntimeError ;
4- use pyo3:: prelude:: * ;
5-
6- use hyperlight_sandbox:: { DirPerms , FilePerms , Sandbox , SandboxConfig , HttpMethod } ;
7- use hyperlight_wasm_sandbox:: Wasm ;
8-
3+ use hyperlight_sandbox:: { DirPerms , FilePerms , HttpMethod , Sandbox , SandboxConfig } ;
94use hyperlight_sandbox_pyo3_common:: {
105 PyExecutionResult , PySnapshot , build_tool_registry, parse_size, parse_tool_registration,
116} ;
7+ use hyperlight_wasm_sandbox:: Wasm ;
8+ use pyo3:: exceptions:: PyRuntimeError ;
9+ use pyo3:: prelude:: * ;
1210
1311#[ pyclass( unsendable) ]
1412pub struct WasmSandbox {
@@ -80,16 +78,22 @@ impl WasmSandbox {
8078 builder = builder. input_dir ( dir) ;
8179 }
8280 if let Some ( ref dir) = self . output_dir {
83- builder = builder. output_dir ( dir, DirPerms :: READ | DirPerms :: MUTATE , FilePerms :: READ | FilePerms :: WRITE ) ;
81+ builder = builder. output_dir (
82+ dir,
83+ DirPerms :: READ | DirPerms :: MUTATE ,
84+ FilePerms :: READ | FilePerms :: WRITE ,
85+ ) ;
8486 } else if self . temp_output {
8587 builder = builder. temp_output ( ) ;
8688 }
87- let mut sandbox = builder. build ( )
89+ let mut sandbox = builder
90+ . build ( )
8891 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "Failed to create sandbox: {e:#}" ) ) ) ?;
8992 for ( target, methods) in std:: mem:: take ( & mut self . pending_networks ) {
9093 let methods = HttpMethod :: parse_list ( methods)
9194 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) ) ?;
92- sandbox. allow_domain ( & target, methods)
95+ sandbox
96+ . allow_domain ( & target, methods)
9397 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) ) ?;
9498 }
9599 self . inner = Some ( sandbox) ;
@@ -110,7 +114,8 @@ impl WasmSandbox {
110114 if let Some ( sandbox) = self . inner . as_mut ( ) {
111115 let methods = HttpMethod :: parse_list ( methods)
112116 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) ) ?;
113- sandbox. allow_domain ( target, methods)
117+ sandbox
118+ . allow_domain ( target, methods)
114119 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "{e}" ) ) ) ?;
115120 } else {
116121 self . pending_networks . push ( ( target. to_string ( ) , methods) ) ;
@@ -119,32 +124,44 @@ impl WasmSandbox {
119124 }
120125
121126 fn snapshot ( & mut self ) -> PyResult < PySnapshot > {
122- let sandbox = self . inner . as_mut ( )
127+ let sandbox = self
128+ . inner
129+ . as_mut ( )
123130 . ok_or_else ( || PyRuntimeError :: new_err ( "Sandbox not initialized" ) ) ?;
124- let snap = sandbox. snapshot ( )
131+ let snap = sandbox
132+ . snapshot ( )
125133 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "Snapshot failed: {e}" ) ) ) ?;
126134 Ok ( PySnapshot { inner : snap } )
127135 }
128136
129137 fn restore ( & mut self , snapshot : & PySnapshot ) -> PyResult < ( ) > {
130- let sandbox = self . inner . as_mut ( )
138+ let sandbox = self
139+ . inner
140+ . as_mut ( )
131141 . ok_or_else ( || PyRuntimeError :: new_err ( "Sandbox not initialized" ) ) ?;
132- sandbox. restore ( & snapshot. inner )
142+ sandbox
143+ . restore ( & snapshot. inner )
133144 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "Restore failed: {e}" ) ) ) ?;
134145 Ok ( ( ) )
135146 }
136147
137148 fn get_output_files ( & self ) -> PyResult < Vec < String > > {
138- let sandbox = self . inner . as_ref ( )
149+ let sandbox = self
150+ . inner
151+ . as_ref ( )
139152 . ok_or_else ( || PyRuntimeError :: new_err ( "Sandbox not initialized" ) ) ?;
140- sandbox. get_output_files ( )
153+ sandbox
154+ . get_output_files ( )
141155 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "Failed to get output files: {e}" ) ) )
142156 }
143157
144158 fn output_path ( & self ) -> PyResult < Option < String > > {
145- let sandbox = self . inner . as_ref ( )
159+ let sandbox = self
160+ . inner
161+ . as_ref ( )
146162 . ok_or_else ( || PyRuntimeError :: new_err ( "Sandbox not initialized" ) ) ?;
147- let path = sandbox. output_path ( )
163+ let path = sandbox
164+ . output_path ( )
148165 . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "Failed to get output path: {e}" ) ) ) ?;
149166 Ok ( path. map ( |p| p. display ( ) . to_string ( ) ) )
150167 }
@@ -157,4 +174,4 @@ fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> {
157174 m. add_class :: < PySnapshot > ( ) ?;
158175 m. add ( "__version__" , "0.1.0" ) ?;
159176 Ok ( ( ) )
160- }
177+ }
0 commit comments