Skip to content

Commit c6e3cb0

Browse files
authored
feat(stdlib): Standardize path module examples (#2325)
1 parent b7f902b commit c6e3cb0

File tree

2 files changed

+198
-102
lines changed

2 files changed

+198
-102
lines changed

stdlib/path.gr

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* - The path segment `.` indicates the relative "current" directory of a path, and `..` indicates the parent directory of a path
1818
*
1919
* @example from "path" include Path
20+
* @example let p = Path.fromString("./tmp/file.txt")
2021
*
2122
* @since v0.5.5
2223
*/
@@ -88,6 +89,8 @@ and record TBase<a> {
8889
}
8990
/**
9091
* Represents an absolute path's anchor point.
92+
*
93+
* @since v0.5.5
9194
*/
9295
and provide enum AbsoluteRoot {
9396
Root,
@@ -98,27 +101,35 @@ and provide enum AbsoluteRoot {
98101
// replaced with opaque types if they get added to the language
99102
/**
100103
* Represents a relative path.
104+
*
105+
* @since v0.5.5
101106
*/
102107
abstract record Relative {
103108
_rel: Void,
104109
}
105110

106111
/**
107112
* Represents an absolute path.
113+
*
114+
* @since v0.5.5
108115
*/
109116
abstract record Absolute {
110117
_abs: Void,
111118
}
112119

113120
/**
114121
* Represents a path referencing a file.
122+
*
123+
* @since v0.5.5
115124
*/
116125
abstract record File {
117126
_file: Void,
118127
}
119128

120129
/**
121130
* Represents a path referencing a directory.
131+
*
132+
* @since v0.5.5
122133
*/
123134
abstract record Directory {
124135
_directory: Void,
@@ -127,10 +138,14 @@ abstract record Directory {
127138
/**
128139
* Represents a path typed on (`Absolute` or `Relative`) and (`File` or
129140
* `Directory`)
141+
*
142+
* @since v0.5.5
130143
*/
131144
abstract type rec TypedPath<a, b> = (TBase<a>, TFileType<b>, List<String>)
132145
/**
133146
* Represents a system path.
147+
*
148+
* @since v0.5.5
134149
*/
135150
and provide enum Path {
136151
AbsoluteFile(TypedPath<Absolute, File>),
@@ -141,6 +156,8 @@ and provide enum Path {
141156

142157
/**
143158
* Represents a platform-specific path encoding scheme.
159+
*
160+
* @since v0.5.5
144161
*/
145162
provide enum Platform {
146163
Windows,
@@ -149,13 +166,17 @@ provide enum Platform {
149166

150167
/**
151168
* Represents an error that can occur when finding a property of a path.
169+
*
170+
* @since v0.5.5
152171
*/
153172
provide enum PathOperationError {
154173
IncompatiblePathType,
155174
}
156175

157176
/**
158177
* Represents an error that can occur when appending paths.
178+
*
179+
* @since v0.5.5
159180
*/
160181
provide enum AppendError {
161182
AppendToFile,
@@ -164,6 +185,8 @@ provide enum AppendError {
164185

165186
/**
166187
* Represents the status of an ancestry check between two paths.
188+
*
189+
* @since v0.5.5
167190
*/
168191
provide enum AncestryStatus {
169192
Descendant,
@@ -175,6 +198,8 @@ provide enum AncestryStatus {
175198
/**
176199
* Represents an error that can occur when the types of paths are incompatible
177200
* for an operation.
201+
*
202+
* @since v0.5.5
178203
*/
179204
provide enum IncompatibilityError {
180205
DifferentRoots,
@@ -183,6 +208,8 @@ provide enum IncompatibilityError {
183208

184209
/**
185210
* Represents possible errors for the `relativeTo` operation.
211+
*
212+
* @since v0.5.5
186213
*/
187214
provide enum RelativizationError {
188215
Incompatible(IncompatibilityError),
@@ -375,10 +402,10 @@ let fromStringHelper = (pathStr, platform) => {
375402
* @param platform: The platform whose path separators should be used for parsing
376403
* @returns The path wrapped with details encoded within the type
377404
*
378-
* @example fromString("file.txt") // a relative Path referencing the file ./file.txt
379-
* @example fromString(".") // a relative Path referencing the current directory
380-
* @example fromString("/bin/", Posix) // an absolute Path referencing the directory /bin/
381-
* @example fromString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt
405+
* @example Path.fromString("file.txt") // a relative Path referencing the file ./file.txt
406+
* @example Path.fromString(".") // a relative Path referencing the current directory
407+
* @example Path.fromString("/bin/", Path.Posix) // an absolute Path referencing the directory /bin/
408+
* @example Path.fromString("C:\\file.txt", Path.Windows) // a relative Path referencing the file C:\file.txt
382409
*
383410
* @since v0.5.5
384411
* @history v0.6.0: Merged with `fromPlatformString`; modified signature to accept platform
@@ -423,9 +450,9 @@ let toStringHelper = (path, platform) => {
423450
* @param platform: The `Platform` to use to represent the path as a string
424451
* @returns A string representing the given path
425452
*
426-
* @example toString(fromString("/file.txt")) == "/file.txt"
427-
* @example toString(fromString("dir/"), Posix) == "./dir/"
428-
* @example toString(fromString("C:/file.txt"), Windows) == "C:\\file.txt"
453+
* @example Path.toString(Path.fromString("/file.txt")) == "/file.txt"
454+
* @example Path.toString(Path.fromString("dir/"), Path.Posix) == "./dir/"
455+
* @example Path.toString(Path.fromString("C:/file.txt"), Path.Windows) == "C:\\file.txt"
429456
*
430457
* @since v0.5.5
431458
* @history v0.6.0: Merged with `toPlatformString`; modified signature to accept platform
@@ -440,8 +467,8 @@ provide let toString = (path, platform=Posix) => {
440467
* @param path: The path to inspect
441468
* @returns `true` if the path is a directory path or `false` otherwise
442469
*
443-
* @example isDirectory(fromString("file.txt")) == false
444-
* @example isDirectory(fromString("/bin/")) == true
470+
* @example Path.isDirectory(Path.fromString("file.txt")) == false
471+
* @example Path.isDirectory(Path.fromString("/bin/")) == true
445472
*
446473
* @since v0.5.5
447474
*/
@@ -456,8 +483,8 @@ provide let isDirectory = path => {
456483
* @param path: The path to inspect
457484
* @returns `true` if the path is absolute or `false` otherwise
458485
*
459-
* @example isAbsolute(fromString("/Users/me")) == true
460-
* @example isAbsolute(fromString("./file.txt")) == false
486+
* @example Path.isAbsolute(Path.fromString("/Users/me")) == true
487+
* @example Path.isAbsolute(Path.fromString("./file.txt")) == false
461488
*/
462489
provide let isAbsolute = path => {
463490
let (base, _, _) = pathInfo(path)
@@ -490,9 +517,9 @@ let rec appendHelper = (path: PathInfo, toAppend: PathInfo) =>
490517
* @param toAppend: The relative path to append
491518
* @returns `Ok(path)` combining the base and appended paths or `Err(err)` if the paths are incompatible
492519
*
493-
* @example append(fromString("./dir/"), fromString("file.txt")) == Ok(fromString("./dir/file.txt"))
494-
* @example append(fromString("a.txt"), fromString("b.sh")) == Err(AppendToFile) // cannot append to file path
495-
* @example append(fromString("./dir/"), fromString("/dir2")) == Err(AppendAbsolute) // cannot append an absolute path
520+
* @example Path.append(Path.fromString("./dir/"), Path.fromString("file.txt")) == Ok(Path.fromString("./dir/file.txt"))
521+
* @example Path.append(Path.fromString("a.txt"), Path.fromString("b.sh")) == Err(Path.AppendToFile) // cannot append to file path
522+
* @example Path.append(Path.fromString("./dir/"), Path.fromString("/dir2")) == Err(Path.AppendAbsolute) // cannot append an absolute path
496523
*
497524
* @since v0.5.5
498525
*/
@@ -547,21 +574,21 @@ let relativeToHelper = (source: PathInfo, dest: PathInfo) => {
547574
* path from the source path.
548575
*
549576
* If the source and destination are incompatible in their bases, the result
550-
* will be `Err(IncompatibilityError)`.
577+
* will be `Err(Path.IncompatibilityError)`.
551578
*
552579
* If the route to the destination cannot be concretely determined from the
553-
* source, the result will be `Err(ImpossibleRelativization)`.
580+
* source, the result will be `Err(Path.ImpossibleRelativization)`.
554581
*
555582
* @param source: The source path
556583
* @param dest: The destination path to resolve
557584
* @returns `Ok(path)` containing the relative path if successfully resolved or `Err(err)` otherwise
558585
*
559-
* @example relativeTo(fromString("/usr"), fromString("/usr/bin")) == Ok(fromString("./bin"))
560-
* @example relativeTo(fromString("/home/me"), fromString("/home/me")) == Ok(fromString("."))
561-
* @example relativeTo(fromString("/file.txt"), fromString("/etc/")) == Ok(fromString("../etc/"))
562-
* @example relativeTo(fromString(".."), fromString("../../thing")) Ok(fromString("../thing"))
563-
* @example relativeTo(fromString("/usr/bin"), fromString("C:/Users")) == Err(Incompatible(DifferentRoots))
564-
* @example relativeTo(fromString("../here"), fromString("./there")) == Err(ImpossibleRelativization)
586+
* @example Path.relativeTo(Path.fromString("/usr"), Path.fromString("/usr/bin")) == Ok(Path.fromString("./bin"))
587+
* @example Path.relativeTo(Path.fromString("/home/me"), Path.fromString("/home/me")) == Ok(Path.fromString("."))
588+
* @example Path.relativeTo(Path.fromString("/file.txt"), Path.fromString("/etc/")) == Ok(Path.fromString("../etc/"))
589+
* @example Path.relativeTo(Path.fromString(".."), Path.fromString("../../thing")) Ok(Path.fromString("../thing"))
590+
* @example Path.relativeTo(Path.fromString("/usr/bin"), Path.fromString("C:/Users")) == Err(Path.Incompatible(Path.DifferentRoots))
591+
* @example Path.relativeTo(Path.fromString("../here"), Path.fromString("./there")) == Err(Path.ImpossibleRelativization)
565592
*
566593
* @since v0.5.5
567594
*/
@@ -595,16 +622,16 @@ let ancestryHelper = (base: PathInfo, path: PathInfo) => {
595622
}
596623

597624
/**
598-
* Determines the relative ancestry betwen two paths.
625+
* Determines the relative ancestry between two paths.
599626
*
600627
* @param base: The first path to consider
601628
* @param path: The second path to consider
602629
* @returns `Ok(ancestryStatus)` with the relative ancestry between the paths if they are compatible or `Err(err)` if they are incompatible
603630
*
604-
* @example ancestry(fromString("/usr"), fromString("/usr/bin/bash")) == Ok(Ancestor)
605-
* @example ancestry(fromString("/Users/me"), fromString("/Users")) == Ok(Descendant)
606-
* @example ancestry(fromString("/usr"), fromString("/etc")) == Ok(Neither)
607-
* @example ancestry(fromString("C:/dir1"), fromString("/dir2")) == Err(DifferentRoots)
631+
* @example Path.ancestry(Path.fromString("/usr"), Path.fromString("/usr/bin/bash")) == Ok(Path.Ancestor)
632+
* @example Path.ancestry(Path.fromString("/Users/me"), Path.fromString("/Users")) == Ok(Path.Descendant)
633+
* @example Path.ancestry(Path.fromString("/usr"), Path.fromString("/etc")) == Ok(Path.Neither)
634+
* @example Path.ancestry(Path.fromString("C:/dir1"), Path.fromString("/dir2")) == Err(Path.DifferentRoots)
608635
*
609636
* @since v0.5.5
610637
*/
@@ -631,8 +658,8 @@ let parentHelper = (path: PathInfo) => match (path) {
631658
* @param path: The path to inspect
632659
* @returns A path corresponding to the parent directory of the given path
633660
*
634-
* @example parent(fromString("./dir/inner")) == fromString("./dir/")
635-
* @example parent(fromString("/")) == fromString("/")
661+
* @example Path.parent(Path.fromString("./dir/inner")) == Path.fromString("./dir/")
662+
* @example Path.parent(Path.fromString("/")) == Path.fromString("/")
636663
*
637664
* @since v0.5.5
638665
*/
@@ -651,8 +678,8 @@ let basenameHelper = (path: PathInfo) => match (path) {
651678
* @param path: The path to inspect
652679
* @returns `Some(path)` containing the basename of the path or `None` if the path does not have one
653680
*
654-
* @example basename(fromString("./dir/file.txt")) == Some("file.txt")
655-
* @example basename(fromString(".."))) == None
681+
* @example Path.basename(Path.fromString("./dir/file.txt")) == Some("file.txt")
682+
* @example Path.basename(Path.fromString(".."))) == None
656683
*
657684
* @since v0.5.5
658685
*/
@@ -682,10 +709,10 @@ let stemExtHelper = (path: PathInfo) => match (path) {
682709
* @param path: The path to inspect
683710
* @returns `Ok(path)` containing the stem of the file path or `Err(err)` if the path is a directory path
684711
*
685-
* @example stem(fromString("file.txt")) == Ok("file")
686-
* @example stem(fromString(".gitignore")) == Ok(".gitignore")
687-
* @example stem(fromString(".a.tar.gz")) == Ok(".a")
688-
* @example stem(fromString("/dir/")) == Err(IncompatiblePathType) // can only take stem of a file path
712+
* @example Path.stem(Path.fromString("file.txt")) == Ok("file")
713+
* @example Path.stem(Path.fromString(".gitignore")) == Ok(".gitignore")
714+
* @example Path.stem(Path.fromString(".a.tar.gz")) == Ok(".a")
715+
* @example Path.stem(Path.fromString("/dir/")) == Err(Path.IncompatiblePathType) // can only take stem of a file path
689716
*
690717
* @since v0.5.5
691718
*/
@@ -705,10 +732,10 @@ provide let stem = (path: Path) => {
705732
* @param path: The path to inspect
706733
* @returns `Ok(path)` containing the extension of the file path or `Err(err)` if the path is a directory path
707734
*
708-
* @example extension(fromString("file.txt")) == Ok(".txt")
709-
* @example extension(fromString(".gitignore")) == Ok("")
710-
* @example extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
711-
* @example extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path
735+
* @example Path.extension(Path.fromString("file.txt")) == Ok(".txt")
736+
* @example Path.extension(Path.fromString(".gitignore")) == Ok("")
737+
* @example Path.extension(Path.fromString(".a.tar.gz")) == Ok(".tar.gz")
738+
* @example Path.extension(Path.fromString("/dir/")) == Err(Path.IncompatiblePathType) // can only take extension of a file path
712739
*
713740
* @since v0.5.5
714741
*/
@@ -728,10 +755,10 @@ provide let extension = (path: Path) => {
728755
* @param path: The path to modify
729756
* @returns The path with the extension removed
730757
*
731-
* @example removeExtension(fromString("file.txt")) == fromString("file")
732-
* @example removeExtension(fromString(".gitignore")) == fromString(".gitignore")
733-
* @example removeExtension(fromString("./dir/file")) == fromString("dir/file")
734-
* @example removeExtension(fromString("./dir/")) == fromString("dir/")
758+
* @example Path.removeExtension(Path.fromString("file.txt")) == Path.fromString("file")
759+
* @example Path.removeExtension(Path.fromString(".gitignore")) == Path.fromString(".gitignore")
760+
* @example Path.removeExtension(Path.fromString("./dir/file")) == Path.fromString("dir/file")
761+
* @example Path.removeExtension(Path.fromString("./dir/")) == Path.fromString("dir/")
735762
*
736763
* @since v7.0.0
737764
*/
@@ -752,11 +779,11 @@ provide let removeExtension = (path: Path) => {
752779
* @param extension: The new extension
753780
* @returns The modified path
754781
*
755-
* @example updateExtension(fromString("file.txt"), "ext") == fromString("file.ext")
756-
* @example updateExtension(fromString("file.txt"), "") == fromString("file.")
757-
* @example updateExtension(fromString(".gitignore"), "ext") == fromString(".gitignore.ext")
758-
* @example updateExtension(fromString("./dir/file"), "ext") == fromString("dir/file.ext")
759-
* @example updateExtension(fromString("./dir/"), "ext") == fromString("dir/")
782+
* @example Path.updateExtension(Path.fromString("file.txt"), "ext") == Path.fromString("file.ext")
783+
* @example Path.updateExtension(Path.fromString("file.txt"), "") == Path.fromString("file.")
784+
* @example Path.updateExtension(Path.fromString(".gitignore"), "ext") == Path.fromString(".gitignore.ext")
785+
* @example Path.updateExtension(Path.fromString("./dir/file"), "ext") == Path.fromString("dir/file.ext")
786+
* @example Path.updateExtension(Path.fromString("./dir/"), "ext") == Path.fromString("dir/")
760787
*
761788
* @since v7.0.0
762789
*/
@@ -782,9 +809,9 @@ let rootHelper = (path: PathInfo) => match (path) {
782809
* @param path: The path to inspect
783810
* @returns `Ok(root)` containing the root of the path or `Err(err)` if the path is a relative path
784811
*
785-
* @example root(fromString("C:/Users/me/")) == Ok(Drive('C'))
786-
* @example root(fromString("/home/me/")) == Ok(Root)
787-
* @example root(fromString("./file.txt")) == Err(IncompatiblePathType)
812+
* @example Path.root(Path.fromString("C:/Users/me/")) == Ok(Path.Drive('C'))
813+
* @example Path.root(Path.fromString("/home/me/")) == Ok(Path.Root)
814+
* @example Path.root(Path.fromString("./file.txt")) == Err(Path.IncompatiblePathType)
788815
*
789816
* @since v0.5.5
790817
*/

0 commit comments

Comments
 (0)