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
/// Concatenates constants of primitive types into a `&'static str`.
///
/// Each argument is stringified after evaluating it, so `concatcp!(1u8 + 3) == "4"`
///
/// [For **examples** look here](#examples)
///
/// `concatcp` stands for "concatenate constants (of) primitives"
///
/// # Limitations
///
/// This macro can only take constants of these types as inputs:
///
/// - `&str`
///
/// - `i*`/`u*` (all the primitive integer types).
///
/// - `bool`
///
/// This macro also shares
/// [the limitations described in here](./index.html#macro-limitations)
/// as well.
///
/// # Examples
///
/// ### Literal arguments
///
///
/// ```rust
/// use const_format::concatcp;
///
/// const MSG: &str = concatcp!(2u8, "+", 2u8, "=", 2u8 + 2);
///
/// assert_eq!(MSG, "2+2=4");
///
/// ```
///
/// ### `const` arguments
///
/// ```rust
/// use const_format::concatcp;
///
/// const PASSWORD: &str = "password";
///
/// const fn times() -> u64 { 10 }
///
/// const MSG: &str =
///     concatcp!("The password is \"", PASSWORD, "\", you can only guess ", times(), " times.");
///
/// assert_eq!(MSG, r#"The password is "password", you can only guess 10 times."#);
///
/// ```
///
#[macro_export]
macro_rules! concatcp {
    ()=>{""};
    ($($arg: expr),* $(,)?)=>({
        use $crate::__cf_osRcTFl4A;
        $crate::pmr::__concatcp_impl!{
            $( ( $arg ), )*
        }
    });
}

#[doc(hidden)]
#[macro_export]
macro_rules! __concatcp_inner {
    ($variables:ident) => {{
        const ARR_LEN: usize = $variables.0;

        const CONCAT_ARR: &$crate::pmr::LenAndArray<[u8; ARR_LEN]> = {
            use $crate::{__write_pvariant, pmr::PVariant};

            let mut out = $crate::pmr::LenAndArray {
                len: 0,
                array: [0u8; ARR_LEN],
            };

            let input = $variables.1;

            $crate::__for_range! { outer_i in 0..input.len() =>
                let current = &input[outer_i];

                match current.elem {
                    PVariant::Str(s) => __write_pvariant!(str, current, s => out),
                    PVariant::Int(int) => __write_pvariant!(int, current, int => out),
                }
            }
            &{ out }
        };

        #[allow(clippy::transmute_ptr_to_ptr)]
        const CONCAT_STR: &str = unsafe {
            // This transmute truncates the length of the array to the amound of written bytes.
            let slice =
                $crate::pmr::transmute::<&[u8; ARR_LEN], &[u8; CONCAT_ARR.len]>(&CONCAT_ARR.array);

            $crate::pmr::transmute::<&[u8], &str>(slice)
        };
        CONCAT_STR
    }};
}

////////////////////////////////////////////////////////////////////////////////

/// Formats constants of primitive types into a `&'static str`
///
/// [For **examples** look here](#examples)
///
/// `formatcp` stands for "format constants (of) primitives"
///
/// # Syntax
///
/// This macro uses a limited version of the syntax from the standard library [`format`] macro,
/// it can do these things:
///
/// - Take positional arguments: `formatcp!("{}{0}", "hello" )`
///
/// - Take named arguments: `formatcp!("{a}{a}", a = "hello" )`
///
/// - Use constants from scope as arguments: `formatcp!("{FOO}")`,
/// equivalent to the [`format_args_implicits` RFC]
///
/// - Use Debug-like formatting (eg: `formatcp!("{:?}", "hello" ):
/// Similar to how Debug formatting in the standard library works,
/// except that it does not escape unicode characters.`
///
/// - Use Hexsadecimal formatting (eg: `formatcp!("{:x}", "hello" )`):
/// Formats numbers as capitalized hexadecimal,
/// The alternate version (written as `"{:#x}"`), prefixes the number with `0x`
///
/// - Use Binary formatting (eg: `formatcp!("{:b}", "hello" )`).
/// The alternate version (written as `"{:#b}"`), prefixes the number with `0b`
///
/// - Use Display formatting: `formatcp!("{}", "hello" )`
///
///
/// # Limitations
///
/// This macro can only take constants of these types as inputs:
///
/// - `&str`
///
/// - `i*`/`u*` (all the primitive integer types).
///
/// - `bool`
///
/// This macro also shares
/// [the limitations described in here](./index.html#macro-limitations)
/// as well.
///
/// # Format specifiers
///
/// ### Debug-like
///
/// The `{:?}` formatter formats things similarly to how Debug does it.
///
/// For `&'static str` it does these things:
/// - Prepend and append the double quote character (`"`).
/// - Escape the `'\t'`,`'\n'`,`'\r'`,`'\\'`, `'\''`, and`'\"'` characters.
/// - Escape control characters with `\xYY`,
/// where `YY` is the hexadecimal value of the control character.
///
/// Example:
/// ```
/// use const_format::formatcp;
///
/// assert_eq!(formatcp!("{:?}", r#" \ " ó "#), r#"" \\ \" ó ""#);
/// ```
///
/// ### Display
///
/// The `{}`/`{:}` formatter works the same as in [`format`].
///
///
/// # Examples
///
/// ### Implicit argument
///
/// ```rust
/// use const_format::formatcp;
///
/// const NAME: &str = "John";
///
/// const MSG: &str = formatcp!("Hello {NAME}, your name is {} bytes long", NAME.len());
///
/// assert_eq!(MSG, "Hello John, your name is 4 bytes long");
///
/// ```
///
/// ### Repeating arguments
///
/// ```rust
/// use const_format::formatcp;
///
/// const MSG: &str = formatcp!("{0}{S}{0}{S}{0}", "SPAM", S = "   ");
///
/// assert_eq!(MSG, "SPAM   SPAM   SPAM");
///
/// ```
///
/// ### Debug-like and Display formatting
///
/// ```rust
/// use const_format::formatcp;
///
/// const TEXT: &str = r#"hello " \ world"#;
/// const MSG: &str = formatcp!("{TEXT}____{TEXT:?}");
///
/// assert_eq!(MSG, r#"hello " \ world____"hello \" \\ world""#);
///
/// ```
///
/// [`format`]: https://doc.rust-lang.org/std/macro.format.html
///
/// [`format_args_implicits` RFC]:
/// https://github.com/rust-lang/rfcs/blob/master/text/2795-format-args-implicit-identifiers.md
///
///
#[macro_export]
macro_rules! formatcp {
    ($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
        use $crate::__cf_osRcTFl4A;

        $crate::pmr::__formatcp_impl!(
            ($format_string)
            $(, $(($expr),)+)?
        )
    });
}

////////////////////////////////////////////////////////////////////////////////

/// Concatenates constants of standard library and/or user-defined types into a `&'static str`.
///
/// User defined types must implement the [`FormatMarker`] trait and
/// and have a `const_display_fmt` method (as described in the trait) to be concatenated.
///
/// # Stable equivalent
///
/// For an equivalent macro which can be used in stable Rust (1.46.0 onwards),
/// but can only concatenate primitive types,
/// you can use the [`concatcp`](crate::concatcp) macro.
///
/// # Limitations
///
/// This macro has [the limitations described in here](./index.html#macro-limitations).
///
/// # Examples
///
/// ### With standard library types
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::concatc;
///
/// assert_eq!(concatc!("There is ", 99u8, " monkeys!"), "There is 99 monkeys!");
///
/// ```
///
/// ### With user-defined types
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::{Formatter, Sliced, concatc, impl_fmt};
///
/// const STRING: &str = "foo bar baz";
///
/// assert_eq!(concatc!(Sliced(STRING, 4..8), Foo), "bar table");
///
/// struct Foo;
///
/// impl_fmt!{
///     impl Foo;
///     const fn const_display_fmt(&self, fmt: &mut Formatter<'_>) -> const_format::Result {
///         fmt.write_str("table")
///     }
/// }
/// ```
///
///
/// [`FormatMarker`]: ./marker_traits/trait.FormatMarker.html
///
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "fmt")))]
#[cfg(feature = "fmt")]
#[macro_export]
macro_rules! concatc {
    ()=>{""};
    ($($anything:tt)*)=>({
        use $crate::__cf_osRcTFl4A;

        $crate::__concatc_expr!(($($anything)*) ($($anything)*))
    })
}

#[doc(hidden)]
#[cfg(feature = "fmt")]
#[macro_export]
macro_rules! __concatc_expr {
    (($($arg: expr),* $(,)?) ($($span:tt)*) )=>({
        const fn fmt_NHPMWYD3NJA(
            mut fmt: $crate::fmt::Formatter<'_>,
        ) -> $crate::Result {
            use $crate::coerce_to_fmt as __cf_coerce_to_fmt;
            use $crate::pmr::respan_to as __cf_respan_to;
            use $crate::try_ as __cf_try;

            $({
                let __cf_respan_to!(($arg) fmt) = &mut fmt;
                __cf_respan_to!(($arg)
                    __cf_try!(__cf_coerce_to_fmt!($arg).const_display_fmt(fmt))
                );
            })*

            $crate::pmr::Ok(())
        }

        $crate::__concatc_inner!(fmt_NHPMWYD3NJA, true, $($span)*)
    })
}

#[doc(hidden)]
#[macro_export]
macro_rules! __concatc_inner {
    ($debug_fmt_fn:ident, $cond:expr, $($span:tt)*) => {{
        const fn len_nhpmwyd3nj() -> usize {
            if $cond {
                let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
                let fmt = strlen.make_formatter(__cf_osRcTFl4A::FormattingFlags::NEW);
                match $debug_fmt_fn(fmt) {
                    __cf_osRcTFl4A::pmr::Ok(()) => strlen.len(),
                    __cf_osRcTFl4A::pmr::Err(_) => 0,
                }
            } else {
                0
            }
        }

        const LEN_NHPMWYD3NJA: usize = len_nhpmwyd3nj();

        const fn str_writer_nhpmwyd3nja(
        ) -> __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]> {
            let mut writer = __cf_osRcTFl4A::pmr::StrWriter::new([0; LEN_NHPMWYD3NJA]);
            let error = if $cond {
                $debug_fmt_fn(__cf_osRcTFl4A::pmr::Formatter::from_sw(
                    &mut writer,
                    __cf_osRcTFl4A::FormattingFlags::NEW,
                ))
            } else {
                __cf_osRcTFl4A::pmr::Ok(())
            };

            __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter {
                error: __cf_osRcTFl4A::msg::ErrorTuple::new(error, &writer),
                writer,
            }
        }

        const STR_WRITER_NHPMWYD3NJA: &__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<
            [u8; LEN_NHPMWYD3NJA],
        > = &str_writer_nhpmwyd3nja();

        const _: __cf_osRcTFl4A::msg::Ok = <<__cf_osRcTFl4A::msg::ErrorPicker<
            [(); STR_WRITER_NHPMWYD3NJA.error.error_variant],
            [(); STR_WRITER_NHPMWYD3NJA.error.capacity],
        > as __cf_osRcTFl4A::msg::ErrorAsType>::Type>::NEW;

        const STR_NHPMWYD3NJA: &str =
            __cf_osRcTFl4A::strwriter_as_str!(&STR_WRITER_NHPMWYD3NJA.writer);

        STR_NHPMWYD3NJA
    }};
}

////////////////////////////////////////////////////////////////////////////////

/// Formats constants of standard library and/or user-defined types into a `&'static str`.
///
/// # Stable equivalent
///
/// For an equivalent macro which can be used in stable Rust (1.46.0 onwards),
/// but can only format primitive types,
/// you can use the [`formatcp`](crate::formatcp) macro.
///
/// # Syntax
///
/// This macro uses the syntax described in
/// [the const_format::fmt module](./fmt/index.html#fmtsyntax)
///
/// # Limitations
///
/// This macro has [the limitations described in here](./index.html#macro-limitations).
///
/// # Example
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::for_examples::Point3;
/// use const_format::formatc;
///
/// // Formatting a non-std struct.
/// const POINT: &str = formatc!("{:?}", Point3{x: 8, y: 13, z: 21});
///
/// // Formatting a number as decimal, hexadecimal, and binary
/// const NUMBER: &str = formatc!("{0},{0:x},{0:b}", 10u8);
///
/// // Formatting the numbers in an array as decimal, hexadecimal, and binary.
/// // You can use the name of cnstants from scope, as well as named arguments.
/// const ARR: &[u32] = &[9, 25];
/// const ARRAY: &str = formatc!("{ARR:?},{ARR:x},{ARR:b}");
///
///
/// assert_eq!(POINT, "Point3 { x: 8, y: 13, z: 21 }");
/// assert_eq!(NUMBER, "10,A,1010");
/// assert_eq!(ARRAY, "[9, 25],[9, 19],[1001, 11001]");
///
/// ```
///
/// ### Custom formatting.
///
/// This example demonstrates how you can access the [`Formatter`] in arguments
/// to do custom formatting.
///
/// For more details on this you can look
/// [in the fmt module](./fmt/index.html#custom-formatting-section).
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::for_examples::Point3;
/// use const_format::{formatc, try_};
///
/// const P: Point3 = Point3{x: 5, y: 13, z: 21};
///
/// const STR: &str = formatc!("{0};{0:#x};{0:#b}", |fmt|{
///     try_!(fmt.write_u32_debug(P.x));
///     try_!(fmt.write_str(" "));
///     try_!(fmt.write_u32_debug(P.y));
/// });
///
/// assert_eq!(STR, "5 13;0x5 0xD;0b101 0b1101");
///
/// ```
/// [`Formatter`]: ./fmt/struct.Formatter.html
///
///
#[macro_export]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "fmt")))]
#[cfg(feature = "fmt")]
macro_rules! formatc {
    ($format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
        use $crate::__cf_osRcTFl4A;

        $crate::pmr::__formatc_impl!{
            ($format_string)
            $(, $(($expr),)+)?
        }
    });
}

/// Writes some formatted standard library and/or user-defined types into a buffer.
///
/// This macro evaluates to a `Result<(), const_format::Error>` which must be handled.
///
/// # Syntax
///
/// The syntax is similar to that of other formatting macros in this crate:
///
/// ```ìgnore
/// ẁritec!(
///     writer_expression,
///     "formatting literal",
///     positional_arg_0_expression,
///     positional_arg_1_expression,
///     named_arg_foo = expression,
///     named_arg_bar = expression,
/// )
/// ```
///
/// The syntax is otherwise the same as described in
/// [the `const_format::fmt` module](./fmt/index.html#fmtsyntax).
///
/// # Writers
///
/// The first argument must be a type that implements the [`WriteMarker`] trait,
/// and has these inherent methods:
/// ```ignore
/// const fn borrow_mutably(&mut self) -> &mut Self
/// const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_>
/// ```
///
/// [This example](#custom-writable-example) below shows how to use this macro
/// with a custom type.
///
/// # Limitations
///
/// Integer arguments must have a type inferrable from context,
/// [more details in the Integer arguments section](./index.html#integer-args).
///
/// # Examples
///
/// ### Ẁriting a Display impl.
///
/// ```
/// #![feature(const_mut_refs)]
///
/// use const_format::{Error, Formatter, StrWriter};
/// use const_format::{impl_fmt, try_, writec};
///
/// pub struct Foo(u32, &'static str);
///
/// impl_fmt!{
///     impl Foo;
///     pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
///         try_!(writec!(f, "{},", self.0));
///         try_!(writec!(f, "{:?};", self.1));
///         Ok(())
///     }
/// }
///
/// // Coerces the `&mut StrWriter<[u8; 128]>` to `&mut StrWriter<[u8]>`.
/// // This is necessary because the `as_str` method is defined for `StrWriter<[u8]>`.
/// let writer: &mut StrWriter = &mut StrWriter::new([0; 128]);
/// writec!(writer, "{}", Foo(100, "bar"))?;
///
/// assert_eq!(writer.as_str(), r#"100,"bar";"#);
///
/// # Ok::<(), const_format::Error>(())
/// ```
///
/// <span id="custom-writable-example"></span>
/// ### Writing to a custom type
///
/// This example demonstrates how you can use the `ẁritec` macro with a custom type,
/// in this case it's a buffer that is cleared every time it's written.
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::marker_traits::{IsNotAStrWriter, WriteMarker};
/// use const_format::{Formatter, FormattingFlags};
/// use const_format::writec;
///
/// const ARRAY_CAP: usize = 20;
/// struct Array {
///     len: usize,
///     arr: [u8; ARRAY_CAP],
/// }
///
/// impl WriteMarker for Array{
///     type Kind = IsNotAStrWriter;
///     type This = Self;
/// }
///
/// impl Array {
///     // Gets the part of the array that has been written to.
///     pub const fn as_bytes(&self) -> &[u8] {
///         const_format::utils::slice_up_to_len_alt(&self.arr, self.len)
///     }
///
///     pub const fn borrow_mutably(&mut self) -> &mut Self {
///         self
///     }
///
///     pub const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_> {
///         Formatter::from_custom_cleared(&mut self.arr, &mut self.len, flags)
///     }
/// }
///
///
/// let mut buffer = Array{ arr: [0; ARRAY_CAP], len: 0 };
///
/// writec!(buffer, "{:?}", [3u8, 5, 8, 13, 21])?;
/// assert_eq!(buffer.as_bytes(), b"[3, 5, 8, 13, 21]");
///
/// writec!(buffer, "{}{}", "Hello, world!", 100u16)?;
/// assert_eq!(buffer.as_bytes(), b"Hello, world!100");
///
/// # Ok::<(), const_format::Error>(())
/// ```
///
/// ### Custom formatting.
///
/// This example demonstrates how you can access the [`Formatter`] in arguments
/// to do custom formatting.
///
/// Note that `return` inside arguments returns from the function around the `writec`.
///
/// For more details on this you can look
/// [in the fmt module](./fmt/index.html#custom-formatting-section).
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::for_examples::Point3;
/// use const_format::{StrWriter, call_debug_fmt, try_, writec};
///
/// const P: Point3 = Point3{x: 5, y: 13, z: 21};
///
/// let writer: &mut StrWriter = &mut StrWriter::new([0; 128]);
///
/// writec!(
///     writer,
///     "The options are: {}, and {}",
///     |fmt| call_debug_fmt!(Option, Some(P), fmt),
///     |fmt| call_debug_fmt!(Option, None::<Point3>, fmt),
/// )?;
///
/// assert_eq!(writer.as_str(), "The options are: Some(Point3 { x: 5, y: 13, z: 21 }), and None");
///
/// # Ok::<(), const_format::Error>(())
/// ```
///
/// ### Locals in the format string
///
/// This example demonstrates how you can format local variables,
/// by using their identifiers in the format string.
///
/// ```rust
/// #![feature(const_mut_refs)]
///
/// use const_format::{Formatter, FormattingFlags, StrWriter, try_, writec};
///
/// const fn writeit(mut fmt: Formatter<'_>, foo: u32, bar: &str) -> const_format::Result {
///     try_!(writec!(fmt, "{foo},{foo:?},{foo:#x},{foo:#b};"));
///     try_!(writec!(fmt, "{bar},{bar:?}"));
///     Ok(())
/// }
///
/// let writer: &mut StrWriter = &mut StrWriter::new([0; 128]);
///
/// writeit(writer.make_formatter(FormattingFlags::NEW), 100, "hello")?;
///
/// assert_eq!(writer.as_str(), r#"100,100,0x64,0b1100100;hello,"hello""#);
///
/// # Ok::<(), const_format::Error>(())
/// ```
///
/// [`Formatter`]: ./fmt/struct.Formatter.html
/// [`WriteMarker`]: ./marker_traits/trait.WriteMarker.html
///
///
///
///
#[macro_export]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "fmt")))]
#[cfg(feature = "fmt")]
macro_rules! writec {
    ( $writer:expr, $format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => ({
        use $crate::__cf_osRcTFl4A;

        $crate::pmr::__writec_impl!{
            ($writer)
            ($format_string)
            $(, $(($expr),)+)?
        }
    });
}