pub fn parse_jdx(filepath : &str) -> Result<Spectrum, &str> {
    // Reads to string all at once
    let file : &str;
    let res = &read_file_to_string(filepath);
    match res {
        Err(_) => {
            return Err("Error reading file! Does this file exist?")
        }, 
        Ok(x) => {
            file = x;
        }
    }
    // If this block is run, the file is prpbably not a proper .jdx format
    if !is_next_char_x(file, '#') {
        println!("Error! {} is not a valid .jdx format!", filepath);
        return Err("invalid file")
    }

    // Parses the header to construct the initial Spectrum
    // The order of these statements is important
    let (mut file, mut spectrum) = {
        let (file, title) = get_field_or_default(file, "TITLE", "UNKNOWN TITLE");
        let (file, spectrum_type) = get_field_or_default(file, "DATA TYPE", "UNKNOWN TYPE");
        let (file, state) = get_field_or_default(file, "STATE", "UNKNOWN STATE");
        debug_println!("{}, {}, {}", title, spectrum_type, state);

        let (file, xunits_str) = get_field_or_default(file, "XUNITS", "1/CM");
        let mut xunits : Xunits = Xunits::cm;
        // Sets xunits to one of the possibilities in the Xunits enum
        if xunits_str == "MICROMETERS" {
            xunits = Xunits::um;
        } else {
            xunits = Xunits::cm;
        }

        let (file, yunits_str) = get_field_or_default(file, "YUNITS", "TRANSMITTACE");
        let mut yunits : Yunits = Yunits::Absorbance;
        if yunits_str == "ABSORBANCE" {
            yunits = Yunits::Absorbance;
        } else {
            yunits = Yunits::Transmittance;
        }


        let (file, y_factor) = get_field_or_default(file, "YFACTOR", "1");
        let y_factor : f32 = y_factor.parse::<f32>().unwrap_or(1.0);
        debug_println!("y_factor {}", y_factor);

        let (file, first_x) = parse_until_field(file, "FIRSTX").unwrap();
        let first_x : f32 = first_x.parse::<f32>().unwrap();
        debug_println!("first_x {}", first_x);

        let (file, last_x) = parse_until_field(file, "LASTX").unwrap();
        let last_x : f32 = last_x.parse::<f32>().unwrap();
        debug_println!("last_x {}", last_x);

        let (file, npoints) = parse_until_field(file, "NPOINTS").unwrap();
        let npoints : i32 = npoints.parse::<i32>().unwrap();
        debug_println!("npoints {}", npoints);

        let mut spec = Spectrum::new(title, spectrum_type, state, xunits, yunits, first_x, last_x, npoints);
        spec.set_y_factor(y_factor);

        (file, spec)
    };

    // Parses until the XYDATA
    file = parse_until_field(file, "XYDATA").unwrap().0;

    // Finds what delimiter the XYDATA line uses
    // The only possible delimiters are " " and "+"
    let mut delimiter = " \n";
    let not_numeric : &str = not_numeric(file).unwrap().0;
    if is_next_char_x(not_numeric, '+') {
        delimiter = "+\n";
    }
    // Parses until reaching the ##END
    loop {

        // If at the end of the data section
        if is_next_tag_x(file, "##END") {
            break
        }
        let res = parse_xydata_line(file, Some(delimiter));
        match res {
            Err(_) => {
                break
            },
            Ok(x) => {
                file = x.0;
                for val in x.1 {
                    spectrum.add_y(val.to_owned());
                }
            }
        }
    }

    // If the spectrum doesn't have all of the y values alloted for every x value, something has
    // probably gone wrong in the mol file
    assert!(spectrum.is_complete());

    Ok(spectrum)
}
// Feed this function a jdx filepath and get back a Spectrum struct
Feed this function a jdx filepath and get back a Spectrum struct
Feed this function a jdx filepath and get back a Spectrum struct
pub fn parse_jdx(filepath : &str) -> Result<Spectrum, &str> {
    // Reads to string all at once
    let file : &str;
    let res = &read_file_to_string(filepath);
    match res {
        Err(_) => {
            return Err("Error reading file! Does this file exist?")
        }, 
        Ok(x) => {
            file = x;
        }
    }
    // If this block is run, the file is prpbably not a proper .jdx format
    if !is_next_char_x(file, '#') {
        println!("Error! {} is not a valid .jdx format!", filepath);
        return Err("invalid file")
    }
    // Parses the header to construct the initial Spectrum
    // The order of these statements is important
    let (mut file, mut spectrum) = {
        let (file, title) = get_field_or_default(file, "TITLE", "UNKNOWN TITLE");
        let (file, spectrum_type) = get_field_or_default(file, "DATA TYPE", "UNKNOWN TYPE");
        let (file, state) = get_field_or_default(file, "STATE", "UNKNOWN STATE");
        debug_println!("{}, {}, {}", title, spectrum_type, state);
        let (file, xunits_str) = get_field_or_default(file, "XUNITS", "1/CM");
        let mut xunits : Xunits = Xunits::cm;
        // Sets xunits to one of the possibilities in the Xunits enum
        if xunits_str == "MICROMETERS" {
            xunits = Xunits::um;
        } else {
            xunits = Xunits::cm;
        }
        let (file, yunits_str) = get_field_or_default(file, "YUNITS", "TRANSMITTACE");
        let mut yunits : Yunits = Yunits::Absorbance;
        if yunits_str == "ABSORBANCE" {
            yunits = Yunits::Absorbance;
        } else {
            yunits = Yunits::Transmittance;
        }
        let (file, y_factor) = get_field_or_default(file, "YFACTOR", "1");
        let y_factor : f32 = y_factor.parse::<f32>().unwrap_or(1.0);
        debug_println!("y_factor {}", y_factor);
        let (file, first_x) = parse_until_field(file, "FIRSTX").unwrap();
        let first_x : f32 = first_x.parse::<f32>().unwrap();
        debug_println!("first_x {}", first_x);
        let (file, last_x) = parse_until_field(file, "LASTX").unwrap();
        let last_x : f32 = last_x.parse::<f32>().unwrap();
        debug_println!("last_x {}", last_x);
        let (file, npoints) = parse_until_field(file, "NPOINTS").unwrap();
        let npoints : i32 = npoints.parse::<i32>().unwrap();
        debug_println!("npoints {}", npoints);
        let mut spec = Spectrum::new(title, spectrum_type, state, xunits, yunits, first_x, last_x, npoints);
        spec.set_y_factor(y_factor);
        (file, spec)
    };
    // Parses until the XYDATA
    file = parse_until_field(file, "XYDATA").unwrap().0;
    // Finds what delimiter the XYDATA line uses
    // The only possible delimiters are " " and "+"
    let mut delimiter = " \n";
    let not_numeric : &str = not_numeric(file).unwrap().0;
    if is_next_char_x(not_numeric, '+') {
        delimiter = "+\n";
    }
    // Parses until reaching the ##END
    loop {
        // If at the end of the data section
        if is_next_tag_x(file, "##END") {
            break
        }
        let res = parse_xydata_line(file, Some(delimiter));
        match res {
            Err(_) => {
                break
            },
            Ok(x) => {
                file = x.0;
                for val in x.1 {
                    spectrum.add_y(val.to_owned());
                }
            }
        }
    }
    // If the spectrum doesn't have all of the y values alloted for every x value, something has
    // probably gone wrong in the mol file
    assert!(spectrum.is_complete());
    Ok(spectrum)
}
pub fn parse_jdx ( filepath : & str ) -> Result < Spectrum , & str > { // Reads to string all at once let file : & str ; let res = & read_file_to_string ( filepath ) ; match res { Err ( _ ) => { return Err ( " " ) } , Ok ( x ) => { file = x ; } } // If this block is run, the file is prpbably not a proper .jdx format if ! is_next_char_x ( file , '#' ) { println ! ( " " filepath ) ; return Err ( " " ) } // Parses the header to construct the initial Spectrum // The order of these statements is important let ( mut file , mut spectrum ) = { let ( file , title ) = get_field_or_default ( file , " " , " " ) ; let ( file , spectrum_type ) = get_field_or_default ( file , " " , " " ) ; let ( file , state ) = get_field_or_default ( file , " " , " " ) ; debug_println ! ( " " title spectrum_type state ) ; let ( file , xunits_str ) = get_field_or_default ( file , " " , " " ) ; let mut xunits : Xunits = Xunits :: cm ; // Sets xunits to one of the possibilities in the Xunits enum if xunits_str == " " { xunits = Xunits :: um ; } else { xunits = Xunits :: cm ; } let ( file , yunits_str ) = get_field_or_default ( file , " " , " " ) ; let mut yunits : Yunits = Yunits :: Absorbance ; if yunits_str == " " { yunits = Yunits :: Absorbance ; } else { yunits = Yunits :: Transmittance ; } let ( file , y_factor ) = get_field_or_default ( file , " " , " " ) ; let y_factor : f32 = y_factor . parse :: < f32 > ( ) . unwrap_or ( 1.0 ) ; debug_println ! ( " " y_factor ) ; let ( file , first_x ) = parse_until_field ( file , " " ) . unwrap ( ) ; let first_x : f32 = first_x . parse :: < f32 > ( ) . unwrap ( ) ; debug_println ! ( " " first_x ) ; let ( file , last_x ) = parse_until_field ( file , " " ) . unwrap ( ) ; let last_x : f32 = last_x . parse :: < f32 > ( ) . unwrap ( ) ; debug_println ! ( " " last_x ) ; let ( file , npoints ) = parse_until_field ( file , " " ) . unwrap ( ) ; let npoints : i32 = npoints . parse :: < i32 > ( ) . unwrap ( ) ; debug_println ! ( " " npoints ) ; let mut spec = Spectrum :: new ( title , spectrum_type , state , xunits , yunits , first_x , last_x , npoints ) ; spec . set_y_factor ( y_factor ) ; ( file , spec ) } ; // Parses until the XYDATA file = parse_until_field ( file , " " ) . unwrap ( ) . 0 ; // Finds what delimiter the XYDATA line uses // The only possible delimiters are " " and "+" let mut delimiter = " \n " ; let not_numeric : & str = not_numeric ( file ) . unwrap ( ) . 0 ; if is_next_char_x ( not_numeric , '+' ) { delimiter = " \n " ; } // Parses until reaching the ##END loop { // If at the end of the data section if is_next_tag_x ( file , " " ) { break } let res = parse_xydata_line ( file , Some ( delimiter ) ) ; match res { Err ( _ ) => { break } , Ok ( x ) => { file = x . 0 ; for val in x . 1 { spectrum . add_y ( val . to_owned ( ) ) ; } } } } // If the spectrum doesn't have all of the y values alloted for every x value, something has // probably gone wrong in the mol file assert ! ( spectrum is_complete ( ) ) ; Ok ( spectrum ) }
Feed this function a jdx filepath and get back a Spectrum struct
Feed this function a jdx filepath and get back a Spectrum struct
filepath
&str
filepath
&str