pub fn read_from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, NewPaletteError> {
        let f = File::open(&file_path)?;
        let reader = BufReader::new(f);

        fn is_comment(s: &str) -> bool {
            s.chars().skip_while(|c| c.is_whitespace()).next() == Some('#')
        }

        let mut colors = vec![];
        let mut name = String::new();
        //let mut color_count = 0;

        let mut lines = reader.lines().enumerate();
        while let Some((index, Ok(line))) = lines.next() {
            if is_comment(&line) || line.trim().len() == 0 {
                continue;
            }

            let line_num = index + 1;

            if line_num == 1 {
                if line != "GIMP Palette" {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }

                continue;
            } else if line_num == 2 {
                if !line.starts_with("Name:") {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }

                name = line[4..].trim().to_string();
                continue;
            } else if line_num == 3 {
                if !line.starts_with("Columns:") {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }

                // TODO: Handle this value.
                // - Should we only read `color_count` colors, or
                // - Err if color_count != (line count - 3 - comments), or
                // - Explicitly ignore this value
                //color_count = line[4..].trim().to_string();
                continue;
            }

            let mut split = line.split_whitespace();
            match (split.next(), split.next(), split.next()) {
                (Some(r_str), Some(g_str), Some(b_str)) => {
                    let r =
                        r_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s r into a byte", line_num));
                    let g =
                        g_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s g into a byte", line_num));
                    let b =
                        b_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s b into a byte", line_num));
                    colors.push(Color { r, g, b });
                }
                _ => {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line.clone(),
                               })
                }
            }
        }

        Ok(Self {
               _name: name,
               _colors: colors,
           })
    }
/// Creates a new Palette from a file path.
/// TODO: Decide how to handle the 'Columns' header value.
Creates a new Palette from a file path.
TODO: Decide how to handle the 'Columns' header value.
Creates a new Palette from a file path . TODO : Decide how to handle the ' Columns ' header value .
pub fn read_from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, NewPaletteError> {
        let f = File::open(&file_path)?;
        let reader = BufReader::new(f);
        fn is_comment(s: &str) -> bool {
            s.chars().skip_while(|c| c.is_whitespace()).next() == Some('#')
        }
        let mut colors = vec![];
        let mut name = String::new();
        //let mut color_count = 0;
        let mut lines = reader.lines().enumerate();
        while let Some((index, Ok(line))) = lines.next() {
            if is_comment(&line) || line.trim().len() == 0 {
                continue;
            }
            let line_num = index + 1;
            if line_num == 1 {
                if line != "GIMP Palette" {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }
                continue;
            } else if line_num == 2 {
                if !line.starts_with("Name:") {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }
                name = line[4..].trim().to_string();
                continue;
            } else if line_num == 3 {
                if !line.starts_with("Columns:") {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line,
                               });
                }
                // TODO: Handle this value.
                // - Should we only read `color_count` colors, or
                // - Err if color_count != (line count - 3 - comments), or
                // - Explicitly ignore this value
                //color_count = line[4..].trim().to_string();
                continue;
            }
            let mut split = line.split_whitespace();
            match (split.next(), split.next(), split.next()) {
                (Some(r_str), Some(g_str), Some(b_str)) => {
                    let r =
                        r_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s r into a byte", line_num));
                    let g =
                        g_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s g into a byte", line_num));
                    let b =
                        b_str
                            .parse::<u8>()
                            .expect(&format!("Failed to parse line {}'s b into a byte", line_num));
                    colors.push(Color { r, g, b });
                }
                _ => {
                    return Err(NewPaletteError::InvalidData {
                                   line_num,
                                   val: line.clone(),
                               })
                }
            }
        }
        Ok(Self {
               _name: name,
               _colors: colors,
           })
    }
pub fn read_from_file < P : AsRef < Path > > ( file_path : P ) -> Result < Self , NewPaletteError > { let f = File :: open ( & file_path ) ? ; let reader = BufReader :: new ( f ) ; fn is_comment ( s : & str ) -> bool { s . chars ( ) . skip_while ( | c | c . is_whitespace ( ) ) . next ( ) == Some ( '#' ) } let mut colors = vec ! [ ] ; let mut name = String :: new ( ) ; //let mut color_count = 0; let mut lines = reader . lines ( ) . enumerate ( ) ; while let Some ( ( index , Ok ( line ) ) ) = lines . next ( ) { if is_comment ( & line ) || line . trim ( ) . len ( ) == 0 { continue ; } let line_num = index + 1 ; if line_num == 1 { if line != " " { return Err ( NewPaletteError :: InvalidData { line_num , val : line , } ) ; } continue ; } else if line_num == 2 { if ! line . starts_with ( " " ) { return Err ( NewPaletteError :: InvalidData { line_num , val : line , } ) ; } name = line [ 4 .. ] . trim ( ) . to_string ( ) ; continue ; } else if line_num == 3 { if ! line . starts_with ( " " ) { return Err ( NewPaletteError :: InvalidData { line_num , val : line , } ) ; } // TODO: Handle this value. // - Should we only read `color_count` colors, or // - Err if color_count != (line count - 3 - comments), or // - Explicitly ignore this value //color_count = line[4..].trim().to_string(); continue ; } let mut split = line . split_whitespace ( ) ; match ( split . next ( ) , split . next ( ) , split . next ( ) ) { ( Some ( r_str ) , Some ( g_str ) , Some ( b_str ) ) => { let r = r_str . parse :: < u8 > ( ) . expect ( & format ! ( " " line_num ) ) ; let g = g_str . parse :: < u8 > ( ) . expect ( & format ! ( " " line_num ) ) ; let b = b_str . parse :: < u8 > ( ) . expect ( & format ! ( " " line_num ) ) ; colors . push ( Color { r , g , b } ) ; } _ => { return Err ( NewPaletteError :: InvalidData { line_num , val : line . clone ( ) , } ) } } } Ok ( Self { _name : name , _colors : colors , } ) }
Creates a new Palette from a file path.
Creates a new Palette from a file path .
file_path
P
file_path
P