lazy_motd/
lib.rs

1/* src/lib.rs */
2
3pub mod env;
4pub mod pkg;
5
6#[macro_export]
7macro_rules! lazy_motd {
8    (copyright = [$($copyright_line:expr),* $(,)?] $(, $key:ident = $val:expr)* $(,)?) => {{
9        let copyright_lines: &[&str] = &[$($copyright_line),*];
10        $crate::lazy_motd!(copyright = copyright_lines $(, $key = $val)*);
11    }};
12
13    ($( $key:ident = $val:expr ),* $(,)?) => {{
14        let mut bin: Option<&str> = None;
15        let mut build: Option<&str> = None;
16        let mut mode: Option<&str> = None;
17        let mut timestamp: Option<&str> = None;
18        let mut environment: Option<&str> = None;
19        let mut copyright: Option<&[&str]> = None;
20
21        $(
22            lazy_motd!(@assign $key = $val, bin, build, mode, timestamp, environment, copyright);
23        )*
24        let final_pkg_name = if let Some(b) = bin {
25            if b == "None" {
26                // If bin is "None", use the raw binary name without formatting.
27                env!("CARGO_BIN_NAME").to_string()
28            } else if !b.is_empty() {
29                // If a custom bin name is provided, format it.
30                $crate::pkg::capitalize_first(b)
31            } else {
32                // If bin is empty, use the default package name and format it.
33                $crate::pkg::capitalize_first(env!("CARGO_PKG_NAME"))
34            }
35        } else {
36            // If bin is not specified, use the default package name and format it.
37            $crate::pkg::capitalize_first(env!("CARGO_PKG_NAME"))
38        };
39
40        let pkg_version = env!("CARGO_PKG_VERSION");
41
42        if let Err(e) = $crate::pkg::print_motd(
43            &final_pkg_name,
44            pkg_version,
45            build.unwrap_or("Preview"),
46            $crate::pkg::MotdOptions {
47                mode,
48                timestamp,
49                environment,
50                copyright,
51            }
52        ) {
53            eprintln!("Failed to print motd: {}", e);
54        }
55    }};
56
57    (@assign bin = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
58        $bin = Some($val);
59    };
60    (@assign build = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
61        $build = Some($val);
62    };
63    (@assign mode = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
64        $mode = Some($val);
65    };
66    (@assign timestamp = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
67        $timestamp = Some($val);
68    };
69    (@assign environment = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
70        $environment = Some($val);
71    };
72    (@assign copyright = $val:expr, $bin:ident, $build:ident, $mode:ident, $timestamp:ident, $environment:ident, $copyright:ident) => {
73        $copyright = Some($val);
74    };
75
76    () => {
77        $crate::lazy_motd!(bin = "", build = "Preview");
78    };
79}