pub fn from_line<'a>(
        line: &'a str,
        output: &'a Option<HashMap<String, String>>,
        separator: &str,
    ) -> Result<HashMap<NumOrText<'a>, NumOrText<'a>>, WnError>
    {
        // split line into parts.
        let separator = separator.chars().nth(0).unwrap_or('\0');
        let fields = if separator == '\0' {
            line.split_whitespace().collect::<Vec<_>>()
        } else {
            line.split(separator).collect::<Vec<_>>()
        };

        // no output mapping, return hashmap keyed by the index number, starting at 1.
        // { 1 => "username", 2 => "passwd", 3 => uid, ... }
        if output.is_none() {
            let res = fields
                .into_iter()
                .enumerate()
                .map(|(num, val)| (NumOrText::Number((num + 1) as i64), NumOrText::parse(val)))
                .collect::<HashMap<_, _>>();
            return Ok(res);
        }

        // apply output mapping.
        lazy_static! {
            // matches { (index) (:modifier) }
            // modifier is ignored for now
            static ref RE: Regex = Regex::new(r"^\{([0-9]+)(:[a-z])?\}$").unwrap();
        }

        let mut hm = HashMap::new();

        // apply output format. result goes into "hm".
        for (k, v) in output.as_ref().unwrap().iter() {
            // interpolate 'v'. so replace {1}, {2} etc with the corresponding field.
            let mut nv = v.as_str();
            if let Some(caps) = RE.captures(v) {
                if let Ok(n) = caps[1].parse::<usize>() {
                    if n > 0 && n <= fields.len() {
                        nv = fields[n - 1];
                    }
                }
            }
            // and insert into output hashmap.
            hm.insert(NumOrText::Text(k), NumOrText::parse(nv));
        }
        Ok(hm)
    }
// This could be a free-standing function. It's defined as Fields::from_line() only
// to have parity with the other from_line methods.
This could be a free-standing function. It's defined as Fields::from_line() only
to have parity with the other from_line methods.
This could be a free - standing function . It ' s defined as Fields :: from_line () only to have parity with the other from_line methods .
pub fn from_line<'a>(
        line: &'a str,
        output: &'a Option<HashMap<String, String>>,
        separator: &str,
    ) -> Result<HashMap<NumOrText<'a>, NumOrText<'a>>, WnError>
    {
        // split line into parts.
        let separator = separator.chars().nth(0).unwrap_or('\0');
        let fields = if separator == '\0' {
            line.split_whitespace().collect::<Vec<_>>()
        } else {
            line.split(separator).collect::<Vec<_>>()
        };
        // no output mapping, return hashmap keyed by the index number, starting at 1.
        // { 1 => "username", 2 => "passwd", 3 => uid, ... }
        if output.is_none() {
            let res = fields
                .into_iter()
                .enumerate()
                .map(|(num, val)| (NumOrText::Number((num + 1) as i64), NumOrText::parse(val)))
                .collect::<HashMap<_, _>>();
            return Ok(res);
        }
        // apply output mapping.
        lazy_static! {
            // matches { (index) (:modifier) }
            // modifier is ignored for now
            static ref RE: Regex = Regex::new(r"^\{([0-9]+)(:[a-z])?\}$").unwrap();
        }
        let mut hm = HashMap::new();
        // apply output format. result goes into "hm".
        for (k, v) in output.as_ref().unwrap().iter() {
            // interpolate 'v'. so replace {1}, {2} etc with the corresponding field.
            let mut nv = v.as_str();
            if let Some(caps) = RE.captures(v) {
                if let Ok(n) = caps[1].parse::<usize>() {
                    if n > 0 && n <= fields.len() {
                        nv = fields[n - 1];
                    }
                }
            }
            // and insert into output hashmap.
            hm.insert(NumOrText::Text(k), NumOrText::parse(nv));
        }
        Ok(hm)
    }
pub fn from_line < ' a > ( line : & ' a str , output : & ' a Option < HashMap < String , String > > , separator : & str , ) -> Result < HashMap < NumOrText < ' a > , NumOrText < ' a > > , WnError > { // split line into parts. let separator = separator . chars ( ) . nth ( 0 ) . unwrap_or ( '\0' ) ; let fields = if separator == '\0' { line . split_whitespace ( ) . collect :: < Vec < _ > > ( ) } else { line . split ( separator ) . collect :: < Vec < _ > > ( ) } ; // no output mapping, return hashmap keyed by the index number, starting at 1. // { 1 => "username", 2 => "passwd", 3 => uid, ... } if output . is_none ( ) { let res = fields . into_iter ( ) . enumerate ( ) . map ( | ( num , val ) | ( NumOrText :: Number ( ( num + 1 ) as i64 ) , NumOrText :: parse ( val ) ) ) . collect :: < HashMap < _ , _ > > ( ) ; return Ok ( res ) ; } // apply output mapping. lazy_static ! { // matches { (index) (:modifier) } // modifier is ignored for now static ref RE Regex Regex new ( r"^\{([0-9]+)(:[a-z])?\}$" ) unwrap ( ) } let mut hm = HashMap :: new ( ) ; // apply output format. result goes into "hm". for ( k , v ) in output . as_ref ( ) . unwrap ( ) . iter ( ) { // interpolate 'v'. so replace {1}, {2} etc with the corresponding field. let mut nv = v . as_str ( ) ; if let Some ( caps ) = RE . captures ( v ) { if let Ok ( n ) = caps [ 1 ] . parse :: < usize > ( ) { if n > 0 && n <= fields . len ( ) { nv = fields [ n - 1 ] ; } } } // and insert into output hashmap. hm . insert ( NumOrText :: Text ( k ) , NumOrText :: parse ( nv ) ) ; } Ok ( hm ) }
This could be a free-standing function.
This could be a free - standing function .
line
&'a str
output
&'a Option<HashMap<String, String>>
separator
&str