pub fn load_cows_from_files(path: &Path) -> Result<Vec<Cow>, Box<dyn Error>> {
    // Initialize a vector for images.
    let mut cows: Vec<Cow> = Vec::new();

    // Read each file from the directory.
    for data in path.read_dir()? {

        // If at least one file exists...
        if let Ok(cow_path) = data {
            // Read data as String.
            let raw_data = fs::read_to_string(cow_path.path())?;
            // Convert raw data(String) to Json.
            let json_data: Value = serde_json::from_str(&raw_data)?;
            // Convert image saved as serde_json::Value to a vector of String.
            // This converted data will be cleaned later.
            let image_raw = value_vec_to_string_vec(json_data["image"].as_array().unwrap().clone());

            // Initialize a vector that will be returned as a result.
            let mut image = Vec::new();

            // Correct some escape characters and quotes.
            // Todo: Make an indenpendent function for this process.
            // https://users.rust-lang.org/t/escape-speech-marks-in-regex/4266/5
            let starting_double_quote_re = Regex::new(r#"^""#).unwrap();
            let ending_double_quote_re = Regex::new(r#""$"#).unwrap();
            let escaped_back_slash_re = Regex::new(r#"\\\\"#).unwrap();

            // Read each line.
            for line in image_raw {
                // Initialize an empty vector that will store cleaned lines.
                let mut temp_line = String::new();
                // Clean lines.
                temp_line = starting_double_quote_re.replace(&line,
                    |_caps: &Captures| { "".to_string() }
                ).to_string();
                temp_line = ending_double_quote_re.replace(&temp_line,
                    |_caps: &Captures| { "".to_string() }
                ).to_string();
                temp_line = escaped_back_slash_re.replace_all(&temp_line,
                    |_caps: &Captures| { "\\".to_string() }
                ).to_string();

                image.push(temp_line);
            }

            // Push the image data to the vector of Cow.
            cows.push( Cow {
                name: json_data["name"].to_string(),
                max_text_length: get_max_text_len(image.clone()).unwrap(),
                image: image.clone(),
            });
        }
    }

    match cows.is_empty() {
        // If there's at least one image, return data.
        false => Ok(cows),
        // If there's no image, return Error.
        true => Err(Box::new(NoCowFound)),
    }
}
/// Load images from files.
///
/// Note: Some characters in image may make the function fails.
Load images from files.
Note: Some characters in image may make the function fails.
Load images from files . Note : Some characters in image may make the function fails .
pub fn load_cows_from_files(path: &Path) -> Result<Vec<Cow>, Box<dyn Error>> {
    // Initialize a vector for images.
    let mut cows: Vec<Cow> = Vec::new();
    // Read each file from the directory.
    for data in path.read_dir()? {
        // If at least one file exists...
        if let Ok(cow_path) = data {
            // Read data as String.
            let raw_data = fs::read_to_string(cow_path.path())?;
            // Convert raw data(String) to Json.
            let json_data: Value = serde_json::from_str(&raw_data)?;
            // Convert image saved as serde_json::Value to a vector of String.
            // This converted data will be cleaned later.
            let image_raw = value_vec_to_string_vec(json_data["image"].as_array().unwrap().clone());
            // Initialize a vector that will be returned as a result.
            let mut image = Vec::new();
            // Correct some escape characters and quotes.
            // Todo: Make an indenpendent function for this process.
            // https://users.rust-lang.org/t/escape-speech-marks-in-regex/4266/5
            let starting_double_quote_re = Regex::new(r#"^""#).unwrap();
            let ending_double_quote_re = Regex::new(r#""$"#).unwrap();
            let escaped_back_slash_re = Regex::new(r#"\\\\"#).unwrap();
            // Read each line.
            for line in image_raw {
                // Initialize an empty vector that will store cleaned lines.
                let mut temp_line = String::new();
                // Clean lines.
                temp_line = starting_double_quote_re.replace(&line,
                    |_caps: &Captures| { "".to_string() }
                ).to_string();
                temp_line = ending_double_quote_re.replace(&temp_line,
                    |_caps: &Captures| { "".to_string() }
                ).to_string();
                temp_line = escaped_back_slash_re.replace_all(&temp_line,
                    |_caps: &Captures| { "\\".to_string() }
                ).to_string();
                image.push(temp_line);
            }
            // Push the image data to the vector of Cow.
            cows.push( Cow {
                name: json_data["name"].to_string(),
                max_text_length: get_max_text_len(image.clone()).unwrap(),
                image: image.clone(),
            });
        }
    }
    match cows.is_empty() {
        // If there's at least one image, return data.
        false => Ok(cows),
        // If there's no image, return Error.
        true => Err(Box::new(NoCowFound)),
    }
}
pub fn load_cows_from_files ( path : & Path ) -> Result < Vec < Cow > , Box < dyn Error > > { // Initialize a vector for images. let mut cows : Vec < Cow > = Vec :: new ( ) ; // Read each file from the directory. for data in path . read_dir ( ) ? { // If at least one file exists... if let Ok ( cow_path ) = data { // Read data as String. let raw_data = fs :: read_to_string ( cow_path . path ( ) ) ? ; // Convert raw data(String) to Json. let json_data : Value = serde_json :: from_str ( & raw_data ) ? ; // Convert image saved as serde_json::Value to a vector of String. // This converted data will be cleaned later. let image_raw = value_vec_to_string_vec ( json_data [ " " ] . as_array ( ) . unwrap ( ) . clone ( ) ) ; // Initialize a vector that will be returned as a result. let mut image = Vec :: new ( ) ; // Correct some escape characters and quotes. // Todo: Make an indenpendent function for this process. // https://users.rust-lang.org/t/escape-speech-marks-in-regex/4266/5 let starting_double_quote_re = Regex :: new ( r#"^""# ) . unwrap ( ) ; let ending_double_quote_re = Regex :: new ( r#""$"# ) . unwrap ( ) ; let escaped_back_slash_re = Regex :: new ( r#"\\\\"# ) . unwrap ( ) ; // Read each line. for line in image_raw { // Initialize an empty vector that will store cleaned lines. let mut temp_line = String :: new ( ) ; // Clean lines. temp_line = starting_double_quote_re . replace ( & line , | _caps : & Captures | { " " . to_string ( ) } ) . to_string ( ) ; temp_line = ending_double_quote_re . replace ( & temp_line , | _caps : & Captures | { " " . to_string ( ) } ) . to_string ( ) ; temp_line = escaped_back_slash_re . replace_all ( & temp_line , | _caps : & Captures | { " \\ " . to_string ( ) } ) . to_string ( ) ; image . push ( temp_line ) ; } // Push the image data to the vector of Cow. cows . push ( Cow { name : json_data [ " " ] . to_string ( ) , max_text_length : get_max_text_len ( image . clone ( ) ) . unwrap ( ) , image : image . clone ( ) , } ) ; } } match cows . is_empty ( ) { // If there's at least one image, return data. false => Ok ( cows ) , // If there's no image, return Error. true => Err ( Box :: new ( NoCowFound ) ) , } }
Load images from files.
Load images from files .
path
&Path