@@ -5,9 +5,15 @@ use std::path::{Path, PathBuf};
55use log:: { debug, trace} ;
66use crate :: { config, path} ;
77
8- #[ derive( Debug , Clone , Copy ) ]
8+ #[ derive( Clone , Copy ) ]
99pub struct TrapLabel ( u64 ) ;
1010
11+ impl Debug for TrapLabel {
12+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
13+ write ! ( f, "TrapLabel({:x})" , self . 0 )
14+ }
15+ }
16+
1117impl Display for TrapLabel {
1218 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
1319 write ! ( f, "#{:x}" , self . 0 )
@@ -16,64 +22,59 @@ impl Display for TrapLabel {
1622
1723//TODO: typed labels
1824
25+ impl TrapLabel {
26+ pub fn as_key_part ( & self ) -> String {
27+ format ! ( "{{{}}}" , self )
28+ }
29+ }
30+
1931#[ derive( Debug , Clone ) ]
20- pub enum KeyPart {
32+ pub enum TrapId {
33+ Star ,
34+ Key ( String ) ,
2135 Label ( TrapLabel ) ,
22- Text ( String ) ,
2336}
2437
25- impl From < String > for KeyPart {
38+ impl From < String > for TrapId {
2639 fn from ( value : String ) -> Self {
27- KeyPart :: Text ( value)
40+ TrapId :: Key ( value)
2841 }
2942}
3043
31- impl From < & str > for KeyPart {
44+ impl From < & str > for TrapId {
3245 fn from ( value : & str ) -> Self {
33- KeyPart :: Text ( value. into ( ) )
46+ TrapId :: Key ( value. into ( ) )
3447 }
3548}
3649
37- impl From < TrapLabel > for KeyPart {
50+ impl From < TrapLabel > for TrapId {
3851 fn from ( value : TrapLabel ) -> Self {
39- KeyPart :: Label ( value)
40- }
41- }
42-
43- #[ derive( Debug , Clone ) ]
44- pub enum TrapId {
45- Star ,
46- Key ( Vec < KeyPart > ) ,
47- Label ( TrapLabel ) ,
48- }
49-
50- impl < T : Into < KeyPart > > From < T > for TrapId {
51- fn from ( value : T ) -> Self {
52- TrapId :: Key ( vec ! [ value. into( ) ] )
52+ TrapId :: Label ( value)
5353 }
5454}
5555
5656#[ macro_export]
5757macro_rules! trap_key {
58- ( $( $x: expr) ,+ $( , ) ?) => (
59- $crate:: trap:: TrapId :: Key ( vec![ $( $x. into( ) ) ,+] )
60- ) ;
58+ ( $( $x: expr) ,+ $( , ) ?) => { {
59+ trait BlanketKeyPart : std:: fmt:: Display {
60+ fn as_key_part( & self ) -> String {
61+ format!( "{}" , self )
62+ }
63+ }
64+ impl <T : std:: fmt:: Display > BlanketKeyPart for T { }
65+ let mut key = String :: new( ) ;
66+ $(
67+ key. push_str( & $x. as_key_part( ) ) ;
68+ ) *
69+ $crate:: TrapId :: Key ( key)
70+ } } ;
6171}
6272
6373impl Display for TrapId {
6474 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
6575 match self {
6676 TrapId :: Star => write ! ( f, "*" ) ,
67- TrapId :: Key ( k) => {
68- f. write_str ( "@\" " ) ?;
69- for p in k {
70- match p {
71- KeyPart :: Label ( l) => write ! ( f, "{{{l}}}" ) ?,
72- KeyPart :: Text ( s) => f. write_str ( & escaped ( s) ) ?,
73- }
74- }
75- f. write_str ( "\" " )
76- }
77+ TrapId :: Key ( k) => write ! ( f, "@{}" , quoted( k) ) ,
7778 TrapId :: Label ( l) => Display :: fmt ( & l, f)
7879 }
7980 }
@@ -109,9 +110,17 @@ impl TrapFile {
109110 Ok ( ( ) )
110111 }
111112
112- pub fn label ( & mut self , id : TrapId ) -> std:: io:: Result < TrapLabel > {
113- if let TrapId :: Label ( l) = id {
114- return Ok ( l) ;
113+ pub fn label ( & mut self , mut id : TrapId ) -> std:: io:: Result < TrapLabel > {
114+ match id {
115+ TrapId :: Star => { }
116+ TrapId :: Key ( ref s) => {
117+ if s. is_empty ( ) {
118+ id = TrapId :: Star ;
119+ }
120+ }
121+ TrapId :: Label ( l) => {
122+ return Ok ( l) ;
123+ }
115124 }
116125 let ret = self . create_label ( ) ;
117126 trace ! ( "emit -> {}: {ret:?} = {id:?}" , self . trap_name) ;
0 commit comments