aboutsummaryrefslogtreecommitdiff
path: root/src/commands/build/macros.rs
blob: 4281dff0f42181015b1dbe1b71c287a0e4298643 (plain) (blame)
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
// This file is part of Chorus <https://github.com/Fuwn/chorus>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

/// "None if empty string, or with preformat"
///
/// By default, this macro evaluates whether the input `$string` is empty by
/// calling `is_empty`. However, by specifying the type `i32` at the end of the
/// macro invocation, you can tell this macro to evaluate based on `== 0`
/// instead of `is_empty`.
#[macro_export]
macro_rules! niesowp {
  ($preformat:literal, $string:expr) => {{
    if $string.is_empty() {
      "".to_string()
    } else {
      format!("{}{}", $preformat, $string)
    }
  }};
  ($preformat:literal, $number:expr,i32) => {{
    if $number == 0 {
      "".to_string()
    } else {
      format!("{}{}", $preformat, $number)
    }
  }};
}

#[macro_export]
macro_rules! compiler_get_or {
  ($compiler:ident, $key:ident, $default:literal) => {
    $crate::config::CONFIG.build.$compiler.as_ref().map_or_else(
      || $default.into(),
      |compiler| {
        compiler
          .$key
          .as_ref()
          .map_or_else(|| $default.into(), Clone::clone)
      },
    )
  };
}

/// A helper to assist with the sanity checks
#[macro_export]
macro_rules! entry_exists {
  ($entries:ident, $path:ident, $entry:literal, $context:literal) => {
    let entry = $entry.replace("\\", "/");

    if !$entries.contains(&PathBuf::from_slash(
      format!("{}/{}", $path.as_path().display(), entry).to_string(),
    )) {
      return Err(anyhow::anyhow!(
        "{} \"{}\" does not exist",
        $context,
        format!("{}/{}", $path.as_path().display(), entry)
      ));
    }
  };
}