@@ -4,104 +4,9 @@ use std::io::{Error, ErrorKind};
44use std:: path:: { Path , PathBuf } ;
55use std:: str:: FromStr ;
66
7- use rust_code_analysis :: { FuncSpace , Ops } ;
7+ use serde :: Serialize ;
88
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- }
9+ use rust_code_analysis:: { FuncSpace , Ops } ;
10510
10611#[ derive( Debug , Clone ) ]
10712pub enum Format {
@@ -116,9 +21,118 @@ impl Format {
11621 & [ "cbor" , "json" , "toml" , "yaml" ]
11722 }
11823
119- format_spaces ! ( dump_formats, FuncSpace ) ;
24+ pub fn dump_formats (
25+ & self ,
26+ space : & FuncSpace ,
27+ path : & Path ,
28+ output_path : & Option < PathBuf > ,
29+ pretty : bool ,
30+ ) -> std:: io:: Result < ( ) > {
31+ self . format_spaces ( space, path, output_path, pretty)
32+ }
33+
34+ pub fn dump_ops_formats (
35+ & self ,
36+ space : & Ops ,
37+ path : & Path ,
38+ output_path : & Option < PathBuf > ,
39+ pretty : bool ,
40+ ) -> std:: io:: Result < ( ) > {
41+ self . format_spaces ( space, path, output_path, pretty)
42+ }
43+
44+ fn format_spaces < T : Serialize > (
45+ & self ,
46+ space : & T ,
47+ path : & Path ,
48+ output_path : & Option < PathBuf > ,
49+ pretty : bool ,
50+ ) -> std:: io:: Result < ( ) > {
51+ if output_path. is_none ( ) {
52+ let stdout = std:: io:: stdout ( ) ;
53+ let mut stdout = stdout. lock ( ) ;
12054
121- format_spaces ! ( dump_ops_formats, Ops ) ;
55+ match self {
56+ Format :: Cbor => Err ( Error :: new (
57+ ErrorKind :: Other ,
58+ "Cbor format cannot be printed to stdout" ,
59+ ) ) ,
60+ Format :: Json => {
61+ let json_data = if pretty {
62+ serde_json:: to_string_pretty ( & space) . unwrap ( )
63+ } else {
64+ serde_json:: to_string ( & space) . unwrap ( )
65+ } ;
66+ writeln ! ( stdout, "{}" , json_data)
67+ }
68+ Format :: Toml => {
69+ let toml_data = if pretty {
70+ toml:: to_string_pretty ( & space) . unwrap ( )
71+ } else {
72+ toml:: to_string ( & space) . unwrap ( )
73+ } ;
74+ writeln ! ( stdout, "{}" , toml_data)
75+ }
76+ Format :: Yaml => writeln ! ( stdout, "{}" , serde_yaml:: to_string( & space) . unwrap( ) ) ,
77+ }
78+ } else {
79+ let format_ext = match self {
80+ Format :: Cbor => ".cbor" ,
81+ Format :: Json => ".json" ,
82+ Format :: Toml => ".toml" ,
83+ Format :: Yaml => ".yml" ,
84+ } ;
85+
86+ // Remove root /
87+ let path = path. strip_prefix ( "/" ) . unwrap_or ( path) ;
88+
89+ // Remove root ./
90+ let path = path. strip_prefix ( "./" ) . unwrap_or ( path) ;
91+
92+ // Replace .. symbol with "_" to create a unique filename
93+ let cleaned_path: Vec < & str > = path
94+ . iter ( )
95+ . map ( |os_str| {
96+ let s_str = os_str. to_str ( ) . unwrap ( ) ;
97+ if s_str == ".." {
98+ "_"
99+ } else {
100+ s_str
101+ }
102+ } )
103+ . collect ( ) ;
104+
105+ // Create the filename
106+ let filename = cleaned_path. join ( "_" ) + format_ext;
107+
108+ let format_path = output_path. as_ref ( ) . unwrap ( ) . join ( filename) ;
109+
110+ let mut format_file = File :: create ( format_path) ?;
111+ match self {
112+ Format :: Cbor => serde_cbor:: to_writer ( format_file, & space)
113+ . map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) ) ,
114+ Format :: Json => {
115+ if pretty {
116+ serde_json:: to_writer_pretty ( format_file, & space)
117+ . map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) )
118+ } else {
119+ serde_json:: to_writer ( format_file, & space)
120+ . map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) )
121+ }
122+ }
123+ Format :: Toml => {
124+ let toml_data = if pretty {
125+ toml:: to_string_pretty ( & space) . unwrap ( )
126+ } else {
127+ toml:: to_string ( & space) . unwrap ( )
128+ } ;
129+ format_file. write_all ( toml_data. as_bytes ( ) )
130+ }
131+ Format :: Yaml => serde_yaml:: to_writer ( format_file, & space)
132+ . map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) ) ,
133+ }
134+ }
135+ }
122136}
123137
124138impl FromStr for Format {
0 commit comments