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
//! `rt_*` macros
//!
//! The central macro is `rt_format_args!`, analogous to `format_args!`.
//! The rest of the macros correspond to the other `std` formatting macros.

/// The core macro for runtime formatting.
///
/// This macro produces a value of type `Result<FormatBuf, Error>`. Invalid
/// format strings are indicated by an error result. The resulting value can
/// be converted to a `std::fmt::Arguments` via the `with()` method.
///
/// The syntax accepted is the same as `format_args!`. See the module-level
/// docs for more detail.
#[macro_export]
macro_rules! rt_format_args {
    (@[$spec:expr] [$($args:tt)*] $name:tt = $e:expr, $($rest:tt)*) => {
        rt_format_args!(@[$spec] [$($args)* $crate::Param::named(stringify!($name), &$e),] $($rest)*)
    };
    (@[$spec:expr] [$($args:tt)*] $name:tt = $e:expr) => {
        rt_format_args!(@[$spec] [$($args)* $crate::Param::named(stringify!($name), &$e),])
    };
    (@[$spec:expr] [$($args:tt)*] $e:expr, $($rest:tt)*) => {
        rt_format_args!(@[$spec] [$($args)* $crate::Param::normal(&$e),] $($rest)*)
    };
    (@[$spec:expr] [$($args:tt)*] $e:expr) => {
        rt_format_args!(@[$spec] [$($args)* $crate::Param::normal(&$e),])
    };
    (@[$spec:expr] [$($args:tt)*]) => {
        $crate::FormatBuf::new(&$spec, &[$($args)*])
    };
    ($spec:expr, $($rest:tt)*) => {
        rt_format_args!(@[$spec] [] $($rest)*)
    };
    ($spec:expr) => {
        $crate::FormatBuf::new(&$spec, &[])
    };
}

/// Format a value of type `String` with a runtime format string.
///
/// The format string should be any type coercible to an `&str`, and will not
/// be consumed.
///
/// Returns a `Result<String, Error>`. See the module-level docs for more
/// information.
#[macro_export]
macro_rules! rt_format {
    ($($rest:tt)*) => {
        rt_format_args!($($rest)*).map(|x| x.with(::std::fmt::format))
    }
}

/// Print to standard output with a runtime format string.
///
/// Returns a `Result<(), Error>`. Panics if writing to stdout fails. See the
/// module-level docs for more information.
#[macro_export]
macro_rules! rt_print {
    ($($rest:tt)*) => {
        rt_format_args!($($rest)*).map(|x| x.with($crate::_print))
    }
}

/// Print to standard output with a runtime format string and trailing newline.
///
/// Returns a `Result<(), Error>`. Panics if writing to stdout fails. See the
/// module-level docs for more information.
#[macro_export]
macro_rules! rt_println {
    ($($rest:tt)*) => {
        rt_format_args!($($rest)*).map(|mut x| x.newln().with($crate::_print))
    }
}

/// Write runtime-formatted data into a buffer.
///
/// Like `write!`, implementations of either `std::fmt::Write` or
/// `std::io::Write` are accepted. `Error` variants of the appropriate type may
/// be returned.
///
/// Returns a `Result<(), Error>`. See the module-level docs for more
/// information.
#[macro_export]
macro_rules! rt_write {
    ($dest:expr, $($rest:tt)*) => {
        rt_format_args!($($rest)*).and_then(|x|
            x.with(|args| $dest.write_fmt(args)).map_err(::std::convert::From::from)
        )
    }
}

/// Write runtime-formatted data into a buffer with a trailing newline.
///
/// Like `writeln!`, implementations of either `std::fmt::Write` or
/// `std::io::Write` are accepted. `Error` variants of the appropriate type may
/// be returned.
///
/// Returns a `Result<(), Error>`. See the module-level docs for more
/// information.
#[macro_export]
macro_rules! rt_writeln {
    ($dest:expr, $($rest:tt)*) => {
        rt_format_args!($($rest)*).and_then(|mut x|
            x.newln().with(|args| $dest.write_fmt(args)).map_err(::std::convert::From::from)
        )
    }
}