Garden 🌻

Path::set_extension

Set the extension of the filename to new_ext.

Path{ p: "/foo/bar"}.set_extension("log") //-> Path{ p: "/foo/bar.log"}
Path{ p: "/foo/bar.txt"}.set_extension("log") //-> Path{ p: "/foo/bar.log"}
Path{ p: "/foo/bar.txt.gz"}.set_extension("log") //-> Path{ p: "/foo/bar.log"}
Source Codepublic method set_extension(this: Path, new_ext: String): Path {
  let new_ext = "." ^ new_ext
  match this.file_name() {
    Some(file_name) => {
      let base = match this.parent() {
        Some(parent) => parent.p ^ "/"
        None => ""
      }

      let p = match file_name.split_once(".") {
        Some((base_file_name, _ext)) => {
          (base ^ base_file_name) ^ new_ext
        }
        None => (base ^ file_name) ^ new_ext
      }
      Path{ p: p }
    },
    None => this,
  }
}