pub fn parse(
        &mut self,
        dataset_id: &str,
        dataset_path: &str,
        landing_page_proposal: &str,
        provider_name: &str,
        xml_bytes: &[u8],
    ) -> Result<AbcdResult, Error> {
        let mut xml_reader = Reader::from_reader(xml_bytes);
        xml_reader.trim_text(true);

        let mut dataset_data = None;
        let mut units = Vec::new();

        loop {
            match xml_reader.read_event(&mut self.xml_buffer) {
                Ok(Event::Start(ref e)) => {
                    self.xml_tag_path.push(b'/');
                    self.xml_tag_path.extend(Self::strip_tag(e.name()));

                    //                    debug!("XML START: {}", String::from_utf8_lossy(&self.xml_tag_path));

                    match self.xml_tag_path.as_slice() {
                        b"/DataSets" => {
                            for attribute in e.attributes().filter_map(Result::ok) {
                                match attribute.value.as_ref() {
                                    b"http://www.tdwg.org/schemas/abcd/2.06" => {
                                        self.abcd_version = AbcdVersion::Version206;
                                        break;
                                    }
                                    b"http://www.tdwg.org/schemas/abcd/2.1" => {
                                        self.abcd_version = AbcdVersion::Version210;
                                        break;
                                    }
                                    _ => {}
                                }
                            }

                            //                            dbg!(&abcd_version);
                        }
                        b"/DataSets/DataSet/Units" => {
                            //                            eprintln!("Dataset Metadata:");
                            //                            dbg!(&numeric_values);
                            //                            dbg!(&textual_values);
                            //                            dbg!(units);

                            dataset_data = Some(self.finish_map())
                        }
                        _ => {} // ignore other start tags
                    }
                }
                Ok(Event::End(ref e)) => {
                    const SEPARATOR_LENGTH: usize = 1;

                    let tag: Vec<u8> = Self::strip_tag(e.name()).cloned().collect();
                    let stripped_name_length = tag.len();

                    self.xml_tag_path.truncate(
                        self.xml_tag_path.len() - stripped_name_length - SEPARATOR_LENGTH,
                    );

                    if self.xml_tag_path == b"/DataSets/DataSet/Units" && tag == b"Unit" {
                        //                        eprintln!("Unit Data:");
                        //                        dbg!(&numeric_values);
                        //                        dbg!(&textual_values);

                        units.push(self.finish_map());
                    }
                }
                Ok(Event::Text(ref e)) => {
                    if let Some(abcd_field) = self.abcd_fields.value_of(&self.xml_tag_path) {
                        if abcd_field.numeric {
                            let string = String::from_utf8_lossy(e.escaped());
                            if let Ok(number) = string.parse::<f64>() {
                                self.values.insert(abcd_field.name.clone(), number.into());
                            }
                        } else {
                            self.values.insert(
                                abcd_field.name.clone(),
                                String::from_utf8_lossy(e.escaped()).into(),
                            );
                        }
                    }
                }
                Ok(Event::Eof) => break, // exits the loop when reaching end of file
                Err(e) => panic!(
                    "Error at position {}: {:?}",
                    xml_reader.buffer_position(),
                    e
                ),
                _ => (), // ignore all other events
            }

            self.xml_buffer.clear();
        }

        self.clear(); // clear resources like buffers

        if let Some(dataset_data) = dataset_data {
            let landing_page = if let Some(VatType::Textual(value)) =
                dataset_data.get(&self.abcd_settings.landing_page_field)
            {
                value
            } else {
                landing_page_proposal
            };

            Ok(AbcdResult::new(
                dataset_id.into(),
                dataset_path.into(),
                landing_page.into(),
                provider_name.into(),
                dataset_data,
                units,
            ))
        } else {
            Err(AbcdContainsNoDatasetMetadata {}.into())
        }
    }
/// Parse a binary XML file to `AbcdResult`s.
Parse a binary XML file to `AbcdResult`s.
Parse a binary XML file to ` AbcdResult ` s .
pub fn parse(
        &mut self,
        dataset_id: &str,
        dataset_path: &str,
        landing_page_proposal: &str,
        provider_name: &str,
        xml_bytes: &[u8],
    ) -> Result<AbcdResult, Error> {
        let mut xml_reader = Reader::from_reader(xml_bytes);
        xml_reader.trim_text(true);
        let mut dataset_data = None;
        let mut units = Vec::new();
        loop {
            match xml_reader.read_event(&mut self.xml_buffer) {
                Ok(Event::Start(ref e)) => {
                    self.xml_tag_path.push(b'/');
                    self.xml_tag_path.extend(Self::strip_tag(e.name()));
                    //                    debug!("XML START: {}", String::from_utf8_lossy(&self.xml_tag_path));
                    match self.xml_tag_path.as_slice() {
                        b"/DataSets" => {
                            for attribute in e.attributes().filter_map(Result::ok) {
                                match attribute.value.as_ref() {
                                    b"http://www.tdwg.org/schemas/abcd/2.06" => {
                                        self.abcd_version = AbcdVersion::Version206;
                                        break;
                                    }
                                    b"http://www.tdwg.org/schemas/abcd/2.1" => {
                                        self.abcd_version = AbcdVersion::Version210;
                                        break;
                                    }
                                    _ => {}
                                }
                            }
                            //                            dbg!(&abcd_version);
                        }
                        b"/DataSets/DataSet/Units" => {
                            //                            eprintln!("Dataset Metadata:");
                            //                            dbg!(&numeric_values);
                            //                            dbg!(&textual_values);
                            //                            dbg!(units);
                            dataset_data = Some(self.finish_map())
                        }
                        _ => {} // ignore other start tags
                    }
                }
                Ok(Event::End(ref e)) => {
                    const SEPARATOR_LENGTH: usize = 1;
                    let tag: Vec<u8> = Self::strip_tag(e.name()).cloned().collect();
                    let stripped_name_length = tag.len();
                    self.xml_tag_path.truncate(
                        self.xml_tag_path.len() - stripped_name_length - SEPARATOR_LENGTH,
                    );
                    if self.xml_tag_path == b"/DataSets/DataSet/Units" && tag == b"Unit" {
                        //                        eprintln!("Unit Data:");
                        //                        dbg!(&numeric_values);
                        //                        dbg!(&textual_values);
                        units.push(self.finish_map());
                    }
                }
                Ok(Event::Text(ref e)) => {
                    if let Some(abcd_field) = self.abcd_fields.value_of(&self.xml_tag_path) {
                        if abcd_field.numeric {
                            let string = String::from_utf8_lossy(e.escaped());
                            if let Ok(number) = string.parse::<f64>() {
                                self.values.insert(abcd_field.name.clone(), number.into());
                            }
                        } else {
                            self.values.insert(
                                abcd_field.name.clone(),
                                String::from_utf8_lossy(e.escaped()).into(),
                            );
                        }
                    }
                }
                Ok(Event::Eof) => break, // exits the loop when reaching end of file
                Err(e) => panic!(
                    "Error at position {}: {:?}",
                    xml_reader.buffer_position(),
                    e
                ),
                _ => (), // ignore all other events
            }
            self.xml_buffer.clear();
        }
        self.clear(); // clear resources like buffers
        if let Some(dataset_data) = dataset_data {
            let landing_page = if let Some(VatType::Textual(value)) =
                dataset_data.get(&self.abcd_settings.landing_page_field)
            {
                value
            } else {
                landing_page_proposal
            };
            Ok(AbcdResult::new(
                dataset_id.into(),
                dataset_path.into(),
                landing_page.into(),
                provider_name.into(),
                dataset_data,
                units,
            ))
        } else {
            Err(AbcdContainsNoDatasetMetadata {}.into())
        }
    }
pub fn parse ( & mut self , dataset_id : & str , dataset_path : & str , landing_page_proposal : & str , provider_name : & str , xml_bytes : & [ u8 ] , ) -> Result < AbcdResult , Error > { let mut xml_reader = Reader :: from_reader ( xml_bytes ) ; xml_reader . trim_text ( true ) ; let mut dataset_data = None ; let mut units = Vec :: new ( ) ; loop { match xml_reader . read_event ( & mut self . xml_buffer ) { Ok ( Event :: Start ( ref e ) ) => { self . xml_tag_path . push ( b'/' ) ; self . xml_tag_path . extend ( Self :: strip_tag ( e . name ( ) ) ) ; //                    debug!("XML START: {}", String::from_utf8_lossy(&self.xml_tag_path)); match self . xml_tag_path . as_slice ( ) { b" " => { for attribute in e . attributes ( ) . filter_map ( Result :: ok ) { match attribute . value . as_ref ( ) { b" " => { self . abcd_version = AbcdVersion :: Version206 ; break ; } b" " => { self . abcd_version = AbcdVersion :: Version210 ; break ; } _ => { } } } //                            dbg!(&abcd_version); } b" " => { //                            eprintln!("Dataset Metadata:"); //                            dbg!(&numeric_values); //                            dbg!(&textual_values); //                            dbg!(units); dataset_data = Some ( self . finish_map ( ) ) } _ => { } // ignore other start tags } } Ok ( Event :: End ( ref e ) ) => { const SEPARATOR_LENGTH : usize = 1 ; let tag : Vec < u8 > = Self :: strip_tag ( e . name ( ) ) . cloned ( ) . collect ( ) ; let stripped_name_length = tag . len ( ) ; self . xml_tag_path . truncate ( self . xml_tag_path . len ( ) - stripped_name_length - SEPARATOR_LENGTH , ) ; if self . xml_tag_path == b" " && tag == b" " { //                        eprintln!("Unit Data:"); //                        dbg!(&numeric_values); //                        dbg!(&textual_values); units . push ( self . finish_map ( ) ) ; } } Ok ( Event :: Text ( ref e ) ) => { if let Some ( abcd_field ) = self . abcd_fields . value_of ( & self . xml_tag_path ) { if abcd_field . numeric { let string = String :: from_utf8_lossy ( e . escaped ( ) ) ; if let Ok ( number ) = string . parse :: < f64 > ( ) { self . values . insert ( abcd_field . name . clone ( ) , number . into ( ) ) ; } } else { self . values . insert ( abcd_field . name . clone ( ) , String :: from_utf8_lossy ( e . escaped ( ) ) . into ( ) , ) ; } } } Ok ( Event :: Eof ) => break , // exits the loop when reaching end of file Err ( e ) => panic ! ( " " xml_reader buffer_position ( ) e ) , _ => ( ) , // ignore all other events } self . xml_buffer . clear ( ) ; } self . clear ( ) ; // clear resources like buffers if let Some ( dataset_data ) = dataset_data { let landing_page = if let Some ( VatType :: Textual ( value ) ) = dataset_data . get ( & self . abcd_settings . landing_page_field ) { value } else { landing_page_proposal } ; Ok ( AbcdResult :: new ( dataset_id . into ( ) , dataset_path . into ( ) , landing_page . into ( ) , provider_name . into ( ) , dataset_data , units , ) ) } else { Err ( AbcdContainsNoDatasetMetadata { } . into ( ) ) } }
Parse a binary XML file to `AbcdResult`s.
Parse a binary XML file to ` AbcdResult ` s .
&mut self
dataset_id
&str
dataset_path
&str
landing_page_proposal
&str
provider_name
&str
xml_bytes
&[u8]
&mut self
dataset_id
&str
dataset_path
&str
landing_page_proposal
&str
provider_name
&str
xml_bytes
&[u8]