1+ use std:: fmt;
2+
13use flagset:: { FlagSet , flags} ;
24use internment:: ArcIntern ;
35use rowan:: TextRange ;
@@ -26,6 +28,7 @@ flags! {
2628 Exact ,
2729 Meta ,
2830 Constructor ,
31+ Private
2932 }
3033}
3134
@@ -232,24 +235,39 @@ impl LuaTypeDecl {
232235 }
233236}
234237
238+ #[ derive( Debug , Eq , PartialEq , Hash , Clone ) ]
239+ pub enum LuaTypeIdentifier {
240+ Global ( SmolStr ) ,
241+ Local ( FileId , SmolStr ) ,
242+ }
243+
235244#[ derive( Debug , Eq , PartialEq , Hash , Clone ) ]
236245pub struct LuaTypeDeclId {
237- id : ArcIntern < SmolStr > ,
246+ id : ArcIntern < LuaTypeIdentifier > ,
238247}
239248
240249impl LuaTypeDeclId {
241- pub fn new_by_id ( id : ArcIntern < SmolStr > ) -> Self {
242- Self { id }
250+ pub fn global ( str : & str ) -> Self {
251+ Self {
252+ id : ArcIntern :: new ( LuaTypeIdentifier :: Global ( SmolStr :: new ( str) ) ) ,
253+ }
243254 }
244255
245- pub fn new ( str : & str ) -> Self {
256+ pub fn local ( file_id : FileId , str : & str ) -> Self {
246257 Self {
247- id : ArcIntern :: new ( SmolStr :: new ( str) ) ,
258+ id : ArcIntern :: new ( LuaTypeIdentifier :: Local ( file_id , SmolStr :: new ( str) ) ) ,
248259 }
249260 }
250261
262+ pub fn get_id ( & self ) -> & LuaTypeIdentifier {
263+ self . id . as_ref ( )
264+ }
265+
251266 pub fn get_name ( & self ) -> & str {
252- & self . id
267+ match self . id . as_ref ( ) {
268+ LuaTypeIdentifier :: Global ( name) => name. as_ref ( ) ,
269+ LuaTypeIdentifier :: Local ( _, name) => name. as_ref ( ) ,
270+ }
253271 }
254272
255273 pub fn get_simple_name ( & self ) -> & str {
@@ -301,7 +319,13 @@ impl Serialize for LuaTypeDeclId {
301319 where
302320 S : Serializer ,
303321 {
304- serializer. serialize_str ( & self . id )
322+ match self . id . as_ref ( ) {
323+ LuaTypeIdentifier :: Global ( name) => serializer. serialize_str ( name. as_ref ( ) ) ,
324+ LuaTypeIdentifier :: Local ( file_id, name) => {
325+ let s = format ! ( "{}|{}" , file_id. id, & name) ;
326+ serializer. serialize_str ( & s)
327+ }
328+ }
305329 }
306330}
307331
@@ -310,10 +334,29 @@ impl<'de> Deserialize<'de> for LuaTypeDeclId {
310334 where
311335 D : Deserializer < ' de > ,
312336 {
313- let s = String :: deserialize ( deserializer) ?;
314- Ok ( LuaTypeDeclId {
315- id : ArcIntern :: new ( SmolStr :: new ( s) ) ,
316- } )
337+ struct LuaTypeDeclIdVisitor ;
338+
339+ impl < ' de > serde:: de:: Visitor < ' de > for LuaTypeDeclIdVisitor {
340+ type Value = LuaTypeDeclId ;
341+
342+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
343+ formatter. write_str ( "a string representing LuaTypeDeclId" )
344+ }
345+
346+ fn visit_str < E > ( self , value : & str ) -> Result < Self :: Value , E >
347+ where
348+ E : serde:: de:: Error ,
349+ {
350+ if let Some ( ( file_id_str, name) ) = value. split_once ( '|' ) {
351+ let file_id = file_id_str. parse :: < u32 > ( ) . map_err ( E :: custom) ?;
352+ Ok ( LuaTypeDeclId :: local ( FileId { id : file_id } , name) )
353+ } else {
354+ Ok ( LuaTypeDeclId :: global ( value) )
355+ }
356+ }
357+ }
358+
359+ deserializer. deserialize_str ( LuaTypeDeclIdVisitor )
317360 }
318361}
319362
0 commit comments