Skip to content

Commit 832b627

Browse files
committed
Move string_enum to util module
1 parent 33668c1 commit 832b627

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::collections::{BTreeSet, HashMap, HashSet};
2+
use std::iter;
23
use std::process::Command;
3-
use std::str::FromStr;
44
use std::sync::OnceLock;
5-
use std::{fmt, iter};
65

76
use build_helper::git::GitConfig;
87
use camino::{Utf8Path, Utf8PathBuf};
@@ -12,48 +11,7 @@ use serde::de::{Deserialize, Deserializer, Error as _};
1211
pub use self::Mode::*;
1312
use crate::executor::{ColorConfig, OutputFormat};
1413
use crate::fatal;
15-
use crate::util::{Utf8PathBufExt, add_dylib_path};
16-
17-
macro_rules! string_enum {
18-
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
19-
$(#[$meta])*
20-
$vis enum $name {
21-
$($variant,)*
22-
}
23-
24-
impl $name {
25-
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
26-
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
27-
28-
$vis const fn to_str(&self) -> &'static str {
29-
match self {
30-
$(Self::$variant => $repr,)*
31-
}
32-
}
33-
}
34-
35-
impl fmt::Display for $name {
36-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37-
fmt::Display::fmt(self.to_str(), f)
38-
}
39-
}
40-
41-
impl FromStr for $name {
42-
type Err = String;
43-
44-
fn from_str(s: &str) -> Result<Self, Self::Err> {
45-
match s {
46-
$($repr => Ok(Self::$variant),)*
47-
_ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
48-
}
49-
}
50-
}
51-
}
52-
}
53-
54-
// Make the macro visible outside of this module, for tests.
55-
#[cfg(test)]
56-
pub(crate) use string_enum;
14+
use crate::util::{Utf8PathBufExt, add_dylib_path, string_enum};
5715

5816
string_enum! {
5917
#[derive(Clone, Copy, PartialEq, Debug)]

src/tools/compiletest/src/tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ fn is_test_test() {
6767

6868
#[test]
6969
fn string_enums() {
70-
// These imports are needed for the macro-generated code
71-
use std::fmt;
72-
use std::str::FromStr;
73-
74-
crate::common::string_enum! {
70+
crate::util::string_enum! {
7571
#[derive(Clone, Copy, Debug, PartialEq)]
7672
enum Animal {
7773
Cat => "meow",

src/tools/compiletest/src/util.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,42 @@ macro_rules! static_regex {
104104
}};
105105
}
106106
pub(crate) use static_regex;
107+
108+
macro_rules! string_enum {
109+
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
110+
$(#[$meta])*
111+
$vis enum $name {
112+
$($variant,)*
113+
}
114+
115+
impl $name {
116+
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
117+
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
118+
119+
$vis const fn to_str(&self) -> &'static str {
120+
match self {
121+
$(Self::$variant => $repr,)*
122+
}
123+
}
124+
}
125+
126+
impl ::std::fmt::Display for $name {
127+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
128+
::std::fmt::Display::fmt(self.to_str(), f)
129+
}
130+
}
131+
132+
impl ::std::str::FromStr for $name {
133+
type Err = String;
134+
135+
fn from_str(s: &str) -> Result<Self, Self::Err> {
136+
match s {
137+
$($repr => Ok(Self::$variant),)*
138+
_ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
139+
}
140+
}
141+
}
142+
}
143+
}
144+
145+
pub(crate) use string_enum;

0 commit comments

Comments
 (0)