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
//! This crate provides alternative syntax for //! `write!`, `writeln!`, `print!`, `println!` and `format!` macros. //! It also introduces macros to print on `stderr`. //! //! The names of macros in this crate are formed by //! removing the letter `r` from their `std` counterparts. //! //! Index: [**examples**](#examples) • //! [**syntax**](#syntax-overview): //! [`"string"`](#string-literals), //! [`()`, `[]`](#expressions-in--and--brackets), //! [`{}`](#curly-braces), //! [`for`](#for-loops), //! [`if`](#if-and-if-let), //! [`match`](#match), //! [`=`](#debugging-shorthand) • //! [**troubleshooting**](#troubleshooting) • //! [**macros**](#macros) //! //! # Examples //! //! ``` //! use fomat_macros::pintln; //! //! fn main() { //! pintln!("Hello, World!"); //! pintln!("Display trait: "(2+2)); //! pintln!("Debug trait: "[vec![1, 2, 3]]); //! pintln!("Multiple " "parameters" (1) " " [2]); //! //! pintln!("Formatting parameters: " {(1./3.):5.2}); // 0.333 //! pintln!("Debug: "[= 2 + 2]); // Debug: 2 + 2 = 4 //! } //! ``` //! //! This crate also contains a small templating language, //! allowing you to mix constructs like `for` with //! the printing syntax. The following should print `1 :: 2 :: 3 :: nil`. //! //! ``` //! # use fomat_macros::pintln; //! # fn main() { //! let list = [1, 2, 3]; //! pintln!( for x in &list { (x) " :: " } "nil" ); //! # } //! ``` //! //! You can also use the macros without importing them //! ``` //! fomat_macros::pintln!("2 + 2 = "(2 + 2)); //! ``` //! //! # Syntax overview //! //! All the macros share the same syntax, so //! it will be described in this section. //! //! The macros take list of *things* to print as an argument. //! Each *thing* could be either a string literal, something //! inside brackets (`()`, `[]` or `{}`) or a Rust construct //! (`for`, `if let`, `if` or `match`). There has to be //! no separator (like a comma) between those *things*. //! //! Whitespace is ignored outside the string literals. //! //! ## String literals //! //! String literals will be formatted directly as they are. //! Note that this also applies to `{` and `}` characters. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let s = fomat!("Hi." "{}"); //! assert_eq!(s, "Hi.{}"); //! # } //! ``` //! //! ## Expressions in `()` and `[]` brackets. //! //! Expressions in these brackets will be evaluated and //! printed using: //! //! * `Display` trait for `(expr)` (equivalent to `{}` format). //! * `Debug` trait for `[expr]` (equivalent to `{:?}` format). //! //! Like in `std`, they are implicitly borrowed. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let s = fomat!( ("string") (2 + 2) ", " [vec![1]] ); //! assert_eq!(s, "string4, [1]") //! # } //! ``` //! //! ## Curly braces //! //! ### `write!` passthrough //! //! If you want to use regular `format!` syntax for some //! part of your string, place `format!` arguments //! inside the curly braces: //! //! ``` //! # use fomat_macros::wite; //! # fn main() { //! use std::io::Write; //! //! let mut v = vec![]; //! wite!(v, "foo " {"{} baz {}", "bar", "quux"}); //! assert_eq!(v, "foo bar baz quux".as_bytes()); //! # } //! ``` //! //! ### Single argument //! //! If you only want to print a single argument //! with a custom format parameters, //! you can use the `{token_tree:format_parameters}` //! syntax. //! //! The following will use binary format, //! zero-aligned to 8 places. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let s = fomat!({13:08b}); //! assert_eq!(s, "00001101"); //! # } //! ``` //! //! Please note that there can be only a single //! token tree before the colon – usually //! a literal or an identifier. Anything //! longer has to be wrapped in parentheses //! (like that `{(10+3):08b}`). //! //! ## For loops //! //! For loops use the regular Rust syntax, //! except the body //! will use this printing syntax again. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let list = [1, 2, 3]; //! let s = fomat!( for x in &list { (x) " :: " } "nil" ); //! assert_eq!(s, "1 :: 2 :: 3 :: nil"); //! # } //! ``` //! //! For loops can also use an optional separator, //! denoted by `sep` or `separated` keyword. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! # let list = ["a", "b"]; //! let s = fomat!( //! for (i, x) in list.iter().enumerate() { (i) " → " (x) } //! separated { ", " } //! ); //! assert_eq!(s, "0 → a, 1 → b"); //! # } //! ``` //! //! For loops (and other syntax elements) can also be nested: //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let matrix = [[0, 1], [2, 3]]; //! assert_eq!( //! fomat!( for row in &matrix { for x in row { {x:3} } "\n" } ), //! " 0 1\n 2 3\n" //! ); //! # } //! ``` //! //! ## If and if let //! //! They use the regular Rust syntax, //! except of the body (inside `{}`), //! which uses the printing syntax. //! //! The benefits of using this syntax instead //! of getting `if` "outside" of the printing //! macros is apparent when the conditional is //! a part of a longer string (you don't //! have to split this into three separate `write!`s): //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let opt = Some(5); //! let s = fomat!( //! "a\n" //! if let Some(x) = opt { (x) "\n" } else { "nothing\n" } //! "b\n" //! ); //! assert_eq!(s, "a\n5\nb\n"); //! # } //! ``` //! //! The `else` clause is optional. //! //! `else if`-chaining is not supported. As a workaround, //! use `else { if ... }` or `match`. //! //! ## Match //! //! Match uses the regular Rust syntax, //! except arms has to use `{}` blocks, //! which will be interpreted using printing syntax. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let v = [Some(1), None, Some(2)]; //! let s = fomat!( //! for x in &v { //! match *x { //! Some(x) => { (x) } //! None => { "_" } //! } //! } //! ); //! assert_eq!(s, "1_2"); //! # } //! ``` //! //! Match arms should not be separated by commas. //! //! ## Debugging shorthand //! //! If you want to print both the expression and the value, //! place equal sign as a first character in brackets. //! The trait used to print the value will depend on //! the kind of brackets used. //! //! ``` //! # use fomat_macros::fomat; //! # fn main() { //! let word = "foo"; //! let arr = [10]; //! let s = fomat!( (=word) ", " [=&arr] ", " {=5:#b} ); //! assert_eq!(s, "word = foo, &arr = [10], 5 = 0b101"); //! # } //! ``` //! //! # Troubleshooting //! //! ## Recursion depth //! //! If you hit the error about recursion depth, //! which occurs when you try to print more than //! about 50 elements, you can use this workaround //! instead of increasing the limit: split everything //! into two (or more) dummy `if true` blocks. //! //! ## Errors in macro parsing //! //! If you hit `expected a literal`, that either means //! either you've made a syntactic mistake //! or really a string literal is expected here. //! Remember, naked identifiers won't be printed //! unless you put them in parentheses. use std::fmt; #[doc(hidden)] pub struct DisplayOnce<F> { closure: std::cell::Cell<Option<F>> } impl<F> DisplayOnce<F> where F: FnOnce(&mut fmt::Formatter) -> fmt::Result { pub fn new(f: F) -> Self { Self { closure: std::cell::Cell::new(Some(f)) } } } impl<F> fmt::Display for DisplayOnce<F> where F: FnOnce(&mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.closure.replace(None).take() { Some(closure) => closure(f), None => Ok(()) } } } /// Wrapper implementing Display for every closure with matching signature /// /// This wrapper implements Display for every closure implementing /// `Fn(&mut fmt::Formatter) -> fmt::Result`. /// /// Can be create using [`lazy_fomat`][lazy_fomat] pub struct DisplayFn<F>(F); impl<F> DisplayFn<F> where F: Fn(&mut fmt::Formatter) -> fmt::Result { /// Creates an object which `Display::fmt` impl will call this closure. pub fn new(f: F) -> Self { Self(f) } } impl<F> fmt::Display for DisplayFn<F> where F: Fn(&mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (self.0)(f) } } /// Writes to a specified writer. Analogous to `write!`. /// /// See the crate root for general help on the syntax. /// /// The first argument should be something that implements either `io::Write` /// or `fmt::Write`. This expression will be evaluated once. /// /// The list of things to write should be written after /// the first comma, without any further delimiters. /// /// # Return value /// /// This macro returns `io::Result<()>` or `fmt::Result`, /// just as `write!` from `std`. /// /// # Examples /// /// ``` /// # use fomat_macros::wite; /// # fn main() { /// use ::std::io::Write; /// use ::std::io::BufWriter; /// let mut v = vec![]; /// let world = "World"; /// wite!(v, "Hello, "(world)"!").unwrap(); /// wite!(BufWriter::new(&mut v), " "(2+2)).unwrap(); /// assert_eq!(v, "Hello, World! 4".as_bytes()); /// # } /// ``` #[macro_export] macro_rules! wite { // single tt rules --------------------------------------------------------- (@one $w:ident, ($e:expr)) => { ::std::fmt::Display::fmt(&$e, $w) }; (@one $w:ident, [$e:expr]) => { ::std::fmt::Debug::fmt(&$e, $w) }; (@one $w:ident, {$e:tt : $($fmt:tt)*}) => { write!($w, concat!("{:", $crate::wite!(@stringify-dense $($fmt)*), "}"), $e) }; (@one $w:ident, {$($arg:tt)*}) => { write!($w, $($arg)*) }; (@one $w:ident, $string:tt) => { $w.write_str(concat!($string)) }; (@stringify-dense) => { "" }; (@stringify-dense $($tt:tt)+) => { concat!( $(stringify!($tt)),+ ) }; // expression parsing (manually, because we can't use :expr before `{`) (@expr.. $w:ident {$($before:tt)*} ($($e:tt)*) {$($block:tt)*} $($rest:tt)* ) => { $crate::wite!(@rec $w, $($before)* ($($e)*) {$($block)*} $($rest)*) }; (@expr.. $w:ident {$($before:tt)*} ($($expr:tt)*) $tt:tt $($rest:tt)* ) => { $crate::wite!(@expr.. $w {$($before)*} ($($expr)* $tt) $($rest)*) }; (@expr $w:ident {$($before:tt)*} ($($expr:tt)*) $tt:tt $($rest:tt)* ) => { $crate::wite!(@expr.. $w {$($before)*} ($($expr)* $tt) $($rest)*) }; // recursive parsing ------------------------------------------------------- // for (@rec $w:ident, for $p:pat in ($e:expr) { $($body:tt)* } sep { $($sep:tt)* } $($rest:tt)* ) => { { let mut first_iteration = true; for $p in $e { if first_iteration { first_iteration = false; } else { $crate::wite!(@rec $w, $($sep)*); } $crate::wite!(@rec $w, $($body)*); } $crate::wite!(@rec $w, $($rest)*); } }; (@rec $w:ident, for $p:pat in ($e:expr) { $($body:tt)* } separated { $($sep:tt)* } $($rest:tt)* ) => { $crate::wite!(@rec $w, for $p in ($e) { $($body)* } sep { $($sep)* }$($rest)*) }; (@rec $w:ident, for $p:pat in ($e:expr) { $($body:tt)* } $($rest:tt)*) => { $crate::wite!(@rec $w, for $p in ($e) { $($body)* } sep {} $($rest)*) }; (@rec $w:ident, for $p:pat in $($tt:tt)* ) => { $crate::wite!(@expr $w { for $p in } () $($tt)*) }; // match (@rec $w:ident, match ($e:expr) { $( $($p:pat)|+ $(if $g:expr)* => { $($body:tt)* } )* } $($rest:tt)* ) => { { match $e { $( $($p)|+ $(if $g)* => { $crate::wite!(@rec $w, $($body)*) } )* } $crate::wite!(@rec $w, $($rest)*); } }; (@rec $w:ident, match $($tt:tt)* ) => { $crate::wite!(@expr $w { match } () $($tt)*) }; // if let (@rec $w:ident, if let $p:pat = ($e:expr) { $($then:tt)* } else { $($els:tt)* } $($rest:tt)* ) => { { if let $p = $e { $crate::wite!(@rec $w, $($then)*); } else { $crate::wite!(@rec $w, $($els)*); } $crate::wite!(@rec $w, $($rest)*); } }; (@rec $w:ident, if let $p:pat = ($e:expr) { $($then:tt)* } else if $($rest:tt)* ) => { $crate::wite!(@ifelseerror) }; (@rec $w:ident, if let $p:pat = ($e:expr) { $($then:tt)* } $($rest:tt)* ) => { $crate::wite!(@rec $w, if let $p = ($e) { $($then)* } else {} $($rest)*); }; (@rec $w:ident, if let $p:pat = $($tt:tt)* ) => { $crate::wite!(@expr $w { if let $p = } () $($tt)*) }; // if (@rec $w:ident, if ($cond:expr) { $($then:tt)* } else { $($els:tt)* } $($rest:tt)* ) => { { if $cond { $crate::wite!(@rec $w, $($then)*); } else { $crate::wite!(@rec $w, $($els)*); } $crate::wite!(@rec $w, $($rest)*); } }; (@rec $w:ident, if ($cont:expr) { $($then:tt)* } else if $($rest:tt)* ) => { $crate::wite!(@ifelseerror) }; (@rec $w:ident, if ($cond:expr) { $($then:tt)* } $($rest:tt)* ) => { $crate::wite!(@rec $w, if ($cond) { $($then)* } else {} $($rest)*); }; (@rec $w:ident, if $($tt:tt)* ) => { $crate::wite!(@expr $w { if } () $($tt)*) }; // equal-sign debugging (@rec $w:ident, (= $e:expr) $($rest:tt)*) => { $crate::wite!(@rec $w, (concat!(stringify!($e), " = ")) ($e) $($rest)*) }; (@rec $w:ident, [= $e:expr] $($rest:tt)*) => { $crate::wite!(@rec $w, (concat!(stringify!($e), " = ")) [$e] $($rest)*) }; (@rec $w:ident, {= $e:tt : $($fmt:tt)*} $($rest:tt)*) => { $crate::wite!(@rec $w, (concat!(stringify!($e), " = ")) {$e : $($fmt)*} $($rest)*) }; // single tt (@rec $w:ident, $part:tt $($rest:tt)*) => { { match $crate::wite!(@one $w, $part) { Ok(_) => (), error => return error, } $crate::wite!(@rec $w, $($rest)*); } }; // terminator (@rec $w:ident, ) => { () }; (@ifelseerror) => { { let ERROR: () = "`else if` is not supported"; let NOTE: () = "use `match` or `else { if ... }` instead"; } }; // entry point ------------------------------------------------------------- ($writer:expr, $($part:tt)*) => { write!( $writer, "{}", $crate::DisplayOnce::new(|f| { $crate::wite!(@rec f, $($part)*); Ok(()) }) ) }; } /// Writes to a specified writer, with an appended newline. Analogous to `writeln!`. /// /// See the documentation for [`wite!`](macro.wite.html). /// /// When there are no arguments, the comma may be omitted. /// /// # Examples /// /// ```no_run /// # use fomat_macros::witeln; /// # fn main() { /// # use ::std::io::Write; /// # let mut file = vec![]; /// witeln!(file).unwrap(); /// witeln!(file, "Hi").unwrap(); /// # } /// ``` #[macro_export] macro_rules! witeln { ($writer:expr, $($arg:tt)*) => { $crate::wite!($writer, $($arg)* "\n") }; ($writer:expr) => { $crate::wite!($writer, "\n") }; } /// Prints to stdout. Analogous to `print!`. /// /// See the crate root for general help on the syntax. /// /// # Return value /// /// The macro returns `()`. /// /// # Panics /// /// The macro panics when printing was not successful. /// /// # Behaviour in `#[test]` /// /// The behaviour when testing is similar to `print!`: /// the output of this macro will be captured by the /// testing framework (meaning that by default `cargo test` /// won't show the output). /// /// The only limitation and difference from `print!` is /// that when `pint!` is called by a different crate /// than the one being tested, the output won't be captured /// and will allways be printed to stdout. /// /// # Examples /// /// ```no_run /// # use fomat_macros::pint; /// # fn main() { /// pint!("four = "(2+2)); /// # } /// ``` #[macro_export] macro_rules! pint { ($($arg:tt)*) => { { { #[cfg(not(test))] { use ::std::io::Write; let o = ::std::io::stdout(); $crate::wite!(o.lock(), $($arg)*).unwrap(); } #[cfg(test)] { print!("{}", $crate::fomat!($($arg)*)) } } } } } /// Prints to stdout, with an appended newline. Analoguous to `println!`. /// /// See the docs for [`print!`](macro.pint.html) for more details. /// /// # Examples /// /// ```no_run /// # use fomat_macros::pintln; /// # fn main() { /// pintln!(); /// pintln!((2 * 2)); /// # } /// ``` #[macro_export] macro_rules! pintln { ($($arg:tt)*) => { { #[cfg(not(test))] { $crate::pint!($($arg)* "\n") } #[cfg(test)] { print!("{}", fomat!($($arg)* "\n")) } } } } /// Prints to stderr. Analogous to `eprint!`. /// /// See the crate root for general help on the syntax. /// /// # Return value /// /// None /// /// # Panics /// /// This macro, in contrary to `pint!`, silently ignores /// all errors. /// /// # Examples /// /// ```no_run /// # use fomat_macros::epint; /// # fn main() { /// epint!("foo") /// # } /// ``` #[macro_export] macro_rules! epint { ($($arg:tt)*) => { { use ::std::io::Write; let o = ::std::io::stderr(); $crate::wite!(o.lock(), $($arg)*).unwrap(); } } } /// Same as `epint` #[macro_export] #[deprecated(since="0.2.1", note="use `epint` instead")] macro_rules! perr { ($($arg:tt)*) => { $crate::epint!($($arg)*) } } /// Prints to stderr, with an appended newline. Analogous to `eprintln!`. /// /// See the docs for [`epint!`](macro.epint.html) for more info. /// /// # Examples /// /// ```no_run /// # use fomat_macros::epintln; /// # fn main() { /// let x = 3; /// epintln!((=x)); /// # } /// ``` #[macro_export] macro_rules! epintln { ($($arg:tt)*) => { $crate::epint!($($arg)* "\n") } } /// Same as `epintln` #[macro_export] #[deprecated(since="0.2.1", note="use `epint` instead")] macro_rules! perrln { ($($arg:tt)*) => { $crate::epintln!($($arg)*) } } /// Creates a formatted string. Analogous to `format!`. /// /// See the crate root for general help on the syntax. /// /// This macro returns `String` containing the formatted text. /// /// # Panics /// /// The macro will panic if formatting fails (which shoudn't happen for any /// of `std` types). /// /// # Examples /// /// ``` /// # use fomat_macros::fomat; /// # fn main() { /// let v = [1, 2]; /// /// let s = fomat!("Hello, "[v]); /// assert_eq!(s, "Hello, [1, 2]"); /// /// let s = fomat!(for x in &v { (x*x) ";" }); /// assert_eq!(s, "1;4;"); /// # } /// ``` #[macro_export] macro_rules! fomat { // capacity estimation ----------------------------------------------------- (@cap ($len:expr, $multiplier:expr)) => { ($len, $multiplier) }; // skip all irrelevant tts and conditional bodies (@cap ($($lm:tt)*) for $p:pat in $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) sep $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) separated $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) if let $p:pat = $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) if $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) else $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; (@cap ($($lm:tt)*) match $($tt:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($tt)*) }; // When there's any unconditional string interpolation, // we multiply the initial capacity by 2 // (which would probably happen anyway). (@cap ($len:expr, $mul:expr) ($($x:tt)*) $($rest:tt)*) => { $crate::fomat!(@cap ($len, 2) $($rest)*) }; (@cap ($len:expr, $mul:expr) [$($x:tt)*] $($rest:tt)*) => { $crate::fomat!(@cap ($len, 2) $($rest)*) }; (@cap ($len:expr, $mul:expr) {$($x:tt)*} $($rest:tt)*) => { $crate::fomat!(@cap ($len, 2) $($rest)*) }; // Now the only legal tt is a string literal (@cap ($len:expr, $mul:expr) $string:tt $($rest:tt)*) => { // Concat forces the token to be a string literal. $crate::fomat!(@cap ($len + concat!($string).len(), $mul) $($rest)*) }; // Ignores everything till after next block (@cap-ignore ($($lm:tt)*) { $($block:tt)* } $($rest:tt)*) => { $crate::fomat!(@cap ($($lm)*) $($rest)*) }; (@cap-ignore ($($lm:tt)*) $tt:tt $($rest:tt)*) => { $crate::fomat!(@cap-ignore ($($lm)*) $($rest)*) }; // entry points ------------------------------------------------------------ () => { String::new() }; ($($arg:tt)*) => { { use ::std::fmt::Write; let (len, mul) = $crate::fomat!(@cap (0, 1) $($arg)*); let mut _s = String::with_capacity(len * mul); $crate::wite!(_s, $($arg)*).ok(); _s } } } /// Creates a displayable object based on its arguments. /// /// This macro works in a similar way to [`fomat`](fomat), /// but instead of `String` it returns an object ([`DisplayFn`](DisplayFn)) /// that implements [`Display`][std::fmt::Display] and can be printed later /// (using `format`, `fomat` or calling `Display::fmt` directly). /// /// See the [crate root](crate) for general help on the syntax. /// /// Prefix the arguments with `move` to force moving all variables /// (can help when `'static` bound is required). /// /// # Examples /// /// Direct usage /// /// ``` /// # use fomat_macros::{fomat, lazy_fomat}; /// let fence = lazy_fomat!(for _ in 0..5 { "-" }); /// let s = fomat!((fence)" hello "(fence)); /// /// assert_eq!(s, "----- hello -----"); /// ``` /// /// Returning `impl Display` /// /// ``` /// # use fomat_macros::lazy_fomat; /// fn greet(name: String) -> impl ::std::fmt::Display { /// lazy_fomat!(move "Hello, "(name)"!") /// } /// /// assert_eq!(greet("World".into()).to_string(), "Hello, World!"); /// ``` #[macro_export] macro_rules! lazy_fomat { (move $($arg:tt)*) => { $crate::DisplayFn::new(move |f| { $crate::wite!(@rec f, $($arg)*); Ok(()) }) }; ($($arg:tt)*) => { $crate::DisplayFn::new(|f| { $crate::wite!(@rec f, $($arg)*); Ok(()) }) }; } #[test] fn basics() { let world = "World"; assert_eq!(fomat!("Hello, "(world)"!"), "Hello, World!"); let x = 3; assert_eq!(fomat!((x)" * 2 = "(x * 2)), "3 * 2 = 6"); } #[test] fn empty() { assert_eq!(fomat!(), ""); } #[test] fn debug() { let v = [1,2,3]; assert_eq!(fomat!([v] "."), "[1, 2, 3]."); } #[test] fn test_if() { let s = fomat!( if true { "A" "A" } else { "X" } if false { "X" } else { "D" "D" } if true { "T" "T" } if false { "X" } if let Some(x) = Some(5) { (x) (x) } else { "E" "E" } if let None = Some(5) { "X" } else { "F" "F" } if let Some(x) = Some(5) { (x) } if let None = Some(5) { "X" } if {let t = true; t} { "K" } "." ); assert_eq!(s, "AADDTT55FF5K."); } #[test] fn format() { assert_eq!( fomat!({5:02}), "05" ); assert_eq!( fomat!({"{}-{}", 4, 2}), "4-2" ); } #[test] fn separator() { let v = [1, 2, 3]; let s1 = fomat!( for x in &v { (x) } separated { "-" "-" } "." ); let s2 = fomat!( for x in &v { (x) } sep { "--" } "." ); assert_eq!(s1, "1--2--3."); assert_eq!(s2, "1--2--3."); } #[test] fn test_match() { let s = fomat!( match Some(5) { Some(x) if x > 3 => { (x) "!" } Some(2) | None => {} _ => {} } "." ); assert_eq!(s, "5!."); } #[test] fn capacity() { assert_eq!(fomat!("Hello, " "world!").capacity(), 13); assert_eq!(fomat!("Hello, "[40+2]).capacity(), 14); let s = fomat!( "Hello" for x in [1][1..].iter() { (x) "a" } if let Some(()) = None { "b" } if false { "c" } else {} match 1 { 2 => { "e" } _ => {} } "!" ); assert_eq!(s.capacity(), 6); } #[test] fn fmt_write() { use std::fmt; struct Foo; impl fmt::Display for Foo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { wite!(f, "foo"(42)) } } assert_eq!(format!("{}", Foo), "foo42"); } #[test] fn equal_sign() { let x = 5; let v = [10]; assert_eq!(fomat!((=x) "."), "x = 5."); assert_eq!(fomat!([=&v] "."), "&v = [10]."); assert_eq!(fomat!({=13:05b} "."), "13 = 01101."); } #[test] fn depth() { let _ = fomat!( "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" ); } #[test] fn non_static_writer() { use std::io::Write; use std::io::Result; use std::fmt::Arguments; struct Prepender<'a, T: Write> { prefix: &'a str, writer: T, } impl<'a, T: Write> Write for Prepender<'a, T> { fn write(&mut self, buf: &[u8]) -> Result<usize> { self.writer.write(buf) } fn flush(&mut self) -> Result<()> { self.writer.flush() } fn write_fmt(&mut self, fmt: Arguments) -> Result<()> { self.writer.write_all(self.prefix.as_bytes())?; self.writer.write_fmt(fmt) } } let mut buf = vec![]; witeln!( Prepender { prefix: &"foo ".to_owned(), writer: &mut buf }, (2+2) ).unwrap(); assert_eq!(buf, "foo 4\n".as_bytes()); } #[test] fn no_semicolon() { if true { pint!("foo") } else { epint!("bar") } pintln!("foo" "bar") } #[test] fn move_and_borrow() { // Test if fomat! arguments can move some and borrow other variables. let iter = vec![1, 2, 3].into_iter(); let borrow_me = vec![1, 2, 3]; let s = fomat!(for x in iter { (x) } (borrow_me.len())); assert_eq!(s, "1233"); assert_eq!(borrow_me, [1, 2, 3]); }