@@ -17,7 +17,9 @@ use std::collections::HashMap;
1717
1818use super :: env:: required_type_arg;
1919use crate :: py_packaging:: embedded_resource:: EmbeddedPythonResourcesPrePackaged ;
20- use crate :: py_packaging:: resource:: { BytecodeModule , BytecodeOptimizationLevel , SourceModule } ;
20+ use crate :: py_packaging:: resource:: {
21+ BytecodeModule , BytecodeOptimizationLevel , ResourceData , SourceModule ,
22+ } ;
2123
2224#[ derive( Debug , Clone ) ]
2325pub struct PythonSourceModule {
@@ -150,6 +152,68 @@ impl TypedValue for PythonBytecodeModule {
150152 }
151153}
152154
155+ #[ derive( Debug , Clone ) ]
156+ pub struct PythonResourceData {
157+ pub data : ResourceData ,
158+ }
159+
160+ impl TypedValue for PythonResourceData {
161+ immutable ! ( ) ;
162+ any ! ( ) ;
163+ not_supported ! (
164+ binop, dir_attr, function, get_hash, indexable, iterable, sequence, set_attr, to_int
165+ ) ;
166+
167+ fn to_str ( & self ) -> String {
168+ format ! (
169+ "PythonResourceData<package={}, name={}>" ,
170+ self . data. package, self . data. name
171+ )
172+ }
173+
174+ fn to_repr ( & self ) -> String {
175+ self . to_str ( )
176+ }
177+
178+ fn get_type ( & self ) -> & ' static str {
179+ "PythonResourceData"
180+ }
181+
182+ fn to_bool ( & self ) -> bool {
183+ true
184+ }
185+
186+ fn compare ( & self , other : & dyn TypedValue , _recursion : u32 ) -> Result < Ordering , ValueError > {
187+ default_compare ( self , other)
188+ }
189+
190+ fn get_attr ( & self , attribute : & str ) -> ValueResult {
191+ let v = match attribute {
192+ "package" => Value :: new ( self . data . package . clone ( ) ) ,
193+ "name" => Value :: new ( self . data . name . clone ( ) ) ,
194+ // TODO expose raw data
195+ attr => {
196+ return Err ( ValueError :: OperationNotSupported {
197+ op : format ! ( ".{}" , attr) ,
198+ left : "PythonResourceData" . to_string ( ) ,
199+ right : None ,
200+ } )
201+ }
202+ } ;
203+
204+ Ok ( v)
205+ }
206+
207+ fn has_attr ( & self , attribute : & str ) -> Result < bool , ValueError > {
208+ Ok ( match attribute {
209+ "package" => true ,
210+ "name" => true ,
211+ // TODO expose raw data
212+ _ => false ,
213+ } )
214+ }
215+ }
216+
153217#[ derive( Debug , Clone ) ]
154218pub struct PythonEmbeddedResources {
155219 pub embedded : EmbeddedPythonResourcesPrePackaged ,
@@ -236,4 +300,15 @@ starlark_module! { python_resource_env =>
236300
237301 Ok ( Value :: new( None ) )
238302 }
303+
304+ PythonEmbeddedResources . add_resource_data( this, resource) {
305+ required_type_arg( "resource" , "PythonResourceData" , & resource) ?;
306+
307+ this. downcast_apply_mut( |embedded: & mut PythonEmbeddedResources | {
308+ let r = resource. downcast_apply( |r: & PythonResourceData | r. data. clone( ) ) ;
309+ embedded. embedded. add_resource( & r) ;
310+ } ) ;
311+
312+ Ok ( Value :: new( None ) )
313+ }
239314}
0 commit comments