/// Extract page dimensions from DOCX data.
Extract page dimensions from DOCX data.
Extract page dimensions from DOCX data .
pub(crate) fn parse_page_dimensions(
    document_xml: &str,
) -> Result<PageDimensions, TextkitDocxError> {
    let mut height_opt: Option<i32> = None;
    let mut width_opt: Option<i32> = None;
    let mut m_top_opt: Option<i32> = None;
    let mut m_bottom_opt: Option<i32> = None;
    let mut m_right_opt: Option<i32> = None;
    let mut m_left_opt: Option<i32> = None;
    let mut header_opt: Option<i32> = None;
    let mut footer_opt: Option<i32> = None;
    let mut gutter_opt: Option<i32> = None;
    let source_buf = BufReader::new(document_xml.as_bytes());
    let parser = EventReader::new(source_buf);
    let ns = Some(String::from(NS_WP_ML));
    let fetch_attr_value = |attrs: &[xml::attribute::OwnedAttribute], name: &str| {
        for attr in attrs.iter() {
            if attr.name.local_name == name && attr.name.namespace == ns {
                return Some(attr.value.parse::<i32>().unwrap());
            }
        }
        None
    };
    for event in parser {
        match &event {
            Ok(xml::reader::XmlEvent::StartElement {
                name, attributes, ..
            }) => {
                // Fetch page size
                if name.local_name == "pgSz" && name.namespace == ns {
                    width_opt = fetch_attr_value(attributes, "w");
                    height_opt = fetch_attr_value(attributes, "h");
                }
                // Fetch page margins
                if name.local_name == "pgMar" && name.namespace == ns {
                    m_top_opt = fetch_attr_value(attributes, "top");
                    m_bottom_opt = fetch_attr_value(attributes, "bottom");
                    m_right_opt = fetch_attr_value(attributes, "right");
                    m_left_opt = fetch_attr_value(attributes, "left");
                    header_opt = fetch_attr_value(attributes, "header");
                    footer_opt = fetch_attr_value(attributes, "footer");
                    gutter_opt = fetch_attr_value(attributes, "gutter");
                }
            }
            _ => (),
        }
    }
    if height_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page height attribute not found".into(),
        ))
    } else if width_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page width attribute not found".into(),
        ))
    } else if m_top_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page top margin attribute not found".into(),
        ))
    } else if m_bottom_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page bottom margin attribute not found".into(),
        ))
    } else if m_right_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page right margin attribute not found".into(),
        ))
    } else if m_left_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page left margin attribute not found".into(),
        ))
    } else if header_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page header margin attribute not found".into(),
        ))
    } else if footer_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page footer margin attribute not found".into(),
        ))
    } else if gutter_opt == None {
        Err(TextkitDocxError::Malformed(
            "Page gutter attribute not found".into(),
        ))
    } else {
        Ok(PageDimensions {
            height: height_opt.unwrap(),
            width: width_opt.unwrap(),
            m_top: m_top_opt.unwrap(),
            m_bottom: m_bottom_opt.unwrap(),
            m_right: m_right_opt.unwrap(),
            m_left: m_left_opt.unwrap(),
            header: header_opt.unwrap(),
            footer: footer_opt.unwrap(),
            gutter: gutter_opt.unwrap(),
        })
    }
}
pub ( crate ) fn parse_page_dimensions ( document_xml : & str , ) -> Result < PageDimensions , TextkitDocxError > { let mut height_opt : Option < i32 > = None ; let mut width_opt : Option < i32 > = None ; let mut m_top_opt : Option < i32 > = None ; let mut m_bottom_opt : Option < i32 > = None ; let mut m_right_opt : Option < i32 > = None ; let mut m_left_opt : Option < i32 > = None ; let mut header_opt : Option < i32 > = None ; let mut footer_opt : Option < i32 > = None ; let mut gutter_opt : Option < i32 > = None ; let source_buf = BufReader :: new ( document_xml . as_bytes ( ) ) ; let parser = EventReader :: new ( source_buf ) ; let ns = Some ( String :: from ( NS_WP_ML ) ) ; let fetch_attr_value = | attrs : & [ xml :: attribute :: OwnedAttribute ] , name : & str | { for attr in attrs . iter ( ) { if attr . name . local_name == name && attr . name . namespace == ns { return Some ( attr . value . parse :: < i32 > ( ) . unwrap ( ) ) ; } } None } ; for event in parser { match & event { Ok ( xml :: reader :: XmlEvent :: StartElement { name , attributes , .. } ) => { // Fetch page size if name . local_name == " " && name . namespace == ns { width_opt = fetch_attr_value ( attributes , " " ) ; height_opt = fetch_attr_value ( attributes , " " ) ; } // Fetch page margins if name . local_name == " " && name . namespace == ns { m_top_opt = fetch_attr_value ( attributes , " " ) ; m_bottom_opt = fetch_attr_value ( attributes , " " ) ; m_right_opt = fetch_attr_value ( attributes , " " ) ; m_left_opt = fetch_attr_value ( attributes , " " ) ; header_opt = fetch_attr_value ( attributes , " " ) ; footer_opt = fetch_attr_value ( attributes , " " ) ; gutter_opt = fetch_attr_value ( attributes , " " ) ; } } _ => ( ) , } } if height_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if width_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if m_top_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if m_bottom_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if m_right_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if m_left_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if header_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if footer_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else if gutter_opt == None { Err ( TextkitDocxError :: Malformed ( " " . into ( ) , ) ) } else { Ok ( PageDimensions { height : height_opt . unwrap ( ) , width : width_opt . unwrap ( ) , m_top : m_top_opt . unwrap ( ) , m_bottom : m_bottom_opt . unwrap ( ) , m_right : m_right_opt . unwrap ( ) , m_left : m_left_opt . unwrap ( ) , header : header_opt . unwrap ( ) , footer : footer_opt . unwrap ( ) , gutter : gutter_opt . unwrap ( ) , } ) } }
Extract page dimensions from DOCX data.
Extract page dimensions from DOCX data .
document_xml
&str