pub fn parse_pattern<T: FromStr>(
    filename: String,
    pattern_key: String,
    section_key: &str,
) -> Option<Vec<T>> {
    let dump_map = match std::fs::read_to_string(filename) {
        Ok(belowrc_str) => match belowrc_str.parse::<TValue>() {
            Ok(belowrc_val) => belowrc_val
                .get(get_belowrc_dump_section_key())
                .unwrap_or_else(|| {
                    panic!(
                        "Failed to get section key: [{}.{}]",
                        get_belowrc_dump_section_key(),
                        section_key
                    )
                })
                .to_owned(),
            Err(e) => panic!("Failed to parse belowrc file: {:#}", e),
        },
        Err(e) => {
            // Backward compatibility code.
            // TODO(boyuni) Remove this section of code at May 1st.
            if let Ok(dumprc_str) = std::fs::read_to_string(format!(
                "{}{}",
                std::env::var("HOME").expect("Fail to obtain HOME env var"),
                BELOW_DUMP_RC
            )) {
                eprintln!(
                    "[WARNING] dumprc file has been deprecated and the relative support \
                    will be removed on 05/01/2021. Please use belowrc instead."
                );
                match dumprc_str.parse::<TValue>() {
                    Ok(dumprc_val) => dumprc_val,
                    Err(e) => panic!("Failed to parse dumprc file: {:#}", e),
                }
            } else {
                panic!("Failed to parse belowrc or dumprc file: {:#}", e)
            }
        }
    };

    Some(
        dump_map
            .get(section_key)
            .unwrap_or_else(|| {
                panic!(
                    "Failed to get section key: [{}.{}]",
                    get_belowrc_dump_section_key(),
                    section_key
                )
            })
            .get(&pattern_key)
            .unwrap_or_else(|| panic!("Failed to get pattern key: {}", pattern_key))
            .as_array()
            .unwrap_or_else(|| panic!("Failed to parse pattern {} value to array.", pattern_key))
            .iter()
            .map(|field| {
                T::from_str(
                    field.as_str().unwrap_or_else(|| {
                        panic!("Failed to parse field key {} into string", field)
                    }),
                )
                .or_else(|_| Err(format!("Failed to parse field key: {}", field)))
                .unwrap()
            })
            .collect(),
    )
}
/// Try to read $HOME/.config/below/belowrc file and generate a list of keys which will
/// be used as fields. Any errors happen in this function will directly trigger a panic.
Try to read $HOME/.config/below/belowrc file and generate a list of keys which will
be used as fields. Any errors happen in this function will directly trigger a panic.
Try to read $HOME / . config / below / belowrc file and generate a list of keys which will be used as fields . Any errors happen in this function will directly trigger a panic .
pub fn parse_pattern<T: FromStr>(
    filename: String,
    pattern_key: String,
    section_key: &str,
) -> Option<Vec<T>> {
    let dump_map = match std::fs::read_to_string(filename) {
        Ok(belowrc_str) => match belowrc_str.parse::<TValue>() {
            Ok(belowrc_val) => belowrc_val
                .get(get_belowrc_dump_section_key())
                .unwrap_or_else(|| {
                    panic!(
                        "Failed to get section key: [{}.{}]",
                        get_belowrc_dump_section_key(),
                        section_key
                    )
                })
                .to_owned(),
            Err(e) => panic!("Failed to parse belowrc file: {:#}", e),
        },
        Err(e) => {
            // Backward compatibility code.
            // TODO(boyuni) Remove this section of code at May 1st.
            if let Ok(dumprc_str) = std::fs::read_to_string(format!(
                "{}{}",
                std::env::var("HOME").expect("Fail to obtain HOME env var"),
                BELOW_DUMP_RC
            )) {
                eprintln!(
                    "[WARNING] dumprc file has been deprecated and the relative support \
                    will be removed on 05/01/2021. Please use belowrc instead."
                );
                match dumprc_str.parse::<TValue>() {
                    Ok(dumprc_val) => dumprc_val,
                    Err(e) => panic!("Failed to parse dumprc file: {:#}", e),
                }
            } else {
                panic!("Failed to parse belowrc or dumprc file: {:#}", e)
            }
        }
    };
    Some(
        dump_map
            .get(section_key)
            .unwrap_or_else(|| {
                panic!(
                    "Failed to get section key: [{}.{}]",
                    get_belowrc_dump_section_key(),
                    section_key
                )
            })
            .get(&pattern_key)
            .unwrap_or_else(|| panic!("Failed to get pattern key: {}", pattern_key))
            .as_array()
            .unwrap_or_else(|| panic!("Failed to parse pattern {} value to array.", pattern_key))
            .iter()
            .map(|field| {
                T::from_str(
                    field.as_str().unwrap_or_else(|| {
                        panic!("Failed to parse field key {} into string", field)
                    }),
                )
                .or_else(|_| Err(format!("Failed to parse field key: {}", field)))
                .unwrap()
            })
            .collect(),
    )
}
pub fn parse_pattern < T : FromStr > ( filename : String , pattern_key : String , section_key : & str , ) -> Option < Vec < T > > { let dump_map = match std :: fs :: read_to_string ( filename ) { Ok ( belowrc_str ) => match belowrc_str . parse :: < TValue > ( ) { Ok ( belowrc_val ) => belowrc_val . get ( get_belowrc_dump_section_key ( ) ) . unwrap_or_else ( | | { panic ! ( " " get_belowrc_dump_section_key ( ) section_key ) } ) . to_owned ( ) , Err ( e ) => panic ! ( " " e ) , } , Err ( e ) => { // Backward compatibility code. // TODO(boyuni) Remove this section of code at May 1st. if let Ok ( dumprc_str ) = std :: fs :: read_to_string ( format ! ( " " std env var ( " " ) expect ( " " ) BELOW_DUMP_RC ) ) { eprintln ! ( " \
 " ) ; match dumprc_str . parse :: < TValue > ( ) { Ok ( dumprc_val ) => dumprc_val , Err ( e ) => panic ! ( " " e ) , } } else { panic ! ( " " e ) } } } ; Some ( dump_map . get ( section_key ) . unwrap_or_else ( | | { panic ! ( " " get_belowrc_dump_section_key ( ) section_key ) } ) . get ( & pattern_key ) . unwrap_or_else ( | | panic ! ( " " pattern_key ) ) . as_array ( ) . unwrap_or_else ( | | panic ! ( " " pattern_key ) ) . iter ( ) . map ( | field | { T :: from_str ( field . as_str ( ) . unwrap_or_else ( | | { panic ! ( " " field ) } ) , ) . or_else ( | _ | Err ( format ! ( " " field ) ) ) . unwrap ( ) } ) . collect ( ) , ) }
Try to read $HOME/.config/below/belowrc file and generate a list of keys which will
be used as fields.
Try to read $HOME / . config / below / belowrc file and generate a list of keys which will be used as fields .
filename
String
pattern_key
String
section_key
&str
filename
String
pattern_key
String
section_key
&str