Skip to content

Commit 58fe0f9

Browse files
committed
cli: Remove useless macro
1 parent b4a30c4 commit 58fe0f9

File tree

2 files changed

+93
-101
lines changed

2 files changed

+93
-101
lines changed

rust-code-analysis-cli/src/formats.rs

Lines changed: 92 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,7 @@ use std::io::{Error, ErrorKind};
44
use std::path::{Path, PathBuf};
55
use std::str::FromStr;
66

7-
use rust_code_analysis::{FuncSpace, Ops};
8-
9-
macro_rules! format_spaces {
10-
($func_name: ident, $type: ident) => {
11-
pub fn $func_name(
12-
&self,
13-
space: &$type,
14-
path: &Path,
15-
output_path: &Option<PathBuf>,
16-
pretty: bool,
17-
) -> std::io::Result<()> {
18-
if output_path.is_none() {
19-
let stdout = std::io::stdout();
20-
let mut stdout = stdout.lock();
21-
22-
match self {
23-
Format::Cbor => Err(Error::new(
24-
ErrorKind::Other,
25-
"Cbor format cannot be printed to stdout",
26-
)),
27-
Format::Json => {
28-
let json_data = if pretty {
29-
serde_json::to_string_pretty(&space).unwrap()
30-
} else {
31-
serde_json::to_string(&space).unwrap()
32-
};
33-
writeln!(stdout, "{}", json_data)
34-
}
35-
Format::Toml => {
36-
let toml_data = if pretty {
37-
toml::to_string_pretty(&space).unwrap()
38-
} else {
39-
toml::to_string(&space).unwrap()
40-
};
41-
writeln!(stdout, "{}", toml_data)
42-
}
43-
Format::Yaml => writeln!(stdout, "{}", serde_yaml::to_string(&space).unwrap()),
44-
}
45-
} else {
46-
let format_ext = match self {
47-
Format::Cbor => ".cbor",
48-
Format::Json => ".json",
49-
Format::Toml => ".toml",
50-
Format::Yaml => ".yml",
51-
};
52-
53-
// Remove root /
54-
let path = path.strip_prefix("/").unwrap_or(path);
55-
56-
// Remove root ./
57-
let path = path.strip_prefix("./").unwrap_or(path);
58-
59-
// Replace .. symbol with "_" to create a unique filename
60-
let cleaned_path: Vec<&str> = path
61-
.iter()
62-
.map(|os_str| {
63-
let s_str = os_str.to_str().unwrap();
64-
if s_str == ".." {
65-
"_"
66-
} else {
67-
s_str
68-
}
69-
})
70-
.collect();
71-
72-
// Create the filename
73-
let filename = cleaned_path.join("_") + format_ext;
74-
75-
let format_path = output_path.as_ref().unwrap().join(filename);
76-
77-
let mut format_file = File::create(format_path)?;
78-
match self {
79-
Format::Cbor => serde_cbor::to_writer(format_file, &space)
80-
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
81-
Format::Json => {
82-
if pretty {
83-
serde_json::to_writer_pretty(format_file, &space)
84-
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
85-
} else {
86-
serde_json::to_writer(format_file, &space)
87-
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
88-
}
89-
}
90-
Format::Toml => {
91-
let toml_data = if pretty {
92-
toml::to_string_pretty(&space).unwrap()
93-
} else {
94-
toml::to_string(&space).unwrap()
95-
};
96-
format_file.write_all(toml_data.as_bytes())
97-
}
98-
Format::Yaml => serde_yaml::to_writer(format_file, &space)
99-
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
100-
}
101-
}
102-
}
103-
};
104-
}
7+
use serde::Serialize;
1058

1069
#[derive(Debug, Clone)]
10710
pub enum Format {
@@ -116,9 +19,98 @@ impl Format {
11619
&["cbor", "json", "toml", "yaml"]
11720
}
11821

119-
format_spaces!(dump_formats, FuncSpace);
22+
pub fn dump_formats<T: Serialize>(
23+
&self,
24+
space: &T,
25+
path: &Path,
26+
output_path: &Option<PathBuf>,
27+
pretty: bool,
28+
) -> std::io::Result<()> {
29+
if output_path.is_none() {
30+
let stdout = std::io::stdout();
31+
let mut stdout = stdout.lock();
12032

121-
format_spaces!(dump_ops_formats, Ops);
33+
match self {
34+
Format::Cbor => Err(Error::new(
35+
ErrorKind::Other,
36+
"Cbor format cannot be printed to stdout",
37+
)),
38+
Format::Json => {
39+
let json_data = if pretty {
40+
serde_json::to_string_pretty(&space).unwrap()
41+
} else {
42+
serde_json::to_string(&space).unwrap()
43+
};
44+
writeln!(stdout, "{}", json_data)
45+
}
46+
Format::Toml => {
47+
let toml_data = if pretty {
48+
toml::to_string_pretty(&space).unwrap()
49+
} else {
50+
toml::to_string(&space).unwrap()
51+
};
52+
writeln!(stdout, "{}", toml_data)
53+
}
54+
Format::Yaml => writeln!(stdout, "{}", serde_yaml::to_string(&space).unwrap()),
55+
}
56+
} else {
57+
let format_ext = match self {
58+
Format::Cbor => ".cbor",
59+
Format::Json => ".json",
60+
Format::Toml => ".toml",
61+
Format::Yaml => ".yml",
62+
};
63+
64+
// Remove root /
65+
let path = path.strip_prefix("/").unwrap_or(path);
66+
67+
// Remove root ./
68+
let path = path.strip_prefix("./").unwrap_or(path);
69+
70+
// Replace .. symbol with "_" to create a unique filename
71+
let cleaned_path: Vec<&str> = path
72+
.iter()
73+
.map(|os_str| {
74+
let s_str = os_str.to_str().unwrap();
75+
if s_str == ".." {
76+
"_"
77+
} else {
78+
s_str
79+
}
80+
})
81+
.collect();
82+
83+
// Create the filename
84+
let filename = cleaned_path.join("_") + format_ext;
85+
86+
let format_path = output_path.as_ref().unwrap().join(filename);
87+
88+
let mut format_file = File::create(format_path)?;
89+
match self {
90+
Format::Cbor => serde_cbor::to_writer(format_file, &space)
91+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
92+
Format::Json => {
93+
if pretty {
94+
serde_json::to_writer_pretty(format_file, &space)
95+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
96+
} else {
97+
serde_json::to_writer(format_file, &space)
98+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
99+
}
100+
}
101+
Format::Toml => {
102+
let toml_data = if pretty {
103+
toml::to_string_pretty(&space).unwrap()
104+
} else {
105+
toml::to_string(&space).unwrap()
106+
};
107+
format_file.write_all(toml_data.as_bytes())
108+
}
109+
Format::Yaml => serde_yaml::to_writer(format_file, &space)
110+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
111+
}
112+
}
113+
}
122114
}
123115

124116
impl FromStr for Format {

rust-code-analysis-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn act_on_file(language: Option<LANG>, path: PathBuf, cfg: &Config) -> std::io::
123123
} else if cfg.ops {
124124
if let Some(output_format) = &cfg.output_format {
125125
let ops = get_ops(&language, source, &path, pr).unwrap();
126-
output_format.dump_ops_formats(&ops, &path, &cfg.output, cfg.pretty)
126+
output_format.dump_formats(&ops, &path, &cfg.output, cfg.pretty)
127127
} else {
128128
let cfg = OpsCfg { path };
129129
let path = cfg.path.clone();

0 commit comments

Comments
 (0)