@@ -328,7 +328,7 @@ pub struct Record<'a> {
328328 pub message : Option < RawValue < ' a > > ,
329329 pub level : Option < Level > ,
330330 pub logger : Option < & ' a str > ,
331- pub caller : Option < Caller < ' a > > ,
331+ pub caller : Caller < ' a > ,
332332 pub ( crate ) fields : RecordFields < ' a > ,
333333 pub ( crate ) predefined : heapless:: Vec < ( & ' a str , RawValue < ' a > ) , MAX_PREDEFINED_FIELDS > ,
334334}
@@ -355,7 +355,7 @@ impl<'a> Record<'a> {
355355 message : None ,
356356 level : None ,
357357 logger : None ,
358- caller : None ,
358+ caller : Default :: default ( ) ,
359359 fields : RecordFields :: with_capacity ( capacity) ,
360360 predefined : heapless:: Vec :: new ( ) ,
361361 }
@@ -372,10 +372,36 @@ pub trait RecordWithSourceConstructor<'r, 's> {
372372
373373// ---
374374
375- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
376- pub enum Caller < ' a > {
377- Text ( & ' a str ) ,
378- FileLine ( & ' a str , & ' a str ) ,
375+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Default ) ]
376+ pub struct Caller < ' a > {
377+ pub name : & ' a str ,
378+ pub file : & ' a str ,
379+ pub line : & ' a str ,
380+ }
381+
382+ impl < ' a > Caller < ' a > {
383+ pub fn none ( ) -> Self {
384+ Self :: default ( )
385+ }
386+
387+ pub fn with_name ( name : & ' a str ) -> Self {
388+ Self {
389+ name,
390+ ..Default :: default ( )
391+ }
392+ }
393+
394+ pub fn with_file_line ( file : & ' a str , line : & ' a str ) -> Self {
395+ Self {
396+ file,
397+ line,
398+ ..Default :: default ( )
399+ }
400+ }
401+
402+ pub fn is_empty ( & self ) -> bool {
403+ self . name . is_empty ( ) && self . file . is_empty ( ) && self . line . is_empty ( )
404+ }
379405}
380406
381407// ---
@@ -782,32 +808,13 @@ impl FieldSettings {
782808 true
783809 }
784810 Self :: Caller => {
785- to. caller = value
786- . parse :: < & str > ( )
787- . ok ( )
788- . filter ( |x| !x . is_empty ( ) )
789- . map ( |x| Caller :: Text ( x ) ) ;
811+ to. caller . name = value. parse :: < & str > ( ) . ok ( ) . unwrap_or_default ( ) ;
812+ true
813+ }
814+ Self :: CallerFile => {
815+ to . caller . file = value . parse :: < & str > ( ) . ok ( ) . unwrap_or_default ( ) ;
790816 true
791817 }
792- Self :: CallerFile => match & mut to. caller {
793- None => {
794- to. caller = value
795- . parse :: < & str > ( )
796- . ok ( )
797- . filter ( |x| !x. is_empty ( ) )
798- . map ( |x| Caller :: FileLine ( x, "" ) ) ;
799- to. caller . is_some ( )
800- }
801- Some ( Caller :: FileLine ( file, _) ) => {
802- if let Some ( value) = value. parse :: < & str > ( ) . ok ( ) . filter ( |x| !x. is_empty ( ) ) {
803- * file = value;
804- true
805- } else {
806- false
807- }
808- }
809- _ => false ,
810- } ,
811818 Self :: CallerLine => {
812819 let value = match value {
813820 RawValue :: Number ( value) => value,
@@ -821,11 +828,7 @@ impl FieldSettings {
821828 _ => return false ,
822829 } ;
823830
824- match & mut to. caller {
825- None => to. caller = Some ( Caller :: FileLine ( "" , value) ) ,
826- Some ( Caller :: FileLine ( _, line) ) => * line = value,
827- Some ( Caller :: Text ( _) ) => return false ,
828- }
831+ to. caller . line = value;
829832 true
830833 }
831834 Self :: Nested ( _) => false ,
@@ -1550,8 +1553,8 @@ impl RecordFilter for FieldFilter {
15501553 }
15511554 }
15521555 FieldKind :: Caller => {
1553- if let Some ( Caller :: Text ( caller ) ) = record. caller {
1554- self . match_value ( Some ( caller) , false )
1556+ if ! record. caller . name . is_empty ( ) {
1557+ self . match_value ( Some ( record . caller . name ) , false )
15551558 } else {
15561559 false
15571560 }
@@ -2354,50 +2357,50 @@ mod tests {
23542357 }
23552358
23562359 #[ rstest]
2357- #[ case( br#"{"caller":""}"# , None ) ]
2358- #[ case( br#"{"caller":"x"}"# , Some ( Caller :: Text ( "x" ) ) ) ]
2359- #[ case( br#"caller="""# , None ) ]
2360- #[ case( br#"caller="# , None ) ]
2361- #[ case( br#"caller=x"# , Some ( Caller :: Text ( "x" ) ) ) ]
2362- #[ case( br#"caller="x""# , Some ( Caller :: Text ( "x" ) ) ) ]
2363- fn test_caller ( #[ case] input : & [ u8 ] , #[ case] expected : Option < Caller > ) {
2360+ #[ case( br#"{"caller":""}"# , Caller :: none ( ) ) ]
2361+ #[ case( br#"{"caller":"x"}"# , Caller :: with_name ( "x" ) ) ]
2362+ #[ case( br#"caller="""# , Caller :: none ( ) ) ]
2363+ #[ case( br#"caller="# , Caller :: none ( ) ) ]
2364+ #[ case( br#"caller=x"# , Caller :: with_name ( "x" ) ) ]
2365+ #[ case( br#"caller="x""# , Caller :: with_name ( "x" ) ) ]
2366+ fn test_caller ( #[ case] input : & [ u8 ] , #[ case] expected : Caller ) {
23642367 let parser = Parser :: new ( ParserSettings :: default ( ) ) ;
23652368 let record = RawRecord :: parser ( ) . parse ( input) . next ( ) . unwrap ( ) . unwrap ( ) ;
23662369 let record = parser. parse ( & record. record ) ;
23672370 assert_eq ! ( record. caller, expected) ;
23682371 }
23692372
23702373 #[ rstest]
2371- #[ case( br#"{"file":""}"# , None ) ] // 1
2372- #[ case( br#"{"file":"x"}"# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 2
2373- #[ case( br#"file="""# , None ) ] // 3
2374- #[ case( br#"file="# , None ) ] // 4
2375- #[ case( br#"file=x"# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 5
2376- #[ case( br#"file="x""# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 6
2377- #[ case( br#"{"line":""}"# , None ) ] // 7
2378- #[ case( br#"{"line":"8"}"# , Some ( Caller :: FileLine ( "" , "8" ) ) ) ] // 8
2379- #[ case( br#"line="""# , None ) ] // 9
2380- #[ case( br#"line="# , None ) ] // 10
2381- #[ case( br#"line=11"# , Some ( Caller :: FileLine ( "" , "11" ) ) ) ] // 11
2382- #[ case( br#"line="12""# , Some ( Caller :: FileLine ( "" , "12" ) ) ) ] // 12
2383- #[ case( br#"{"file":"","line":""}"# , None ) ] // 13
2384- #[ case( br#"{"file":"x","line":"14"}"# , Some ( Caller :: FileLine ( "x" , "14" ) ) ) ] // 14
2385- #[ case( br#"file="" line="""# , None ) ] // 15
2386- #[ case( br#"file= line="# , None ) ] // 16
2387- #[ case( br#"file=x line=17"# , Some ( Caller :: FileLine ( "x" , "17" ) ) ) ] // 17
2388- #[ case( br#"file="x" line="18""# , Some ( Caller :: FileLine ( "x" , "18" ) ) ) ] // 18
2389- #[ case( br#"{"file":"","line":"19"}"# , Some ( Caller :: FileLine ( "" , "19" ) ) ) ] // 19
2390- #[ case( br#"{"file":"x","line":""}"# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 20
2391- #[ case( br#"file="" line="21""# , Some ( Caller :: FileLine ( "" , "21" ) ) ) ] // 21
2392- #[ case( br#"file= line=22"# , Some ( Caller :: FileLine ( "" , "22" ) ) ) ] // 22
2393- #[ case( br#"file=x line="# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 23
2394- #[ case( br#"file="x" line="# , Some ( Caller :: FileLine ( "x" , "" ) ) ) ] // 24
2395- #[ case( br#"file="x" line=21 line=25"# , Some ( Caller :: FileLine ( "x" , "25" ) ) ) ] // 25
2396- #[ case( br#"file=x line=26 file=y"# , Some ( Caller :: FileLine ( "y" , "26" ) ) ) ] // 26
2397- #[ case( br#"{"file":123, "file": {}, "line":27}"# , Some ( Caller :: FileLine ( "123 ", "27" ) ) ) ] // 27
2398- #[ case( br#"{"caller":"a", "file": "b", "line":28}"# , Some ( Caller :: Text ( "a" ) ) ) ] // 28
2399- #[ case( br#"{"file": "b", "line":{}}"# , Some ( Caller :: FileLine ( "b" , "" ) ) ) ] // 29
2400- fn test_caller_file_line ( #[ case] input : & [ u8 ] , #[ case] expected : Option < Caller > ) {
2374+ #[ case( br#"{"file":""}"# , Caller :: none ( ) ) ] // 1
2375+ #[ case( br#"{"file":"x"}"# , Caller :: with_file_line ( "x" , "" ) ) ] // 2
2376+ #[ case( br#"file="""# , Caller :: none ( ) ) ] // 3
2377+ #[ case( br#"file="# , Caller :: none ( ) ) ] // 4
2378+ #[ case( br#"file=x"# , Caller :: with_file_line ( "x" , "" ) ) ] // 5
2379+ #[ case( br#"file="x""# , Caller :: with_file_line ( "x" , "" ) ) ] // 6
2380+ #[ case( br#"{"line":""}"# , Caller :: none ( ) ) ] // 7
2381+ #[ case( br#"{"line":"8"}"# , Caller :: with_file_line ( "" , "8" ) ) ] // 8
2382+ #[ case( br#"line="""# , Caller :: none ( ) ) ] // 9
2383+ #[ case( br#"line="# , Caller :: none ( ) ) ] // 10
2384+ #[ case( br#"line=11"# , Caller :: with_file_line ( "" , "11" ) ) ] // 11
2385+ #[ case( br#"line="12""# , Caller :: with_file_line ( "" , "12" ) ) ] // 12
2386+ #[ case( br#"{"file":"","line":""}"# , Caller :: none ( ) ) ] // 13
2387+ #[ case( br#"{"file":"x","line":"14"}"# , Caller :: with_file_line ( "x" , "14" ) ) ] // 14
2388+ #[ case( br#"file="" line="""# , Caller :: none ( ) ) ] // 15
2389+ #[ case( br#"file= line="# , Caller :: none ( ) ) ] // 16
2390+ #[ case( br#"file=x line=17"# , Caller :: with_file_line ( "x" , "17" ) ) ] // 17
2391+ #[ case( br#"file="x" line="18""# , Caller :: with_file_line ( "x" , "18" ) ) ] // 18
2392+ #[ case( br#"{"file":"","line":"19"}"# , Caller :: with_file_line ( "" , "19" ) ) ] // 19
2393+ #[ case( br#"{"file":"x","line":""}"# , Caller :: with_file_line ( "x" , "" ) ) ] // 20
2394+ #[ case( br#"file="" line="21""# , Caller :: with_file_line ( "" , "21" ) ) ] // 21
2395+ #[ case( br#"file= line=22"# , Caller :: with_file_line ( "" , "22" ) ) ] // 22
2396+ #[ case( br#"file=x line="# , Caller :: with_file_line ( "x" , "" ) ) ] // 23
2397+ #[ case( br#"file="x" line="# , Caller :: with_file_line ( "x" , "" ) ) ] // 24
2398+ #[ case( br#"file="x" line=21 line=25"# , Caller :: with_file_line ( "x" , "25" ) ) ] // 25
2399+ #[ case( br#"file=x line=26 file=y"# , Caller :: with_file_line ( "y" , "26" ) ) ] // 26
2400+ #[ case( br#"{"file":123, "file": {}, "line":27}"# , Caller :: with_file_line ( " ", "27" ) ) ] // 27
2401+ #[ case( br#"{"caller":"a", "file": "b", "line":28}"# , Caller { name : "a" , file : "b" , line : "28" } ) ] // 28
2402+ #[ case( br#"{"file": "b", "line":{}}"# , Caller :: with_file_line ( "b" , "" ) ) ] // 29
2403+ fn test_caller_file_line ( #[ case] input : & [ u8 ] , #[ case] expected : Caller ) {
24012404 let mut predefined = PredefinedFields :: default ( ) ;
24022405 predefined. caller_file = Field {
24032406 names : vec ! [ "file" . into( ) ] ,
0 commit comments