@@ -653,20 +653,7 @@ fn assert_binop(ts: TokenStream, binop: BinOp) -> TokenStream {
653653 } ;
654654
655655 for val in vals {
656- let mut segments = Punctuated :: new ( ) ;
657- segments. push ( PathSegment {
658- ident : Ident2 :: new ( * val, Span2 :: call_site ( ) ) ,
659- arguments : PathArguments :: None ,
660- } ) ;
661-
662- log_args. push ( Expr :: Path ( ExprPath {
663- attrs : vec ! [ ] ,
664- qself : None ,
665- path : Path {
666- leading_colon : None ,
667- segments,
668- } ,
669- } ) ) ;
656+ log_args. push ( ident_expr ( * val) ) ;
670657 }
671658
672659 let log_stmt = match binop {
@@ -752,6 +739,71 @@ pub fn debug_assert_ne_(ts: TokenStream) -> TokenStream {
752739 . into ( )
753740}
754741
742+ #[ proc_macro]
743+ pub fn unwrap ( ts : TokenStream ) -> TokenStream {
744+ let assert = parse_macro_input ! ( ts as Assert ) ;
745+
746+ let condition = assert. condition ;
747+ let log_stmt = if let Some ( args) = assert. args {
748+ log (
749+ Level :: Error ,
750+ FormatArgs {
751+ litstr : LitStr :: new (
752+ & format ! ( "panicked at '{}'" , args. litstr. value( ) ) ,
753+ Span2 :: call_site ( ) ,
754+ ) ,
755+ rest : args. rest ,
756+ } ,
757+ )
758+ } else {
759+ let mut log_args = Punctuated :: new ( ) ;
760+ log_args. push ( ident_expr ( "_unwrap_err" ) ) ;
761+
762+ log (
763+ Level :: Error ,
764+ FormatArgs {
765+ litstr : LitStr :: new (
766+ & format ! (
767+ "panicked at 'unwrap failed: {}'
768+ error: `{{:?}}`" ,
769+ escape_expr( & condition)
770+ ) ,
771+ Span2 :: call_site ( ) ,
772+ ) ,
773+ rest : Some ( ( syn:: token:: Comma :: default ( ) , log_args) ) ,
774+ } ,
775+ )
776+ } ;
777+
778+ quote ! (
779+ match defmt:: export:: into_result( #condition) {
780+ :: core:: result:: Result :: Ok ( res) => res,
781+ :: core:: result:: Result :: Err ( _unwrap_err) => {
782+ #log_stmt;
783+ defmt:: export:: panic( )
784+ }
785+ }
786+ )
787+ . into ( )
788+ }
789+
790+ fn ident_expr ( name : & str ) -> Expr {
791+ let mut segments = Punctuated :: new ( ) ;
792+ segments. push ( PathSegment {
793+ ident : Ident2 :: new ( name, Span2 :: call_site ( ) ) ,
794+ arguments : PathArguments :: None ,
795+ } ) ;
796+
797+ Expr :: Path ( ExprPath {
798+ attrs : vec ! [ ] ,
799+ qself : None ,
800+ path : Path {
801+ leading_colon : None ,
802+ segments,
803+ } ,
804+ } )
805+ }
806+
755807fn escape_expr ( expr : & Expr ) -> String {
756808 let q = quote ! ( #expr) ;
757809 q. to_string ( ) . replace ( "{" , "{{" ) . replace ( "}" , "}}" )
0 commit comments