pub fn new(raw: &'a str) -> Result<Self, ParserError> {
        let parsed_file = ProtocolParser::parse(Rule::file, &raw)?
            .next() // there is exactly one { file }
            .expect("Unreachable file rule");

        let mut err_code_rows = vec![];
        let mut api_key_rows = vec![];
        let mut req_resp_specs = IndexMap::new();

        for target in parsed_file.into_inner() {
            match target.as_rule() {
                // Parses the error codes table into a templatable enum motif
                Rule::error_codes => {
                    err_code_rows = target
                        .into_inner() // inner { table }
                        .next() // there is exactly one { table }
                        .expect("Unreachable error_codes table rule")
                        .into_inner() // inner { tr }
                        .into_iter()
                        .map(|tr| {
                            let row = tr
                                .into_inner() // inner { td }
                                .into_iter()
                                .map(|td| td.into_inner().as_str()) // inner { content }
                                .collect::<Vec<_>>();
                            (
                                // Enum variant name of the error code
                                String::from(row[0]).to_camel_case(),
                                // Enum value (i16) of the error code
                                String::from(row[1]),
                                // Error's description
                                capped_comment(
                                    &format!("{} Retriable: {}.", row[3], yes_no(row[2])),
                                    4,
                                ),
                            )
                        })
                        .collect::<Vec<_>>();
                }

                // Parses the API keys table into a templatable enum motif
                Rule::api_keys => {
                    api_key_rows = target
                        .into_inner() // inner { table }
                        .next() // there is exactly one { table }
                        .expect("Unreachable api_keys table rule")
                        .into_inner() // inner { tr }
                        .into_iter()
                        .map(|tr| {
                            let row = tr
                                .into_inner() // inner { td }
                                .into_iter()
                                .map(|td| {
                                    td.into_inner() // inner { a }
                                        .next() // there is exactly one { a }
                                        .expect("Unreachable api_keys a rule")
                                        .into_inner() // inner { content }
                                        .as_str()
                                })
                                .collect::<Vec<_>>();
                            (
                                // Enum variant name of the API key
                                String::from(row[0]),
                                // Enum value (i16) of the API key
                                String::from(row[1]),
                            )
                        })
                        .collect::<Vec<_>>();
                }

                // Parses all req/resp BNF definitions and description tables
                // into templatable versioned enum/mod/strut motifs
                Rule::req_resp => {
                    let parsed_spec = ProtocolParser::parse(Rule::spec, target.as_str())?
                        .next() // there is exactly one { spec }
                        .expect("Unreachable spec rule");

                    let mut curr_name = None;
                    let mut curr_version = None;
                    let mut curr_spec = None;

                    for section in parsed_spec.into_inner() {
                        match section.as_rule() {
                            Rule::content => {
                                let (name, version, spec) = parse_struct_spec(section.as_str())?;
                                curr_name = Some(name);
                                curr_version = Some(version);
                                curr_spec = Some(spec);
                            }

                            Rule::table => {
                                let fields_doc = section
                                    .into_inner() // inner { td }
                                    .map(|tr| {
                                        let row = tr
                                            .into_inner() // inner { td }
                                            .into_iter()
                                            .map(|td| td.into_inner().as_str()) // inner { content }
                                            .collect::<Vec<_>>();
                                        (clean_name(row[0]), String::from(row[1]))
                                    })
                                    .collect::<HashMap<_, _>>();

                                let name = curr_name.take().expect("unreachable no name parsed");
                                let version = (
                                    curr_version.take().expect("unreachable no version parsed"),
                                    curr_spec.take().expect("unreachable no spec parsed"),
                                    fields_doc,
                                );

                                match req_resp_specs.get_mut(&name) {
                                    None => {
                                        req_resp_specs.insert(name, vec![version]);
                                    }
                                    Some(versions) => {
                                        versions.push(version);
                                    }
                                };
                            }

                            _ => unreachable!("No other rules"),
                        }
                    }
                }

                _ => (),
            }
        }

        Ok(SpecParser {
            err_code_rows,
            api_key_rows,
            req_resp_specs,
        })
    }
/// Parses raw file content and return an initialized SpecParser.
Parses raw file content and return an initialized SpecParser.
Parses raw file content and return an initialized SpecParser .
pub fn new(raw: &'a str) -> Result<Self, ParserError> {
        let parsed_file = ProtocolParser::parse(Rule::file, &raw)?
            .next() // there is exactly one { file }
            .expect("Unreachable file rule");
        let mut err_code_rows = vec![];
        let mut api_key_rows = vec![];
        let mut req_resp_specs = IndexMap::new();
        for target in parsed_file.into_inner() {
            match target.as_rule() {
                // Parses the error codes table into a templatable enum motif
                Rule::error_codes => {
                    err_code_rows = target
                        .into_inner() // inner { table }
                        .next() // there is exactly one { table }
                        .expect("Unreachable error_codes table rule")
                        .into_inner() // inner { tr }
                        .into_iter()
                        .map(|tr| {
                            let row = tr
                                .into_inner() // inner { td }
                                .into_iter()
                                .map(|td| td.into_inner().as_str()) // inner { content }
                                .collect::<Vec<_>>();
                            (
                                // Enum variant name of the error code
                                String::from(row[0]).to_camel_case(),
                                // Enum value (i16) of the error code
                                String::from(row[1]),
                                // Error's description
                                capped_comment(
                                    &format!("{} Retriable: {}.", row[3], yes_no(row[2])),
                                    4,
                                ),
                            )
                        })
                        .collect::<Vec<_>>();
                }
                // Parses the API keys table into a templatable enum motif
                Rule::api_keys => {
                    api_key_rows = target
                        .into_inner() // inner { table }
                        .next() // there is exactly one { table }
                        .expect("Unreachable api_keys table rule")
                        .into_inner() // inner { tr }
                        .into_iter()
                        .map(|tr| {
                            let row = tr
                                .into_inner() // inner { td }
                                .into_iter()
                                .map(|td| {
                                    td.into_inner() // inner { a }
                                        .next() // there is exactly one { a }
                                        .expect("Unreachable api_keys a rule")
                                        .into_inner() // inner { content }
                                        .as_str()
                                })
                                .collect::<Vec<_>>();
                            (
                                // Enum variant name of the API key
                                String::from(row[0]),
                                // Enum value (i16) of the API key
                                String::from(row[1]),
                            )
                        })
                        .collect::<Vec<_>>();
                }
                // Parses all req/resp BNF definitions and description tables
                // into templatable versioned enum/mod/strut motifs
                Rule::req_resp => {
                    let parsed_spec = ProtocolParser::parse(Rule::spec, target.as_str())?
                        .next() // there is exactly one { spec }
                        .expect("Unreachable spec rule");
                    let mut curr_name = None;
                    let mut curr_version = None;
                    let mut curr_spec = None;
                    for section in parsed_spec.into_inner() {
                        match section.as_rule() {
                            Rule::content => {
                                let (name, version, spec) = parse_struct_spec(section.as_str())?;
                                curr_name = Some(name);
                                curr_version = Some(version);
                                curr_spec = Some(spec);
                            }
                            Rule::table => {
                                let fields_doc = section
                                    .into_inner() // inner { td }
                                    .map(|tr| {
                                        let row = tr
                                            .into_inner() // inner { td }
                                            .into_iter()
                                            .map(|td| td.into_inner().as_str()) // inner { content }
                                            .collect::<Vec<_>>();
                                        (clean_name(row[0]), String::from(row[1]))
                                    })
                                    .collect::<HashMap<_, _>>();
                                let name = curr_name.take().expect("unreachable no name parsed");
                                let version = (
                                    curr_version.take().expect("unreachable no version parsed"),
                                    curr_spec.take().expect("unreachable no spec parsed"),
                                    fields_doc,
                                );
                                match req_resp_specs.get_mut(&name) {
                                    None => {
                                        req_resp_specs.insert(name, vec![version]);
                                    }
                                    Some(versions) => {
                                        versions.push(version);
                                    }
                                };
                            }
                            _ => unreachable!("No other rules"),
                        }
                    }
                }
                _ => (),
            }
        }
        Ok(SpecParser {
            err_code_rows,
            api_key_rows,
            req_resp_specs,
        })
    }
pub fn new ( raw : & ' a str ) -> Result < Self , ParserError > { let parsed_file = ProtocolParser :: parse ( Rule :: file , & raw ) ? . next ( ) // there is exactly one { file } . expect ( " " ) ; let mut err_code_rows = vec ! [ ] ; let mut api_key_rows = vec ! [ ] ; let mut req_resp_specs = IndexMap :: new ( ) ; for target in parsed_file . into_inner ( ) { match target . as_rule ( ) { // Parses the error codes table into a templatable enum motif Rule :: error_codes => { err_code_rows = target . into_inner ( ) // inner { table } . next ( ) // there is exactly one { table } . expect ( " " ) . into_inner ( ) // inner { tr } . into_iter ( ) . map ( | tr | { let row = tr . into_inner ( ) // inner { td } . into_iter ( ) . map ( | td | td . into_inner ( ) . as_str ( ) ) // inner { content } . collect :: < Vec < _ > > ( ) ; ( // Enum variant name of the error code String :: from ( row [ 0 ] ) . to_camel_case ( ) , // Enum value (i16) of the error code String :: from ( row [ 1 ] ) , // Error's description capped_comment ( & format ! ( " " row [ 3 ] yes_no ( row [ 2 ] ) ) , 4 , ) , ) } ) . collect :: < Vec < _ > > ( ) ; } // Parses the API keys table into a templatable enum motif Rule :: api_keys => { api_key_rows = target . into_inner ( ) // inner { table } . next ( ) // there is exactly one { table } . expect ( " " ) . into_inner ( ) // inner { tr } . into_iter ( ) . map ( | tr | { let row = tr . into_inner ( ) // inner { td } . into_iter ( ) . map ( | td | { td . into_inner ( ) // inner { a } . next ( ) // there is exactly one { a } . expect ( " " ) . into_inner ( ) // inner { content } . as_str ( ) } ) . collect :: < Vec < _ > > ( ) ; ( // Enum variant name of the API key String :: from ( row [ 0 ] ) , // Enum value (i16) of the API key String :: from ( row [ 1 ] ) , ) } ) . collect :: < Vec < _ > > ( ) ; } // Parses all req/resp BNF definitions and description tables // into templatable versioned enum/mod/strut motifs Rule :: req_resp => { let parsed_spec = ProtocolParser :: parse ( Rule :: spec , target . as_str ( ) ) ? . next ( ) // there is exactly one { spec } . expect ( " " ) ; let mut curr_name = None ; let mut curr_version = None ; let mut curr_spec = None ; for section in parsed_spec . into_inner ( ) { match section . as_rule ( ) { Rule :: content => { let ( name , version , spec ) = parse_struct_spec ( section . as_str ( ) ) ? ; curr_name = Some ( name ) ; curr_version = Some ( version ) ; curr_spec = Some ( spec ) ; } Rule :: table => { let fields_doc = section . into_inner ( ) // inner { td } . map ( | tr | { let row = tr . into_inner ( ) // inner { td } . into_iter ( ) . map ( | td | td . into_inner ( ) . as_str ( ) ) // inner { content } . collect :: < Vec < _ > > ( ) ; ( clean_name ( row [ 0 ] ) , String :: from ( row [ 1 ] ) ) } ) . collect :: < HashMap < _ , _ > > ( ) ; let name = curr_name . take ( ) . expect ( " " ) ; let version = ( curr_version . take ( ) . expect ( " " ) , curr_spec . take ( ) . expect ( " " ) , fields_doc , ) ; match req_resp_specs . get_mut ( & name ) { None => { req_resp_specs . insert ( name , vec ! [ version ] ) ; } Some ( versions ) => { versions . push ( version ) ; } } ; } _ => unreachable ! ( " " ) , } } } _ => ( ) , } } Ok ( SpecParser { err_code_rows , api_key_rows , req_resp_specs , } ) }
Parses raw file content and return an initialized SpecParser.
Parses raw file content and return an initialized SpecParser .
raw
&'a str