my.path

Members

Structs

AbsolutePath
struct AbsolutePath

The path is guaranteed to be the absolute, normalized and tilde expanded path.

Path
struct Path

Types a string as a Path to provide path related operations.

Meta

Authors

Joakim Brännström (joakim.brannstrom@gmx.com)

The purpose of this module is to allow you to segregate your string data that represent a path from the rest. A string-that-is-a-type have specific characteristics that we want to represent. This module have two types that help you encode these characteristics.

This allows you to construct type safe APIs wherein a parameter that takes a path can be assured that the data **actually** is a path. The API can further e.g. require the parameter to be have the even higher restriction that it is an absolute path.

I have found it extremely useful in my own programs to internally only work with AbsolutePath types. There is a boundary in my programs that takes data and converts it appropriately to AbsolutePaths. This is usually configuration data, command line input, external libraries etc. This conversion layer handles the defensive coding, validity checking etc that is needed of the data.

This has overall lead to a significant reduction in the number of bugs I have had when handling paths and simplified the code. The program normally look something like this:

user input as raw strings via e.g. getopt. wrap path strings as either Path or AbsolutePath. Prefer AbsolutePath when applicable but there are cases where this is the wrong behavior. Lets say that the user input is relative to some working directory. Then later on in your program the two are combined to produce an AbsolutePath. internally in the program all parameters are AbsolutePath. A function that takes an AbsolutePath can be assured it is a path, full expanded and thus do not need any defensive code. It can use it as it is.

I have used an equivalent program structure when interacting with external libraries.