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.

Example
Path{ p: "/foo/bar.txt" }.parent() //-> Some(Path{ p: "/foo" })
Path{ p: "/foo/bar/" }.parent() //-> Some(Path{ p: "/foo" })
Path{ p: "/" }.parent() //-> None
Source Code
public 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) })
}