@@ -12,6 +12,7 @@ use std::str::FromStr;
12
12
use crate :: help_messages:: { command_message, mode_message} ;
13
13
use crate :: normalize_path;
14
14
use crate :: python_util:: { detect_magic_number, get_python_version, PythonVersion } ;
15
+ use crate :: random:: random;
15
16
use crate :: serialize:: { get_magic_num_from_bytes, get_ver_from_magic_num} ;
16
17
use crate :: stdin:: GLOBAL_STDIN ;
17
18
use crate :: { power_assert, read_file} ;
@@ -111,18 +112,30 @@ impl DummyStdin {
111
112
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
112
113
pub enum Input {
113
114
File ( PathBuf ) ,
114
- REPL ,
115
+ REPL ( u64 ) ,
115
116
DummyREPL ( DummyStdin ) ,
116
117
/// same content as cfg.command
117
- Pipe ( String ) ,
118
+ Pipe ( u64 , String ) ,
118
119
/// from command option | eval
119
- Str ( String ) ,
120
+ Str ( u64 , String ) ,
120
121
Dummy ,
121
122
}
122
123
123
124
impl Input {
124
- pub fn is_repl ( & self ) -> bool {
125
- matches ! ( self , Input :: REPL | Input :: DummyREPL ( _) )
125
+ pub const fn is_repl ( & self ) -> bool {
126
+ matches ! ( self , Input :: REPL ( _) | Input :: DummyREPL ( _) )
127
+ }
128
+
129
+ pub fn pipe ( src : String ) -> Self {
130
+ Self :: Pipe ( random ( ) , src)
131
+ }
132
+
133
+ pub fn str ( src : String ) -> Self {
134
+ Self :: Str ( random ( ) , src)
135
+ }
136
+
137
+ pub fn repl ( ) -> Self {
138
+ Self :: REPL ( random ( ) )
126
139
}
127
140
128
141
pub fn path ( & self ) -> Option < & Path > {
@@ -132,40 +145,57 @@ impl Input {
132
145
}
133
146
}
134
147
148
+ pub const fn id ( & self ) -> u64 {
149
+ match self {
150
+ Input :: File ( _) | Input :: DummyREPL ( _) | Input :: Dummy => 0 ,
151
+ Input :: REPL ( id) | Input :: Pipe ( id, _) | Input :: Str ( id, _) => * id,
152
+ }
153
+ }
154
+
135
155
pub fn enclosed_name ( & self ) -> & str {
136
156
match self {
137
157
Self :: File ( filename) => filename. to_str ( ) . unwrap_or ( "_" ) ,
138
- Self :: REPL | Self :: DummyREPL ( _) | Self :: Pipe ( _) => "<stdin>" ,
139
- Self :: Str ( _) => "<string>" ,
158
+ Self :: REPL ( _ ) | Self :: DummyREPL ( _) | Self :: Pipe ( _ , _) => "<stdin>" ,
159
+ Self :: Str ( _, _ ) => "<string>" ,
140
160
Self :: Dummy => "<dummy>" ,
141
161
}
142
162
}
143
163
144
- pub fn full_path ( & self ) -> & str {
164
+ pub fn full_path ( & self ) -> PathBuf {
145
165
match self {
146
- Self :: File ( filename) => filename. to_str ( ) . unwrap_or ( "_" ) ,
147
- Self :: REPL | Self :: DummyREPL ( _) | Self :: Pipe ( _) => "stdin" ,
148
- Self :: Str ( _) => "string" ,
149
- Self :: Dummy => "dummy" ,
166
+ Self :: File ( filename) => filename. clone ( ) ,
167
+ Self :: REPL ( id) | Self :: Pipe ( id, _) => PathBuf :: from ( format ! ( "stdin_{id}" ) ) ,
168
+ Self :: DummyREPL ( dummy) => PathBuf :: from ( format ! ( "stdin_{}" , dummy. name) ) ,
169
+ Self :: Str ( id, _) => PathBuf :: from ( format ! ( "string_{id}" ) ) ,
170
+ Self :: Dummy => PathBuf :: from ( "dummy" ) ,
150
171
}
151
172
}
152
173
153
- pub fn file_stem ( & self ) -> & str {
174
+ pub fn file_stem ( & self ) -> String {
154
175
match self {
155
- Self :: File ( filename) => filename. file_stem ( ) . and_then ( |f| f. to_str ( ) ) . unwrap_or ( "_" ) ,
156
- Self :: REPL | Self :: DummyREPL ( _) | Self :: Pipe ( _) => "stdin" ,
157
- Self :: Str ( _) => "string" ,
158
- Self :: Dummy => "dummy" ,
176
+ Self :: File ( filename) => filename
177
+ . file_stem ( )
178
+ . and_then ( |f| f. to_str ( ) )
179
+ . unwrap_or ( "_" )
180
+ . to_string ( ) ,
181
+ Self :: REPL ( id) | Self :: Pipe ( id, _) => format ! ( "stdin_{id}" ) ,
182
+ Self :: DummyREPL ( stdin) => format ! ( "stdin_{}" , stdin. name) ,
183
+ Self :: Str ( id, _) => format ! ( "string_{id}" ) ,
184
+ Self :: Dummy => "dummy" . to_string ( ) ,
159
185
}
160
186
}
161
187
162
- pub fn filename ( & self ) -> & str {
188
+ pub fn filename ( & self ) -> String {
163
189
match self {
164
- Self :: File ( filename) => filename. file_name ( ) . and_then ( |f| f. to_str ( ) ) . unwrap_or ( "_" ) ,
165
- Self :: REPL | Self :: Pipe ( _) => "stdin" ,
166
- Self :: DummyREPL ( stdin) => & stdin. name ,
167
- Self :: Str ( _) => "string" ,
168
- Self :: Dummy => "dummy" ,
190
+ Self :: File ( filename) => filename
191
+ . file_name ( )
192
+ . and_then ( |f| f. to_str ( ) )
193
+ . unwrap_or ( "_" )
194
+ . to_string ( ) ,
195
+ Self :: REPL ( id) | Self :: Pipe ( id, _) => format ! ( "stdin_{id}" ) ,
196
+ Self :: DummyREPL ( stdin) => format ! ( "stdin_{}" , stdin. name) ,
197
+ Self :: Str ( id, _) => format ! ( "string_{id}" ) ,
198
+ Self :: Dummy => "dummy" . to_string ( ) ,
169
199
}
170
200
}
171
201
@@ -193,8 +223,8 @@ impl Input {
193
223
}
194
224
}
195
225
}
196
- Self :: Pipe ( s) | Self :: Str ( s) => s. clone ( ) ,
197
- Self :: REPL => GLOBAL_STDIN . read ( ) ,
226
+ Self :: Pipe ( _ , s) | Self :: Str ( _ , s) => s. clone ( ) ,
227
+ Self :: REPL ( _ ) => GLOBAL_STDIN . read ( ) ,
198
228
Self :: DummyREPL ( dummy) => dummy. read_line ( ) . unwrap_or_default ( ) ,
199
229
Self :: Dummy => panic ! ( "cannot read from a dummy file" ) ,
200
230
}
@@ -224,8 +254,8 @@ impl Input {
224
254
}
225
255
}
226
256
}
227
- Self :: Pipe ( s) | Self :: Str ( s) => s. clone ( ) ,
228
- Self :: REPL => GLOBAL_STDIN . read ( ) ,
257
+ Self :: Pipe ( _ , s) | Self :: Str ( _ , s) => s. clone ( ) ,
258
+ Self :: REPL ( _ ) => GLOBAL_STDIN . read ( ) ,
229
259
Self :: Dummy | Self :: DummyREPL ( _) => panic ! ( "cannot read from a dummy file" ) ,
230
260
}
231
261
}
@@ -244,12 +274,12 @@ impl Input {
244
274
}
245
275
Err ( _) => vec ! [ "<file not found>" . into( ) ] ,
246
276
} ,
247
- Self :: Pipe ( s) | Self :: Str ( s) => s. split ( '\n' ) . collect :: < Vec < _ > > ( )
277
+ Self :: Pipe ( _ , s) | Self :: Str ( _ , s) => s. split ( '\n' ) . collect :: < Vec < _ > > ( )
248
278
[ ln_begin - 1 ..=ln_end - 1 ]
249
279
. iter ( )
250
280
. map ( |s| s. to_string ( ) )
251
281
. collect ( ) ,
252
- Self :: REPL => GLOBAL_STDIN . reread_lines ( ln_begin, ln_end) ,
282
+ Self :: REPL ( _ ) => GLOBAL_STDIN . reread_lines ( ln_begin, ln_end) ,
253
283
Self :: DummyREPL ( dummy) => dummy. reread_lines ( ln_begin, ln_end) ,
254
284
Self :: Dummy => panic ! ( "cannot read lines from a dummy file" ) ,
255
285
}
@@ -258,8 +288,8 @@ impl Input {
258
288
pub fn reread ( & self ) -> String {
259
289
match self {
260
290
Self :: File ( _filename) => todo ! ( ) ,
261
- Self :: Pipe ( s) | Self :: Str ( s) => s. clone ( ) ,
262
- Self :: REPL => GLOBAL_STDIN . reread ( ) . trim_end ( ) . to_owned ( ) ,
291
+ Self :: Pipe ( _ , s) | Self :: Str ( _ , s) => s. clone ( ) ,
292
+ Self :: REPL ( _ ) => GLOBAL_STDIN . reread ( ) . trim_end ( ) . to_owned ( ) ,
263
293
Self :: DummyREPL ( dummy) => dummy. reread ( ) . unwrap_or_default ( ) ,
264
294
Self :: Dummy => panic ! ( "cannot read from a dummy file" ) ,
265
295
}
@@ -366,7 +396,7 @@ impl Default for ErgConfig {
366
396
py_server_timeout : 10 ,
367
397
quiet_repl : false ,
368
398
show_type : false ,
369
- input : Input :: REPL ,
399
+ input : Input :: repl ( ) ,
370
400
output_dir : None ,
371
401
module : "<module>" ,
372
402
verbose : 1 ,
@@ -393,29 +423,26 @@ impl ErgConfig {
393
423
self . clone ( )
394
424
}
395
425
396
- pub fn dump_path ( & self ) -> String {
426
+ pub fn dump_path ( & self ) -> PathBuf {
397
427
if let Some ( output) = & self . output_dir {
398
- format ! ( "{output}/{}" , self . input. filename( ) )
428
+ PathBuf :: from ( format ! ( "{output}/{}" , self . input. filename( ) ) )
399
429
} else {
400
- self . input . full_path ( ) . to_string ( )
430
+ self . input . full_path ( )
401
431
}
402
432
}
403
433
404
434
pub fn dump_filename ( & self ) -> String {
405
435
if let Some ( output) = & self . output_dir {
406
436
format ! ( "{output}/{}" , self . input. filename( ) )
407
437
} else {
408
- self . input . filename ( ) . to_string ( )
438
+ self . input . filename ( )
409
439
}
410
440
}
411
441
412
- pub fn dump_pyc_path ( & self ) -> String {
413
- let dump_path = self . dump_path ( ) ;
414
- if dump_path. ends_with ( ".er" ) {
415
- dump_path. replace ( ".er" , ".pyc" )
416
- } else {
417
- dump_path + ".pyc"
418
- }
442
+ pub fn dump_pyc_path ( & self ) -> PathBuf {
443
+ let mut dump_path = self . dump_path ( ) ;
444
+ dump_path. set_extension ( ".pyc" ) ;
445
+ dump_path
419
446
}
420
447
421
448
pub fn dump_pyc_filename ( & self ) -> String {
@@ -450,7 +477,7 @@ impl ErgConfig {
450
477
break ;
451
478
}
452
479
"-c" | "--code" => {
453
- cfg. input = Input :: Str ( args. next ( ) . expect ( "the value of `-c` is not passed" ) ) ;
480
+ cfg. input = Input :: str ( args. next ( ) . expect ( "the value of `-c` is not passed" ) ) ;
454
481
}
455
482
"--check" => {
456
483
cfg. mode = ErgMode :: FullCheck ;
@@ -630,15 +657,15 @@ USAGE:
630
657
}
631
658
}
632
659
}
633
- if cfg. input == Input :: REPL && cfg. mode != ErgMode :: LanguageServer {
660
+ if cfg. input . is_repl ( ) && cfg. mode != ErgMode :: LanguageServer {
634
661
use crate :: tty:: IsTty ;
635
662
let is_stdin_piped = !stdin ( ) . is_tty ( ) ;
636
663
let input = if is_stdin_piped {
637
664
let mut buffer = String :: new ( ) ;
638
665
stdin ( ) . read_to_string ( & mut buffer) . unwrap ( ) ;
639
- Input :: Pipe ( buffer)
666
+ Input :: pipe ( buffer)
640
667
} else {
641
- Input :: REPL
668
+ Input :: repl ( )
642
669
} ;
643
670
cfg. input = input;
644
671
}
0 commit comments