       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
     761
     762
     763
     764
     765
     766
     767
     768
     769
     770
     771
     772
     773
     774
     775
     776
     777
     778
     779
     780
     781
     782
     783
     784
     785
     786
     787
     788
     789
     790
     791
     792
     793
     794
     795
     796
     797
     798
     799
     800
     801
     802
     803
     804
     805
     806
     807
     808
     809
     810
     811
     812
     813
     814
     815
     816
     817
     818
     819
     820
     821
     822
     823
     824
     825
     826
     827
     828
     829
     830
     831
     832
     833
     834
     835
     836
     837
     838
     839
     840
     841
     842
     843
     844
     845
     846
     847
     848
     849
     850
     851
     852
     853
     854
     855
     856
     857
     858
     859
     860
     861
     862
     863
     864
     865
     866
     867
     868
     869
     870
     871
     872
     873
     874
     875
     876
     877
     878
     879
     880
     881
     882
     883
     884
     885
     886
     887
     888
     889
     890
     891
     892
     893
     894
     895
     896
     897
     898
     899
     900
     901
     902
     903
     904
     905
     906
     907
     908
     909
     910
     911
     912
     913
     914
     915
     916
     917
     918
     919
     920
     921
     922
     923
     924
     925
     926
     927
     928
     929
     930
     931
     932
     933
     934
     935
     936
     937
     938
     939
     940
     941
     942
     943
     944
     945
     946
     947
     948
     949
     950
     951
     952
     953
     954
     955
     956
     957
     958
     959
     960
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178

    //!  Mynewt Macros for Rust. Note that macros defined locally should be called with `$crate::`, like `$crate::parse`.
    //!  This works with Rust compiler versions 1.30 and later.  See https://doc.rust-lang.org/stable/edition-guide/rust-2018/macros/macro-changes.html
    //!  To see the expanded macros: `cargo rustc -- -Z unstable-options --pretty expanded`

    ///////////////////////////////////////////////////////////////////////////////
    //  CoAP Macros

    ///  Macro to compose a CoAP payload with JSON or CBOR encoding.
    ///  First parameter is `@none`, `@json`, `@cbor` or `@cbormin`, to indicate
    ///  no encoding (testing), JSON encoding, CBOR encoding for thethings.io or CBOR minimal key-value encoding.
    ///  JSON and CBOR encoding looks like: `{ values: [{key:..., value:...}, ...] }`.
    ///  CBOR Minimal encoding looks like: `{ key: value, ... }`.
    ///  Second parameter is the JSON message to be transmitted.
    ///  Adapted from the `json!()` macro: https://docs.serde.rs/src/serde_json/macros.rs.html
    #[macro_export]
    macro_rules! coap {
      //  No encoding
      (@none $($tokens:tt)+) => {
        $crate::parse!(@none $($tokens)+)
      };
      //  JSON encoding
      (@json $($tokens:tt)+) => {
        $crate::parse!(@json $($tokens)+)
      };
      //  CBOR encoding
      (@cbor $($tokens:tt)+) => {
        $crate::parse!(@cbor $($tokens)+)
      };
      //  CBOR Minimal key-value encoding
      (@cbormin $($tokens:tt)+) => {
        $crate::parse!(@cbormin $($tokens)+)
      };
    }

    ///  Parse the JSON code in the parameter and compose the CoAP payload.
    ///  This macro takes these parameters:
    ///  - __Encoding__: `@json`, `@cbor` or `@none`
    ///  - __State__: Current parsing state (`@object`, `@array` or omitted)
    ///  - __Context__: JSON or CBOR parsing context (`JsonContext` or `CborContext`)
    ///  - __Remaining tokens__ to be parsed
    ///  - __Remaining tokens__ again, for error display
    #[macro_export]
    macro_rules! parse {

      //////////////////////////////////////////////////////////////////////////
      // TT muncher for parsing the inside of an object {...}. Each entry is
      // inserted into the given map variable.
      //
      // Must be invoked as: $crate::parse!(@$enc @object $map () ($($tt)*) ($($tt)*))
      //
      // We require two copies of the input tokens so that we can match on one
      // copy and trigger errors on the other copy.
      //////////////////////////////////////////////////////////////////////////

      // Done.
      (@$enc:ident @object $object:ident () () ()) => {};

      // No Encoding: Insert the current entry followed by trailing comma.
      (@none @object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
        d!(TODO: add key: $($key)+, value: $value, to object: $object);

        //  Previously:
        //  let _ = $object.insert(($($key)+).into(), $value);

        //  Continue expanding the rest of the JSON.
        $crate::parse!(@none @object $object () ($($rest)*) ($($rest)*));
      };

      // JSON and CBOR Encoding: Insert the current entry followed by trailing comma.
      (@$enc:ident @object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
        d!(add1 key: $($key)+ value: $value to object: $object);

        //  Append to the "values" array e.g.
        //    {"key":"device", "value":"0102030405060708090a0b0c0d0e0f10"},
        $crate::coap_item_str!(@$enc $object, $($key)+, $value);
        "--------------------";

        //  Previously:
        //  let _ = $object.insert(($($key)+).into(), $value);

        //  Continue expanding the rest of the JSON.
        $crate::parse!(@$enc @object $object () ($($rest)*) ($($rest)*));
      };

      // Current entry followed by unexpected token.
      (@$enc:ident @object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
        unexpected_token!($unexpected);
      };

      // Insert the last entry without trailing comma.
      (@$enc:ident @object $object:ident [$($key:tt)+] ($value:expr)) => {
        //  TODO
        d!(TODO: add2 key: $($key)+ value: $value to object: $object);
        //  let _ = $object.insert(($($key)+).into(), $value);
      };

      // Next value is `null`.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc null)) $($rest)*);
      };

      // Next value is `true`.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc true)) $($rest)*);
      };

      // Next value is `false`.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc false)) $($rest)*);
      };

      // Next value is an array.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc [$($array)*])) $($rest)*);
      };

      // Next value is a map.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc {$($map)*})) $($rest)*);
      };

      // Next value is an expression followed by comma.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc $value)) , $($rest)*);
      };

      // Last value is an expression with no trailing comma.
      (@$enc:ident @object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
        $crate::parse!(@$enc @object $object [$($key)+]
          ($crate::parse!(@$enc $value)));
      };

      // Missing value for last entry. Trigger a reasonable error message.
      (@$enc:ident @object $object:ident ($($key:tt)+) (:) $copy:tt) => {
        // "unexpected end of macro invocation"
        $crate::parse!();
      };

      // Missing colon and value for last entry. Trigger a reasonable error
      // message.
      (@$enc:ident @object $object:ident ($($key:tt)+) () $copy:tt) => {
        // "unexpected end of macro invocation"
        $crate::parse!();
      };

      // Misplaced colon. Trigger a reasonable error message.
      (@$enc:ident @object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
        // Takes no arguments so "no rules expected the token `:`".
        $crate::unexpected_token!($colon);
      };

      // No Encoding: Found a key followed by a comma. Assume this is a SensorValue type with key and value.
      (@none @object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
        d!(TODO: extract key, value from _sensor_value: $($key)* and add to _object: $object);
        "--------------------";
        //  Continue expanding the rest of the JSON.
        $crate::parse!(@none @object $object () ($($rest)*) ($($rest)*));
      };

      // JSON Encoding: Found a key followed by a comma. Assume this is a SensorValue type with key and value.  Encode as `{key:..., value:...}`.
      (@json @object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
        "--------------------";
        $crate::coap_item_int_val!(@json
          $object,  //  _object,
          $($key)*  //  _sensor_value
        );
        "--------------------";
        //  Continue expanding the rest of the JSON.
        $crate::parse!(@json @object $object () ($($rest)*) ($($rest)*));
      };

      // CBOR Encoding: Found a key followed by a comma. Assume this is a SensorValue type with key and value.  Encode as `{key:..., value:...}`.
      (@cbor @object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
        "--------------------";
        $crate::coap_item_int_val!(@cbor
          $object,  //  _object,
          $($key)*  //  _sensor_value
        );
        "--------------------";
        //  Continue expanding the rest of the JSON.
        $crate::parse!(@cbor @object $object () ($($rest)*) ($($rest)*));
      };

      // CBOR Minimal Encoding: Found a key followed by a comma. Assume this is a SensorValue type with key and value.  Encode as `{key: value}`.
      (@cbormin @object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
        "--------------------";
        $crate::coap_set_int_val!(@cbor
          $object,  //  _object,
          $($key)*  //  _sensor_value
        );
        "--------------------";
        //  Continue expanding the rest of the JSON.
        $crate::parse!(@cbor @object $object () ($($rest)*) ($($rest)*));
      };

      // Previously: Found a comma inside a key. Trigger a reasonable error message.
      // Takes no arguments so "no rules expected the token `,`".
      ////unexpected_token!($comma);

      // Key is fully parenthesized. This avoids clippy double_parens false
      // positives because the parenthesization may be necessary here.
      (@$enc:ident @object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
        d!( got () );
        $crate::parse!(@$enc @object $object ($key) (: $($rest)*) (: $($rest)*));
      };

      // Munch a token into the current key.
      (@$enc:ident @object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
        $crate::nx!( ($($key)*), ($tt), ($($rest)*) );
        //  Parse the next token while we are in the @object state.
        $crate::parse!(@$enc @object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
      };

      //////////////////////////////////////////////////////////////////////////
      // TT muncher for parsing the inside of an array [...]. Produces a vec![...]
      // of the elements.
      //
      // Must be invoked as: $crate::parse!(@$enc @array [] $($tt)*)
      //////////////////////////////////////////////////////////////////////////

      // Done with trailing comma.
      (@$enc:ident @array [$($elems:expr,)*]) => {
        parse_vector![$($elems,)*]
      };

      // Done without trailing comma.
      (@$enc:ident @array [$($elems:expr),*]) => {
        parse_vector![$($elems),*]
      };

      // Next element is `null`.
      (@$enc:ident @array [$($elems:expr,)*] null $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc null)] $($rest)*)
      };

      // Next element is `true`.
      (@$enc:ident @array [$($elems:expr,)*] true $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc true)] $($rest)*)
      };

      // Next element is `false`.
      (@$enc:ident @array [$($elems:expr,)*] false $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc false)] $($rest)*)
      };

      // Next element is an array.
      (@$enc:ident @array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc [$($array)*])] $($rest)*)
      };

      // Next element is a map.
      (@$enc:ident @array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc {$($map)*})] $($rest)*)
      };

      // Next element is an expression followed by comma.
      (@$enc:ident @array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc $next),] $($rest)*)
      };

      // Last element is an expression with no trailing comma.
      (@$enc:ident @array [$($elems:expr,)*] $last:expr) => {
        $crate::parse!(@$enc @array [$($elems,)*
          $crate::parse!(@$enc $last)])
      };

      // Comma after the most recent element.
      (@$enc:ident @array [$($elems:expr),*] , $($rest:tt)*) => {
        $crate::parse!(@$enc @array [$($elems,)*] $($rest)*)
      };

      // Unexpected token after most recent element.
      (@$enc:ident @array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
        $crate::unexpected_token!($unexpected)
      };

      //////////////////////////////////////////////////////////////////////////
      // The main implementation.
      //
      // Must be invoked as: $crate::parse!(@$enc $($tokens)+) where $enc is json, cbor or none
      //////////////////////////////////////////////////////////////////////////

      (@$enc:ident null) => {{
        d!(TODO: null); "null"
      }};  //  Previously: $crate::Value::Null

      (@$enc:ident true) => {
        //  TODO
        { d!(true); "true" }
        //  Previously:
        //  $crate::Value::Bool(true)
      };

      (@$enc:ident false) => {
        //  TODO
        { d!(false); "false" }
        //  Previously:
        //  $crate::Value::Bool(false)
      };

      (@$enc:ident []) => {
        //  TODO
        { d!([ TODO ]); "[ TODO ]" }
        //  Previously:
        //  $crate::Value::Array(parse_vector![])
      };

      (@$enc:ident [ $($tt:tt)+ ]) => {
        //  TODO
        {
          d!(begin array);
          _array = $crate::parse!(@$enc @array [] $($tt)+);
          d!(end array);
          "[ TODO ]"
        }
        //  Previously:
        //  $crate::Value::Array($crate::parse!(@array [] $($tt)+))
      };

      (@$enc:ident {}) => {
        //  TODO
        { d!({ TODO }); "{ TODO }" }
        //  Previously:
        //  $crate::Value::Object($crate::Map::new())
      };

      //  No encoding: If we match the top level of the JSON: { ... }
      (@none { $($tt:tt)+ }) => {{
        //  Substitute with this code...
        d!(begin none root);
        let root = _ROOT;  //  Top level object is named "root".
        //  Expand the items inside { ... } and add them to root.
        $crate::parse!(@none @object root () ($($tt)+) ($($tt)+));
        d!(end none root);
        d!(return none root to caller);
        root
      }};

      //  JSON encoding: If we match the top level of the JSON: { ... }
      (@json { $($tt:tt)+ }) => {{
        //  Substitute with this code...
        d!(begin json root);
        $crate::coap_root!(@json COAP_CONTEXT {  //  Create the payload root
            $crate::coap_array!(@json COAP_CONTEXT, values, {  //  Create "values" as an array of items under the root
              //  Expand the items inside { ... } and add them to values.
              $crate::parse!(@json @object COAP_CONTEXT () ($($tt)+) ($($tt)+));
            });  //  Close the "values" array
        });  //  Close the payload root
        d!(end json root);
        ()
      }};

      //  CBOR encoding: If we match the top level of the JSON: { ... }
      (@cbor { $($tt:tt)+ }) => {{
        //  Substitute with this code...
        d!(begin cbor root);
        $crate::coap_root!(@cbor root {  //  Create the payload root
            $crate::coap_array!(@cbor root, values, {  //  Create "values" as an array of items under the root
              //  Expand the items inside { ... } and add them to values.
              $crate::parse!(@cbor @object values () ($($tt)+) ($($tt)+));
            });  //  Close the "values" array
        });  //  Close the payload root
        d!(end cbor root);
        ()
      }};

      //  CBOR minimal encoding: If we match the top level of the JSON: { ... }
      (@cbormin { $($tt:tt)+ }) => {{
        //  Substitute with this code...
        d!(begin cbor root);
        $crate::coap_root!(@cbormin COAP_CONTEXT {  //  Create the payload root
            //  Expand the items inside { ... } and add them to root.
            $crate::parse!(@cbormin @object COAP_CONTEXT () ($($tt)+) ($($tt)+));  //  TODO: Change COAP_CONTEXT to CBOR
        });  //  Close the payload root
        d!(end cbor root);
        ()
      }};

      /* Previously substitute with:
      $crate::Value::Object({
        let mut object = $crate::Map::new();
        $crate::parse!(@object object () ($($tt)+) ($($tt)+));
        object
      })
      */

      // Any Serialize type: numbers, strings, struct literals, variables etc.
      // Must be below every other rule.
      (@$enc:ident $other:expr) => {
        //  Return itself.
        $other
      };  //  Previously: $crate::to_value(&$other).unwrap()
    }

    ///  TODO: Parse the vector e.g. array items
    #[macro_export]
    macro_rules! parse_vector {
      ($($content:tt)*) => {
        $crate::vec![$($content)*]
      };
    }

    ///  Show an unexpected token error
    #[macro_export]
    macro_rules! unexpected_token {
      () => {};
    }

    ///////////////////////////////////////////////////////////////////////////////
    //  CoAP macros ported from C to Rust:
    //  https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/rust-coap/libs/sensor_coap/include/sensor_coap/sensor_coap.h

    ///  Compose the payload root.
    #[macro_export]
    macro_rules! coap_root {
      (@cbor $context:ident $children0:block) => {{  //  CBOR
        d!(begin cbor coap_root);
        //  Set the payload format.
        unsafe { mynewt::libs::sensor_network::prepare_post(mynewt::encoding::APPLICATION_CBOR) ? ; }
        $crate::oc_rep_start_root_object!($context);
        $children0;
        $crate::oc_rep_end_root_object!($context);
        d!(end cbor coap_root);
      }};

      (@json $context:ident $children0:block) => {{  //  JSON
        d!(begin json coap_root);
        //  Set the payload format.
        unsafe { mynewt::libs::sensor_network::prepare_post(mynewt::encoding::APPLICATION_JSON) ? ; }
        unsafe { mynewt::libs::sensor_coap::json_rep_start_root_object(); }
        $children0;
        unsafe { mynewt::libs::sensor_coap::json_rep_end_root_object(); }
        d!(end json coap_root);
      }};
    }

    ///  Compose an array under `object`, named as `key` (e.g. `values`).  Add `children` as array elements.
    #[macro_export]
    macro_rules! coap_array {
      (@cbor $object0:ident, $key0:ident, $children0:block) => {{  //  CBOR
        d!(begin cbor coap_array, object: $object0, key: $key0);
        $crate::oc_rep_set_array!($object0, $key0);
        $children0;
        $crate::oc_rep_close_array!($object0, $key0);
        d!(end cbor coap_array);
      }};

      (@json $object0:ident, $key0:ident, $children0:block) => {{  //  JSON
        d!(begin json coap_array, object: $object0, key: $key0);
        $crate::json_rep_set_array!($object0, $key0);
        $children0;
        $crate::json_rep_close_array!($object0, $key0);
        d!(end json coap_array);
      }};
    }

    ///  Append a (key + int value) item to the array named `array`:
    ///    `{ <array>: [ ..., {"key": <key0>, "value": <value0>} ], ... }`
    #[macro_export]
    macro_rules! coap_item_int {
      (@cbor $array0:ident, $key0:expr, $value0:expr) => {{  //  CBOR
        d!(begin cbor coap_item_int, key: $key0, value: $value0);
        $crate::coap_item!(@cbor $array0, {
          $crate::oc_rep_set_text_string!($array0, "key",   $key0);
          $crate::oc_rep_set_int!(        $array0, "value", $value0);
        });
        d!(end cbor coap_item_int);
      }};

      (@json $array0:ident, $key0:expr, $value0:expr) => {{  //  JSON
        d!(begin json coap_item_int, key: $key0, value: $value0);
        $crate::coap_item!(@json $array0, {
          $crate::json_rep_set_text_string!($array0, "key",   $key0);
          $crate::json_rep_set_int!(        $array0, "value", $value0);
        });
        d!(end json coap_item_int);
      }};
    }

    ///  Append a (`key` + `val` string value) item to the array named `parent`:
    ///    `{ <parent>: [ ..., {"key": <key>, "value": <val>} ] }`
    #[macro_export]
    macro_rules! coap_item_str {
      (@cbor $parent:ident, $key:expr, $val:expr) => {{  //  CBOR
        d!(begin cbor coap_item_str, parent: $parent, key: $key, val: $val);
        $crate::coap_item!(@cbor
          $parent,
          {
            $crate::oc_rep_set_text_string!($parent, "key", $key);
            $crate::oc_rep_set_text_string!($parent, "value", $val);
          }
        );
        d!(end cbor coap_item_str);
      }};

      (@json $parent:ident, $key:expr, $val:expr) => {{  //  JSON
        d!(begin json coap_item_str, parent: $parent, key: $key, val: $val);
        $crate::coap_item!(@json
          $parent,
          {
            $crate::json_rep_set_text_string!($parent, key, $key);
            $crate::json_rep_set_text_string!($parent, value, $val);
          }
        );
        d!(end json coap_item_str);
      }};
    }

    ///  Append an array item under the current object item.  Add `children0` as the array items.
    ///    `{ <array0>: [ ..., { <children0> } ] }`
    #[macro_export]
    macro_rules! coap_item {
      (@cbor $context:ident, $children0:block) => {{  //  CBOR
        d!(begin cbor coap_item, array: $context);
        $crate::oc_rep_object_array_start_item!($context);
        $children0;
        $crate::oc_rep_object_array_end_item!($context);
        d!(end cbor coap_item);
      }};

      (@json $context:ident, $children0:block) => {{  //  JSON
        d!(begin json coap_item, array: $context);
        $crate::json_rep_object_array_start_item!($context);
        $children0;
        $crate::json_rep_object_array_end_item!($context);
        d!(end json coap_item);
      }};
    }

    ///  Given an object parent and an integer Sensor Value `val`, set the `val`'s key/value in the object.
    #[macro_export]
    macro_rules! coap_set_int_val {
      (@cbor $context:ident, $val0:expr) => {{  //  CBOR
        d!(begin cbor coap_set_int_val, c: $context, val: $val0);
        if let SensorValueType::Uint(val) = $val0.val {
          $crate::oc_rep_set_int!($context, $val0.key, val);
        } else {
          unsafe { COAP_CONTEXT.fail(CoapError::VALUE_NOT_UINT) };  //  Value not uint
        }
        d!(end cbor coap_set_int_val);
      }};

      (@json $context:ident, $val0:expr) => {{  //  JSON
        d!(begin json coap_set_int_val, c: $context, val: $val0);
        if let SensorValueType::Uint(val) = $val0.val {
          $crate::json_rep_set_int!($context, $val0.key, val);
        } else {
          unsafe { COAP_CONTEXT.fail(CoapError::VALUE_NOT_UINT) };  //  Value not uint
        }
        d!(end json coap_set_int_val);
      }};
    }

    ///  Create a new Item object in the parent array and set the Sensor Value's key/value (integer).
    #[macro_export]
    macro_rules! coap_item_int_val {
      (@cbor $context:ident, $val0:expr) => {{  //  CBOR
        d!(begin cbor coap_item_int_val, c: $context, val: $val0);
        if let SensorValueType::Uint(val) = $val0.val {
          $crate::coap_item_int!(@cbor $context, $val0.key, val);
        } else {
          unsafe { COAP_CONTEXT.fail(CoapError::VALUE_NOT_UINT) };  //  Value not uint
        }
        d!(end cbor coap_item_int_val);
      }};

      (@json $context:ident, $val0:expr) => {{  //  JSON
        d!(begin json coap_item_int_val, c: $context, val: $val0);
        if let SensorValueType::Uint(val) = $val0.val {
          $crate::coap_item_int!(@json $context, $val0.key, val);
        } else {
          unsafe { COAP_CONTEXT.fail(CoapError::VALUE_NOT_UINT) };  //  Value not uint
        }
        d!(end json coap_item_int_val);
      }};
    }

    ///////////////////////////////////////////////////////////////////////////////
    //  JSON Sensor CoAP macros ported from C to Rust:
    //  https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/rust-coap/libs/sensor_coap/include/sensor_coap/sensor_coap.h

    ///  Assume we are writing an object now.  Write the key name and start a child array.
    ///  ```
    ///  {a:b --> {a:b, key:[
    ///  ```
    #[macro_export]
    macro_rules! json_rep_set_array {
      ($context:ident, $key:ident) => {{  //  If $key is identifier...
        concat!(
          "<< jarri ",
          ", o: ", stringify!($context),
          ", k: ", stringify!($key)
        );
        //  TODO: Switch to $context.json_set_array()
        //  Convert key to null-terminated char array. If key is `device`, convert to `"device\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($key);
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_set_array(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_null.as_bytes())
          );
        };
      }};

      ($context:ident, $key:expr) => {{  //  If $key is expression...
        concat!(
          "<< jarre ",
          ", o: ", stringify!($context),
          ", k: ", stringify!($key)
        );
        //  TODO: Switch to $context.json_set_array()
        //  Convert key to char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $key.to_bytes_optional_nul();
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_set_array(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_opt_null)
          );
        };
      }};
    }

    ///  End the child array and resume writing the parent object.
    ///  ```
    ///  {a:b, key:[... --> {a:b, key:[...]
    ///  ```
    #[macro_export]
    macro_rules! json_rep_close_array {
      ($context:ident, $key:ident) => {{  //  If $key is identifier...
        concat!(
          ">>"
        );
        //  TODO: Switch to $context.json_close_array()
        //  Convert key to null-terminated char array. If key is `device`, convert to `"device\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($key);
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_close_array(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_null.as_bytes())
          )
        };
      }};

      ($context:ident, $key:expr) => {{  //  If $key is expression...
        concat!(
          ">>"
        );
        //  TODO: Switch to $context.json_close_array()
        //  Convert key to char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $key.to_bytes_optional_nul();
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_close_array(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_opt_null)
          )
        };
      }};
    }

    ///  Assume we have called `set_array`.  Start an array item, assumed to be an object.
    ///  ```
    ///  [... --> [...,
    ///  ```
    #[macro_export]
    macro_rules! json_rep_object_array_start_item {
      ($context:ident) => {{  //  If $key is identifier...
        concat!(
          "<< jitmi",
          " c: ", stringify!($context)
        );
        //  TODO: Switch to $context.json_object_array_start_item()
        //  Convert key to null-terminated char array. If key is `device`, convert to `"device\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($context);    //  TODO
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_object_array_start_item(
            $context.key_to_cstr(key_with_null.as_bytes())
          )
        };
      }};

      ($context:ident) => {{  //  If $key is expression...
        concat!(
          "<< jitme",
          " c: ", stringify!($context)
        );
        //  TODO: Switch to $context.json_object_array_start_item()
        //  Convert key char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $context.to_bytes_optional_nul();  //  TODO
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_object_array_start_item(
            $context.key_to_cstr(key_with_opt_null)
          )
        };
      }};
    }

    ///  End an array item, assumed to be an object.
    ///  ```
    ///  [... --> [...,
    ///  ```
    #[macro_export]
    macro_rules! json_rep_object_array_end_item {
      ($context:ident) => {{  //  If $key is identifier...
        concat!(
          ">>"
        );
        //  TODO: Switch to $context.json_object_array_end_item()
        //  Convert key to null-terminated char array. If key is `device`, convert to `"device\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($context);  //  TODO
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_object_array_end_item(
            $context.key_to_cstr(key_with_null.as_bytes())
          )
        };
      }};

      ($context:ident) => {{  //  If $key is expression...
        concat!(
          ">>"
        );
        //  TODO: Switch to $context.json_object_array_end_item()
        //  Convert key char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $context.to_bytes_optional_nul();  //  TODO
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_object_array_end_item(
            $context.key_to_cstr(key_with_opt_null)
          )
        };
      }};
    }

    ///  Encode an int value into the current JSON encoding value `coap_json_value`
    #[macro_export]
    macro_rules! json_rep_set_int {
      ($context:ident, $key:ident, $value:expr) => {{  //  If $key is identifier...
        concat!(
          "-- jinti",
          " o: ", stringify!($context),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  TODO: Switch to $context.json_set_int()
        //  Convert key to null-terminated char array. If key is `device`, convert to `"device\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($key);
        let value = $value as u64;
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_set_int(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_null.as_bytes()),
            value
          )
        };
      }};

      ($context:ident, $key:expr, $value:expr) => {{  //  If $key is expression...
        concat!(
          "-- jinte",
          " o: ", stringify!($context),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  TODO: Switch to $context.json_set_int()
        //  Convert key to char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $key.to_bytes_optional_nul();
        let value = $value as u64;
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_set_int(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_opt_null),
            value
          )
        };
      }};
    }

    ///  Encode a text value into the current JSON encoding value `coap_json_value`
    #[macro_export]
    macro_rules! json_rep_set_text_string {
      ($context:ident, $key:ident, $value:expr) => {{  //  If $key is identifier...
        concat!(
          "-- jtxti",
          " o: ", stringify!($context),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  Convert key and value to Strn.
        let key_strn: &Strn = strn!(stringify!($key));
        let value_strn: &Strn = strn!($value);
        unsafe { $context.json_set_text_string(key_strn, value_strn) };
      }};

      ($context:ident, $key:expr, $value:expr) => {{  //  If $key is expression...
        concat!(
          "-- jtxte",
          " o: ", stringify!($context),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  TODO: Switch to $context.json_set_text_string()
        //  Convert key and value to char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $key.to_bytes_optional_nul();
        let value_with_opt_null: &[u8] = $value.to_bytes_optional_nul();
        unsafe {
          mynewt::libs::mynewt_rust::json_helper_set_text_string(
            $context.to_void_ptr(),
            $context.key_to_cstr(key_with_opt_null),
            $context.value_to_cstr(value_with_opt_null)
          )
        };
      }};
    }

    //  TODO
    //  Encode an unsigned int value into the current JSON encoding value `coap_json_value`
    //  void json_helper_set_uint(void *object, const char *key, uint64_t value);

    //  Encode a float value into the current JSON encoding value `coap_json_value`
    //  void json_helper_set_float(void *object, const char *key, float value);

    ///////////////////////////////////////////////////////////////////////////////
    //  CBOR macros ported from C to Rust. First parameter `obj` is the name of the current object or array being encoded.
    //  Based on: https://github.com/apache/mynewt-core/blob/master/net/oic/include/oic/oc_rep.h

    #[macro_export]
    macro_rules! oc_rep_start_root_object {
      ($obj:ident) => {{
        d!(begin oc_rep_start_root_object);
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(_ROOT, _MAP);
          //  Previously: g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength)
          cbor_encoder_create_map(
            COAP_CONTEXT.global_encoder(),
            encoder,
            mynewt::encoding::tinycbor::CborIndefiniteLength
          );
        });
        d!(end oc_rep_start_root_object);
      }};
    }

    #[macro_export]
    macro_rules! oc_rep_end_root_object {
      ($obj:ident) => {{
        d!(begin oc_rep_end_root_object);
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(_ROOT, _MAP);
          //  Previously: g_err |= cbor_encoder_close_container(&g_encoder, &root_map)
          cbor_encoder_close_container(
            COAP_CONTEXT.global_encoder(),
            encoder
          );
        });
        d!(end oc_rep_end_root_object);
      }};
    }

    #[macro_export]
    macro_rules! oc_rep_start_object {
      ($parent:ident, $key:ident, $parent_suffix:ident) => {{
        concat!(
          "begin oc_rep_start_object ",
          ", parent: ", stringify!($parent), stringify!($parent_suffix),  //  parent##parent_suffix
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_map"  //  key##_map
        );
        mynewt_macros::try_cbor!({
          let parent_encoder = COAP_CONTEXT.encoder(
            stringify!($parent),
            stringify!($parent_suffix)
          );
          //  Previously: CborEncoder key##_map
          let encoder = COAP_CONTEXT.new_encoder(
            stringify!($key),
            _MAP
          );
          //  Previously: g_err |= cbor_encoder_create_map(&parent, &key##_map, CborIndefiniteLength)
          cbor_encoder_create_map(
            parent_encoder,
            encoder,
            mynewt::encoding::tinycbor::CborIndefiniteLength
          );
        });
        d!(end oc_rep_start_object);
      }};
    }

    #[macro_export]
    macro_rules! oc_rep_end_object {
      ($parent:ident, $key:ident, $parent_suffix:ident) => {{
        concat!(
          "begin oc_rep_end_object ",
          ", parent: ", stringify!($parent), stringify!($parent_suffix),  //  parent##parent_suffix
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_map"  //  key##_map
        );
        mynewt_macros::try_cbor!({
          let parent_encoder = COAP_CONTEXT.encoder(
            stringify!($parent),
            stringify!($parent_suffix)
          );
          let encoder = COAP_CONTEXT.encoder(
            stringify!($key),
            _MAP
          );
          //  Previously: g_err |= cbor_encoder_close_container(&parent, &key##_map)
          cbor_encoder_close_container(
            parent_encoder,
            encoder
          );
        });
        d!(end oc_rep_end_object);
      }};
    }

    #[macro_export]
    macro_rules! oc_rep_start_array {
      ($parent:ident, $key:ident, $parent_suffix:ident) => {{
        concat!(
          "begin oc_rep_start_array ",
          ", parent: ", stringify!($parent), stringify!($parent_suffix),  //  parent##parent_suffix
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_array"  //  key##_array
        );
        mynewt_macros::try_cbor!({
          let parent_encoder = COAP_CONTEXT.encoder(
            stringify!($parent),
            stringify!($parent_suffix)
          );
          //  Previously: CborEncoder key ##_array;
          let encoder = COAP_CONTEXT.new_encoder(
            stringify!($key),
            _ARRAY
          );
          //  Previously: g_err |= cbor_encoder_create_array(&parent, &key##_array, CborIndefiniteLength));
          cbor_encoder_create_array(
            parent_encoder,
            encoder,
            mynewt::encoding::tinycbor::CborIndefiniteLength
          );
        });
        d!(end oc_rep_start_array);
      }};
    }

    #[macro_export]
    macro_rules! oc_rep_end_array {
      ($parent:ident, $key:ident, $parent_suffix:ident) => {{
        concat!(
          "begin oc_rep_end_array ",
          ", parent: ", stringify!($parent), stringify!($parent_suffix),  //  parent##parent_suffix
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_array"  //  key##_array
        );
        mynewt_macros::try_cbor!({
          let parent_encoder = COAP_CONTEXT.encoder(
            stringify!($parent),
            stringify!($parent_suffix)
          );
          let encoder = COAP_CONTEXT.encoder(
            stringify!($key),
            _ARRAY
          );
          //  Previously: g_err |= cbor_encoder_close_container(&parent, &key##_array)
          cbor_encoder_close_container(
            parent_encoder,
            encoder
          );
        });
        d!(end oc_rep_end_array);
      }};
    }

    ///  Assume we are writing an object now.  Write the key name and start a child array.
    ///  ```
    ///  {a:b --> {a:b, key:[
    ///  ```
    #[macro_export]
    macro_rules! oc_rep_set_array {
      ($object:ident, $key:ident) => {{
        concat!(
          "begin oc_rep_set_array ",
          ", object: ", stringify!($object),
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($object), "_map"  //  object##_map
        );
        //  Convert key to char array, which may or may not be null-terminated.
        let key_with_opt_null:   &[u8] = stringify!($key).to_bytes_optional_nul();
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(
            stringify!($object),
            _MAP
          );
          //  Previously: g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key))
          cbor_encode_text_string(
            encoder,
            COAP_CONTEXT.key_to_cstr(key_with_opt_null),
            COAP_CONTEXT.cstr_len(key_with_opt_null)
          );
        });
        //  Previously: oc_rep_start_array!(object##_map, key)
        $crate::oc_rep_start_array!($object, $key, _map);
        d!(end oc_rep_set_array);
      }};
    }

    ///  End the child array and resume writing the parent object.
    ///  ```
    ///  {a:b, key:[... --> {a:b, key:[...]
    ///  ```
    #[macro_export]
    macro_rules! oc_rep_close_array {
      ($object:ident, $key:ident) => {{
        concat!(
          "begin oc_rep_close_array ",
          ", object: ", stringify!($object),
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($object), "_map"  //  object##_map
        );
        //  Previously: oc_rep_end_array(object##_map, key));
        $crate::oc_rep_end_array!($object, $key, _map);
        d!(end oc_rep_close_array);
      }};
    }

    ///  Assume we have called `set_array`.  Start an array item, assumed to be an object.
    ///  ```
    ///  [... --> [...,
    ///  ```
    #[macro_export]
    macro_rules! oc_rep_object_array_start_item {
      ($key:ident) => {{
        concat!(
          "begin oc_rep_object_array_start_item ",
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_array",  //  key##_array
        );
        //  Previously: oc_rep_start_object(key##_array, key));
        $crate::oc_rep_start_object!($key, $key, _array);
        d!(end oc_rep_object_array_start_item);
      }};
    }

    ///  End an array item, assumed to be an object.
    ///  ```
    ///  [... --> [...,
    ///  ```
    #[macro_export]
    macro_rules! oc_rep_object_array_end_item {
      ($key:ident) => {{
        concat!(
          "begin oc_rep_object_array_end_item ",
          ", key: ",    stringify!($key),
          ", child: ",  stringify!($key), "_array",  //  key##_array
        );
        //  Previously: oc_rep_end_object(key##_array, key));
        $crate::oc_rep_end_object!($key, $key, _array);
        d!(end oc_rep_object_array_end_item);
      }};
    }

    ///  Encode an int value
    #[macro_export]
    macro_rules! oc_rep_set_int {
      ($obj:ident, $key:ident, $value:expr) => {  //  If $key is identifier...
        concat!(
          "-- cinti",
          " c: ",  stringify!($obj),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  Convert key to null-terminated char array. If key is `t`, convert to `"t\u{0}"`
        let key_with_null: &str = $crate::stringify_null!($key);
        let value = $value as i64;
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(
            stringify!($obj),
            _MAP
          );
          //  Previously: g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key))
          cbor_encode_text_string(
            encoder,
            COAP_CONTEXT.key_to_cstr(key_with_null.as_bytes()),
            COAP_CONTEXT.cstr_len(key_with_null.as_bytes())
          );
          //  Previously: g_err |= cbor_encode_int(&object##_map, value)
          cbor_encode_int(
            encoder,
            value
          );
        });
      };

      ($obj:ident, $key:expr, $value:expr) => {  //  If $key is expression...
        concat!(
          "-- cinte",
          " c: ",  stringify!($obj),
          ", k: ", stringify!($key),
          ", v: ", stringify!($value)
        );
        //  Convert key to char array, which may or may not be null-terminated.
        let key_with_opt_null: &[u8] = $key.to_bytes_optional_nul();
        let value = $value as i64;
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(
            stringify!($obj),
            _MAP
          );
          //  Previously: g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key))
          cbor_encode_text_string(
            encoder,
            COAP_CONTEXT.key_to_cstr(key_with_opt_null),
            COAP_CONTEXT.cstr_len(   key_with_opt_null)
          );
          //  Previously: g_err |= cbor_encode_int(&object##_map, value)
          cbor_encode_int(
            encoder,
            value
          );
        });
      };
    }

    ///  Encode a text value
    #[macro_export]
    macro_rules! oc_rep_set_text_string {
      ($obj:ident, $key:expr, $value:expr) => {{
        concat!(
          "begin oc_rep_set_text_string ",
          ", c: ",  stringify!($obj),
          ", k: ",  stringify!($key),
          ", v: ",  stringify!($value),
          ", ch: ", stringify!($obj), "_map"  //  object##_map
        );
        //  Convert key and value to char array, which may or may not be null-terminated.
        let key_with_opt_null:   &[u8] = $key.to_bytes_optional_nul();
        let value_with_opt_null: &[u8] = $value.to_bytes_optional_nul();
        mynewt_macros::try_cbor!({
          let encoder = COAP_CONTEXT.encoder(
            stringify!($obj),
            _MAP
          );
          //  Previously: g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key))
          cbor_encode_text_string(
            encoder,
            COAP_CONTEXT.key_to_cstr(key_with_opt_null),
            COAP_CONTEXT.cstr_len(   key_with_opt_null)
          );
          //  Previously: g_err |= cbor_encode_text_string(&object##_map, value, strlen(value))
          cbor_encode_text_string(
            encoder,
            COAP_CONTEXT.value_to_cstr(value_with_opt_null),
            COAP_CONTEXT.cstr_len(     value_with_opt_null)
          );
        });
        d!(end oc_rep_set_text_string);
      }};
    }

    //  TODO
    //  Encode an unsigned int value
    //  void oc_rep_set_uint(void *object, const char *key, uint64_t value);

    //  Encode a float value
    //  void oc_rep_set_float(void *object, const char *key, float value);

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)