@ -113,23 +113,39 @@ impl<'a> Link<'a> {
. split ( LINE_BREAKS )
. split ( LINE_BREAKS )
. map ( | str | str . trim ( ) )
. map ( | str | str . trim ( ) )
. filter ( | trimmed | ! trimmed . is_empty ( ) )
. filter ( | trimmed | ! trimmed . is_empty ( ) )
. map( | mat | {
. filter_ map( | mat | {
let mut split_n = mat . splitn ( 2 , '=' ) ;
let mut split_n = mat . splitn ( 2 , '=' ) ;
let key = split_n . next ( ) . unwrap ( ) . trim ( ) ;
if let Some ( key ) = split_n . next ( ) {
let value = split_n . next ( ) . unwrap ( ) ;
let key = key . trim ( ) ;
( key , value )
if let Some ( value ) = split_n . next ( ) {
return Some ( ( key , value ) ) ;
}
}
eprintln! (
"Couldn't find a key/value pair while parsing the argument '{}'" ,
mat
) ;
None
} )
} )
. collect ::< Vec < _ > > ( ) ,
. collect ::< Vec < _ > > ( ) ,
// This looks like {{#template <file> <args>}}
// This looks like {{#template <file> <args>}}
false = > TEMPLATE_ARGS
false = > TEMPLATE_ARGS
. captures_iter ( args . as_str ( ) )
. captures_iter ( args . as_str ( ) )
. into_iter ( )
. filter_map ( | mat | {
. map ( | mat | {
let captures = mat . ok ( ) ? ;
let mut split_n = mat . unwrap ( ) . get ( 0 ) . unwrap ( ) . as_str ( ) . splitn ( 2 , '=' ) ;
let mut split_n = captures . get ( 0 ) ? . as_str ( ) . splitn ( 2 , '=' ) ;
let key = split_n . next ( ) . unwrap ( ) . trim ( ) ;
if let Some ( key ) = split_n . next ( ) {
let value = split_n . next ( ) . unwrap ( ) ;
let key = key . trim ( ) ;
( key , value )
if let Some ( value ) = split_n . next ( ) {
return Some ( ( key . trim ( ) , value ) ) ;
}
}
eprintln! (
"Couldn't parse key or value while parsing '{:?}'" ,
& args . as_str ( )
) ;
None
} )
} )
. collect ::< Vec < _ > > ( ) ,
. collect ::< Vec < _ > > ( ) ,
} ;
} ;
@ -157,7 +173,7 @@ impl<'a> Link<'a> {
FR : FileReader ,
FR : FileReader ,
{
{
match self . link_type {
match self . link_type {
LinkType ::Escaped = > Ok ( ( & self . link_text [ 1 .. ] ) . to_owned ( ) ) ,
LinkType ::Escaped = > Ok ( ( self . link_text [ 1 .. ] ) . to_owned ( ) ) ,
LinkType ::Template ( ref pat ) = > {
LinkType ::Template ( ref pat ) = > {
let target = base . as_ref ( ) . join ( pat ) ;
let target = base . as_ref ( ) . join ( pat ) ;
let contents = file_reader . read_to_string ( & target , self . link_text ) ? ;
let contents = file_reader . read_to_string ( & target , self . link_text ) ? ;
@ -195,7 +211,7 @@ impl<'a> Iterator for LinkIter<'a> {
fn next ( & mut self ) -> Option < Self ::Item > {
fn next ( & mut self ) -> Option < Self ::Item > {
for cap in & mut self . 0 {
for cap in & mut self . 0 {
if let Some ( inc ) = Link ::from_capture ( cap . unwrap( ) ) {
if let Some ( inc ) = Link ::from_capture ( cap . ok( ) ? ) {
return Some ( inc ) ;
return Some ( inc ) ;
}
}
}
}
@ -499,11 +515,14 @@ year=2022
#[ test ]
#[ test ]
fn test_extract_template_links_with_newlines_malformed ( ) {
fn test_extract_template_links_with_newlines_malformed ( ) {
let s = " { { #template test . rs
let s = [
lang = rust
"{{#template test.rs \n" ,
year = 2022 } } " ;
" lang=rust\n" ,
" year=2022}}" ,
]
. concat ( ) ;
let res = extract_template_links ( s ) . collect ::< Vec < _ > > ( ) ;
let res = extract_template_links ( & s ) . collect ::< Vec < _ > > ( ) ;
assert_eq! (
assert_eq! (
res ,
res ,