1
1
use std:: path:: { Path , PathBuf } ;
2
+ use std:: fs:: File ;
3
+ use std:: io:: Read ;
2
4
3
- pub fn render_playpen ( s : & mut str ) {
5
+ pub fn render_playpen ( s : & mut str , path : & Path ) {
6
+ // When replacing one thing in a string by something with a different length, the indices
7
+ // after that will not correspond, we therefore have to store the difference to correct this
8
+ let difference_index = 0 ;
4
9
5
- for playpen in find_playpens ( s) {
6
- println ! ( "Playpen{{ {}, {}, {:?}, {} }}" , playpen. start_index, playpen. end_index, playpen. rust_file, playpen. editable) ;
10
+ for playpen in find_playpens ( s, path) {
11
+
12
+ // Check if the file exists
13
+ if !playpen. rust_file . exists ( ) || !playpen. rust_file . is_file ( ) {
14
+ output ! ( "[-] No file exists for {{{{#playpen }}}}\n {}" , playpen. rust_file. to_str( ) . unwrap( ) ) ;
15
+ continue
16
+ }
17
+
18
+ // Open file & read file
19
+ let mut file = if let Ok ( f) = File :: open ( & playpen. rust_file ) { f } else { continue } ;
20
+ let mut file_content = String :: new ( ) ;
21
+ if let Err ( _) = file. read_to_string ( & mut file_content) { continue } ;
22
+
23
+ //println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
7
24
}
8
25
9
26
}
@@ -16,21 +33,21 @@ struct Playpen{
16
33
editable : bool
17
34
}
18
35
19
- fn find_playpens ( s : & str ) -> Vec < Playpen > {
36
+ fn find_playpens ( s : & str , base_path : & Path ) -> Vec < Playpen > {
20
37
let mut playpens = vec ! [ ] ;
21
38
for ( i, _) in s. match_indices ( "{{#playpen" ) {
22
- println ! ( "[*]: find_playpen" ) ;
39
+ debug ! ( "[*]: find_playpen" ) ;
23
40
24
41
// DON'T forget the "+ i" else you have an index out of bounds error !!
25
42
let end_i = if let Some ( n) = s[ i..] . find ( "}}" ) { n } else { continue } + i + 2 ;
26
43
27
- println ! ( "s[{}..{}] = {}" , i, end_i, s[ i..end_i] . to_string( ) ) ;
44
+ debug ! ( "s[{}..{}] = {}" , i, end_i, s[ i..end_i] . to_string( ) ) ;
28
45
29
46
// If there is nothing between "{{#playpen" and "}}" skip
30
47
if end_i-2 - ( i+10 ) < 1 { continue }
31
48
if s[ i+10 ..end_i-2 ] . trim ( ) . len ( ) == 0 { continue }
32
49
33
- println ! ( "{}" , s[ i+10 ..end_i-2 ] . to_string( ) ) ;
50
+ debug ! ( "{}" , s[ i+10 ..end_i-2 ] . to_string( ) ) ;
34
51
35
52
// Split on whitespaces
36
53
let params: Vec < & str > = s[ i+10 ..end_i-2 ] . split_whitespace ( ) . collect ( ) ;
@@ -44,7 +61,7 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
44
61
Playpen {
45
62
start_index : i as u32 ,
46
63
end_index : end_i as u32 ,
47
- rust_file : PathBuf :: from ( params[ 0 ] ) ,
64
+ rust_file : base_path . join ( PathBuf :: from ( params[ 0 ] ) ) ,
48
65
editable : editable,
49
66
}
50
67
)
@@ -64,28 +81,28 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
64
81
#[ test]
65
82
fn test_find_playpens_no_playpen ( ) {
66
83
let s = "Some random text without playpen..." ;
67
- assert ! ( find_playpens( s) == vec![ ] ) ;
84
+ assert ! ( find_playpens( s, Path :: new ( "" ) ) == vec![ ] ) ;
68
85
}
69
86
70
87
#[ test]
71
88
fn test_find_playpens_partial_playpen ( ) {
72
89
let s = "Some random text with {{#playpen..." ;
73
- assert ! ( find_playpens( s) == vec![ ] ) ;
90
+ assert ! ( find_playpens( s, Path :: new ( "" ) ) == vec![ ] ) ;
74
91
}
75
92
76
93
#[ test]
77
94
fn test_find_playpens_empty_playpen ( ) {
78
95
let s = "Some random text with {{#playpen}} and {{#playpen }}..." ;
79
- assert ! ( find_playpens( s) == vec![ ] ) ;
96
+ assert ! ( find_playpens( s, Path :: new ( "" ) ) == vec![ ] ) ;
80
97
}
81
98
82
99
#[ test]
83
100
fn test_find_playpens_simple_playpen ( ) {
84
101
let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}..." ;
85
102
86
- println ! ( "\n OUTPUT: {:?}\n " , find_playpens( s) ) ;
103
+ println ! ( "\n OUTPUT: {:?}\n " , find_playpens( s, Path :: new ( "" ) ) ) ;
87
104
88
- assert ! ( find_playpens( s) == vec![
105
+ assert ! ( find_playpens( s, Path :: new ( "" ) ) == vec![
89
106
Playpen { start_index: 22 , end_index: 42 , rust_file: PathBuf :: from( "file.rs" ) , editable: false } ,
90
107
Playpen { start_index: 47 , end_index: 68 , rust_file: PathBuf :: from( "test.rs" ) , editable: false }
91
108
] ) ;
@@ -95,10 +112,10 @@ fn test_find_playpens_simple_playpen() {
95
112
fn test_find_playpens_complex_playpen ( ) {
96
113
let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}..." ;
97
114
98
- println ! ( "\n OUTPUT: {:?}\n " , find_playpens( s) ) ;
115
+ println ! ( "\n OUTPUT: {:?}\n " , find_playpens( s, Path :: new ( "dir" ) ) ) ;
99
116
100
- assert ! ( find_playpens( s) == vec![
101
- Playpen { start_index: 22 , end_index: 51 , rust_file: PathBuf :: from( "file.rs" ) , editable: true } ,
102
- Playpen { start_index: 56 , end_index: 86 , rust_file: PathBuf :: from( "test.rs" ) , editable: true }
117
+ assert ! ( find_playpens( s, Path :: new ( "dir" ) ) == vec![
118
+ Playpen { start_index: 22 , end_index: 51 , rust_file: PathBuf :: from( "dir/ file.rs" ) , editable: true } ,
119
+ Playpen { start_index: 56 , end_index: 86 , rust_file: PathBuf :: from( "dir/ test.rs" ) , editable: true }
103
120
] ) ;
104
121
}
0 commit comments