Garden 🌻

Path::parent

Get the parent for this path. For directories, this is the parent directory. For files, this is the containing directory.

This always returns a Path, so the root directory (i.e. /) is returned as-is.

Path{ p: "/foo/bar.txt" }.parent() //-> Path{ p: "/foo" }
Path{ p: "/foo/bar/" }.parent() //-> Path{ p: "/foo" }
Path{ p: "/" }.parent() //-> Path{ p: "/" }
Source Codepublic method parent(this: Path): Option<Path> {
  let p = this.p.strip_suffix("/")
  if p == "" {
    return None
  }

  let parts = p.split("/").slice(0, -1)
  if parts.is_empty() {
    return None
  }

  Some(Path{ p: "/".join(parts) })
}