@@ -19,9 +19,7 @@ use std::process::{Child, Command};
19
19
use std:: { env, fs} ;
20
20
use std:: { thread, time:: Duration } ;
21
21
22
- use lazy_static:: lazy_static;
23
22
use rand:: Rng ;
24
- use regex:: Regex ;
25
23
use serde_json:: { self , Value } ;
26
24
27
25
pub struct Server {
@@ -37,9 +35,11 @@ struct Inner {
37
35
pidfile : PathBuf ,
38
36
}
39
37
40
- lazy_static ! {
41
- static ref SD_RE : Regex = Regex :: new( r#".+\sStore Directory:\s+"([^"]+)""# ) . unwrap( ) ;
42
- static ref CLIENT_RE : Regex = Regex :: new( r".+\sclient connections on\s+(\S+)" ) . unwrap( ) ;
38
+ macro_rules! regex {
39
+ ( $re: literal $( , ) ?) => { {
40
+ static RE : std:: sync:: OnceLock <regex:: Regex > = std:: sync:: OnceLock :: new( ) ;
41
+ RE . get_or_init( || regex:: Regex :: new( $re) . unwrap( ) )
42
+ } } ;
43
43
}
44
44
45
45
impl Drop for Server {
@@ -48,7 +48,7 @@ impl Drop for Server {
48
48
self . inner . child . wait ( ) . unwrap ( ) ;
49
49
if let Ok ( log) = fs:: read_to_string ( self . inner . logfile . as_os_str ( ) ) {
50
50
// Check if we had JetStream running and if so cleanup the storage directory.
51
- if let Some ( caps) = SD_RE . captures ( & log) {
51
+ if let Some ( caps) = regex ! ( r#".+\sStore Directory:\s+"([^"]+)""# ) . captures ( & log) {
52
52
let sd = caps. get ( 1 ) . map_or ( "" , |m| m. as_str ( ) ) ;
53
53
fs:: remove_dir_all ( sd) . ok ( ) ;
54
54
}
@@ -120,7 +120,7 @@ impl Server {
120
120
for _ in 0 ..100 {
121
121
match fs:: read_to_string ( self . inner . logfile . as_os_str ( ) ) {
122
122
Ok ( l) => {
123
- if let Some ( cre) = CLIENT_RE . captures ( & l) {
123
+ if let Some ( cre) = regex ! ( r".+\sclient connections on\s+(\S+)" ) . captures ( & l) {
124
124
return cre. get ( 1 ) . unwrap ( ) . as_str ( ) . replace ( "0.0.0.0" , "localhost" ) ;
125
125
} else {
126
126
thread:: sleep ( Duration :: from_millis ( 500 ) ) ;
0 commit comments