@@ -42,21 +42,20 @@ pub mod docs {
4242 impl Doc {
4343 /// # Build a `Doc` from an array of strings
4444 /// Parse `Doc` fields.
45- pub fn make_doc ( vector : & [ String ] ) -> Doc {
46- let delims: Delimiters = get_delims ( ) ;
45+ pub fn make_doc ( vector : & [ String ] , delims : Delimiters ) -> Doc {
4746 let mut result: Doc = Default :: default ( ) ;
4847 for line in vector. iter ( ) {
4948 if line == & vector[ 0 ] {
5049 result. short_description . push_str ( line) ;
51- } else if line. contains ( delims. params . as_str ( ) ) {
50+ } else if line. contains ( delims. params ) {
5251 let splitted: Vec < _ > = line. split_whitespace ( ) . map ( |x| x. to_string ( ) ) . collect ( ) ;
5352 let rest: String = splitted[ 2 ..] . join ( " " ) ;
5453 result. params . insert ( splitted[ 1 ] . replace ( ":" , "" ) , rest) ;
55- } else if line. contains ( delims. ret . as_str ( ) ) {
54+ } else if line. contains ( delims. ret ) {
5655 let splitted: Vec < _ > = line. split_whitespace ( ) . map ( |x| x. to_string ( ) ) . collect ( ) ;
5756 let rest: String = splitted[ 2 ..] . join ( " " ) ;
5857 result. returns . insert ( splitted[ 1 ] . replace ( ":" , "" ) , rest) ;
59- } else if line. contains ( delims. opt . as_str ( ) ) {
58+ } else if line. contains ( delims. opt ) {
6059 let splitted: Vec < _ > = line. split_whitespace ( ) . map ( |x| x. to_string ( ) ) . collect ( ) ;
6160 let rest: String = splitted[ 3 ..] . join ( " " ) ;
6261 result
@@ -91,10 +90,9 @@ pub mod docs {
9190 /// and adds every line to a `Vec` until the end delimiter.
9291 ///
9392 /// A final `Vec` of the collected comment strings is returned.
94- fn get_info ( p : & Path ) -> Vec < Vec < String > > {
93+ fn get_info ( p : & Path , delims : Delimiters ) -> Vec < Vec < String > > {
9594 // let mut p = dirs::home_dir().unwrap();
9695 // p.push(".zshrc");
97- let delims: Delimiters = get_delims ( ) ;
9896 let f = File :: open ( & p) . expect ( "file not found." ) ;
9997 let f = BufReader :: new ( f) ;
10098 let mut result: Vec < Vec < String > > = Vec :: new ( ) ;
@@ -103,54 +101,55 @@ pub mod docs {
103101 let mut index = 0 ;
104102 for line in f. lines ( ) {
105103 let curr_line = line. expect ( "Line cannot be accessed." ) ;
106- if curr_line. contains ( delims. start . as_str ( ) ) {
104+ if curr_line. contains ( delims. start ) {
107105 can_add = true ;
108106 continue ;
109- } else if curr_line. contains ( delims. end . as_str ( ) ) {
107+ } else if curr_line. contains ( delims. end ) {
110108 can_add = false ;
111109 index += 1 ;
112110 result. push ( Vec :: new ( ) ) ;
113111 }
114112 if can_add {
115- if curr_line. contains ( delims. opt . as_str ( ) ) {
113+ if curr_line. contains ( delims. opt ) {
116114 result[ index] . push ( curr_line) ;
117115 } else {
118- result[ index] . push ( curr_line. replace ( delims. comm . as_str ( ) , "" ) ) ;
116+ result[ index] . push ( curr_line. replace ( delims. comm , "" ) ) ;
119117 }
120118 }
121119 }
122120 result
123121 }
124122
125- fn generate_doc_file ( docs : & [ Vec < String > ] , fname : String ) -> DocFile {
123+ fn generate_doc_file ( docs : & [ Vec < String > ] , fname : String , delims : Delimiters ) -> DocFile {
126124 let mut all_docs: DocFile = Default :: default ( ) ;
127125 all_docs. filename = fname;
128126 for doc in docs. iter ( ) {
129127 if doc. to_vec ( ) . is_empty ( ) {
130128 continue ;
131129 }
132- let as_bash_doc = Doc :: make_doc ( & doc. to_vec ( ) ) ;
130+ let as_bash_doc = Doc :: make_doc ( & doc. to_vec ( ) , delims ) ;
133131 all_docs. add ( as_bash_doc) ;
134132 }
135133 all_docs
136134 }
137135
138- pub fn start ( p : & str , is_directory : bool ) -> Vec < DocFile > {
136+ pub fn start ( p : & str , is_directory : bool , delims : Delimiters ) -> Vec < DocFile > {
139137 let dir = p. replace ( "~" , home_dir ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ) ;
140138 if is_directory {
141139 let files: Vec < _ > = glob ( & dir) . unwrap ( ) . filter_map ( |x| x. ok ( ) ) . collect ( ) ;
142140 let every_doc: Vec < DocFile > = files
143141 . par_iter ( )
144142 . map ( |entry| {
145- let docs = get_info ( & entry) ;
143+ let docs = get_info ( & entry, delims ) ;
146144 generate_doc_file (
147145 & docs,
148146 entry. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
147+ delims,
149148 )
150149 } ) . collect ( ) ;
151150 every_doc
152151 } else {
153- let docs = get_info ( & Path :: new ( & p) ) ;
152+ let docs = get_info ( & Path :: new ( & p) , delims ) ;
154153 let all_docs = generate_doc_file (
155154 & docs,
156155 Path :: new ( & dir)
@@ -159,6 +158,7 @@ pub mod docs {
159158 . to_str ( )
160159 . unwrap ( )
161160 . to_string ( ) ,
161+ delims,
162162 ) ;
163163 let result = vec ! [ all_docs] ;
164164 result
@@ -251,50 +251,70 @@ pub mod docs {
251251 . expect ( "Could not write to file." ) ;
252252 }
253253
254- #[ derive( Debug , Serialize , Deserialize ) ]
255- struct Delimiters {
256- start : String ,
257- end : String ,
258- params : String ,
259- ret : String ,
260- opt : String ,
261- comm : String ,
254+ #[ derive( Debug , Serialize , Deserialize , Copy , Clone ) ]
255+ pub struct Delimiters < ' a > {
256+ start : & ' a str ,
257+ end : & ' a str ,
258+ params : & ' a str ,
259+ ret : & ' a str ,
260+ opt : & ' a str ,
261+ comm : & ' a str ,
262262 }
263263
264- impl Default for Delimiters {
265- fn default ( ) -> Delimiters {
264+ impl < ' a > Default for Delimiters < ' a > {
265+ fn default ( ) -> Delimiters < ' a > {
266266 Delimiters {
267- start : "#;" . to_string ( ) ,
268- end : "#\" " . to_string ( ) ,
269- params : "@param" . to_string ( ) ,
270- ret : "@return" . to_string ( ) ,
271- opt : "# -" . to_string ( ) ,
272- comm : "# " . to_string ( ) ,
267+ start : "#;" ,
268+ end : "#\" " ,
269+ params : "@param" ,
270+ ret : "@return" ,
271+ opt : "# -" ,
272+ comm : "# " ,
273273 }
274274 }
275275 }
276276
277- fn get_delims ( ) -> Delimiters {
278- let mut contents = String :: new ( ) ;
279- match env:: var_os ( "BASHDOC_CONFIG_PATH" ) {
280- Some ( val) => {
281- let mut config = File :: open ( Path :: new ( & val) ) . expect ( "Invalid path" ) ;
282- config
283- . read_to_string ( & mut contents)
284- . expect ( "could not read from file." ) ;
285- let mut to_convert = String :: new ( ) ;
286- to_convert. push_str ( & contents) ;
287- let sorted: Delimiters = toml:: from_str ( & to_convert. as_str ( ) ) . unwrap ( ) ;
288- sorted
277+ impl < ' a > Delimiters < ' a > {
278+ pub fn override_delims ( overrides : String ) -> Self {
279+ let mut result: Delimiters = Delimiters :: default ( ) ;
280+ let splitted: Vec < _ > = Box :: leak ( overrides. into_boxed_str ( ) )
281+ . split_whitespace ( )
282+ . collect ( ) ;
283+ if splitted. len ( ) != 6 {
284+ panic ! ( "Please enter the proper number of delimiters" ) ;
289285 }
290- None => {
291- let mut delimiters = Delimiters :: default ( ) ;
292- let content =
293- toml:: to_string_pretty ( & delimiters) . expect ( "Could not be converted to TOML" ) ;
294- let mut path = home_dir ( ) . unwrap ( ) ;
295- path. push ( ".bashdocrc" ) ;
296- fs:: write ( path. to_str ( ) . unwrap ( ) , content) . unwrap ( ) ;
297- delimiters
286+ result. start = & splitted[ 0 ] ;
287+ result. end = & splitted[ 1 ] ;
288+ result. params = & splitted[ 2 ] ;
289+ result. ret = & splitted[ 3 ] ;
290+ result. opt = & splitted[ 4 ] ;
291+ result. comm = & splitted[ 5 ] ;
292+ result
293+ }
294+
295+ pub fn get_delims ( ) -> Self {
296+ let mut contents = String :: new ( ) ;
297+ match env:: var_os ( "BASHDOC_CONFIG_PATH" ) {
298+ Some ( val) => {
299+ let mut config = File :: open ( Path :: new ( & val) ) . expect ( "Invalid path" ) ;
300+ config
301+ . read_to_string ( & mut contents)
302+ . expect ( "could not read from file." ) ;
303+ let mut to_convert = String :: new ( ) ;
304+ to_convert. push_str ( & contents) ;
305+ let mut as_static: & ' static str = Box :: leak ( to_convert. into_boxed_str ( ) ) ;
306+ let sorted: Delimiters = toml:: from_str ( & as_static) . unwrap ( ) ;
307+ sorted
308+ }
309+ None => {
310+ let mut delimiters = Delimiters :: default ( ) ;
311+ let content = toml:: to_string_pretty ( & delimiters)
312+ . expect ( "Could not be converted to TOML" ) ;
313+ let mut path = home_dir ( ) . unwrap ( ) ;
314+ path. push ( ".bashdocrc" ) ;
315+ fs:: write ( path. to_str ( ) . unwrap ( ) , content) . unwrap ( ) ;
316+ delimiters
317+ }
298318 }
299319 }
300320 }
@@ -324,7 +344,7 @@ pub mod docs {
324344 "@params location: where to put it" . to_string( ) ,
325345 "@returns nothing:" . to_string( ) ,
326346 ] ;
327- let result = Doc :: make_doc ( & input) ;
347+ let result = Doc :: make_doc ( & input, Delimiters :: get_delims ( ) ) ;
328348 let mut expected = Doc :: default ( ) ;
329349 expected. short_description = "runner()" . to_string ( ) ;
330350 expected. long_description = "This is the beginning" . to_string ( ) ;
@@ -368,7 +388,7 @@ pub mod docs {
368388 #[ test]
369389 fn test_get_info ( ) {
370390 let p = Path :: new ( "example.sh" ) ;
371- let result = get_info ( & p) ;
391+ let result = get_info ( & p, Delimiters :: get_delims ( ) ) ;
372392 let expected: Vec < Vec < String > > = vec ! [
373393 [
374394 "runner()" . to_string( ) ,
0 commit comments