How encapsulate in a function?

Hi friends, this piece work fine, but sometimes i need call in another scope, and again, again.
please help me put together in function?? thanks :sneezing_face:

let mut color_html = String::new();
let movie_path = Path::new("/home/pichibw/movies/");

// .load color from folder date modification
let path = PathBuf::from(&movie_path);

let modified = match path.metadata().unwrap().modified() {
                   Ok(time) => time,
                   Err(_) => SystemTime::now(),
};

match SystemTime::now().duration_since(modified) {
                        Ok(file_age) => {
                            if file_age.as_secs() < 86400 /* 24h in seconds */ {
                                println!("Match green color: {:?}", file_age);
                                color_html = "green".to_string();
                                //return Ok(());
                            } else if file_age.as_secs() > 1728000 && file_age.as_secs() < 3456000 /* 20 days in seconds */ {
                                color_html = "yellow".to_string();
                            } else if file_age.as_secs() > 3456000 /* 40 days in seconds */ {
                                color_html = "#00FFFF".to_string();
                            } else {
                                color_html = "white".to_string();
                            }
                        },
                        Err(_) => {},
                    }

The first step is for you to decide on the parameters for the function and its return type. Please try to write the function signature.

This work for me, maybe something better ?

// return color to html

fn color_to_html(path_file: &Path) -> String {
    let modified = match path_file.metadata().unwrap().modified() {
        Ok(time) => time,
        Err(_) => SystemTime::now(),
    };

    match SystemTime::now().duration_since(modified) {
        Ok(file_age) => {
            if file_age.as_secs() < 86400 /* 24h in seconds */ {
                println!("Match green color: {:?}", file_age);
                "green".to_string()
                //return Ok(());
            } else if file_age.as_secs() > 1728000 && file_age.as_secs() < 3456000 /* 20 days in seconds */ {
                "yellow".to_string()
            } else if file_age.as_secs() > 3456000 /* 40 days in seconds */ {
                "#00FFFF".to_string()
            } else {
                "white".to_string()
            }
        },
        Err(_) => {"white".to_string()},
    }
}

Looks great!

Since all the strings returned are static, you could return type &'static str and then remove the calls to to_string(). This avoids allocating a String. But if you need a String, that's fine too.

If you continue to return a String you could call to_string only once like this, to make it a little cleaner to read (this is just my preference):

            let str = match SystemTime::now().duration_since(modified) {
                Ok(file_age) => {
                    if file_age.as_secs() < 86400
                    /* 24h in seconds */
                    {
                        println!("Match green color: {:?}", file_age);
                        "green"
                        //return Ok(());
                    } else if file_age.as_secs() > 1728000
                        && file_age.as_secs() < 3456000
                    /* 20 days in seconds */
                    {
                        "yellow"
                    } else if file_age.as_secs() > 3456000
                    /* 40 days in seconds */
                    {
                        "#00FFFF"
                    } else {
                        "white"
                    }
                }
                Err(_) => "white",
            };
            str.to_string()

You can also use and_then to get rid of the unwrap above:

    let modified = match path_file.metadata().and_then(|m| m.modified()) {
        Ok(time) => time,
        Err(_) => SystemTime::now(),
    };
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.