1/**
2 * Path service layers backed by the Deno Standard Library.
3 *
4 * **Example** (Using Deno path operations)
5 *
6 * ```ts import.meta.vitest
7 * import { Effect, Path } from "effect"
8 * import { DenoPath } from "@effect/platform-deno"
9 *
10 * const program = Effect.gen(function*() {
11 * const path = yield* Path.Path
12 * return path.extname("file.txt")
13 * }).pipe(Effect.provide(DenoPath.layer))
14 *
15 * Effect.runSync(program) // => ".txt"
16 * ```
17 *
18 * @since 4.0.0
19 */
20
21import * as DenoPath from "@std/path"
22import * as DenoPathPosix from "@std/path/posix"
23import * as DenoPathWin from "@std/path/windows"
24import * as Effect from "effect/Effect"
25import * as Layer from "effect/Layer"
26import * as Path from "effect/Path"
27import * as PlatformError from "effect/PlatformError"
28
29const fileUrlOps = (impl: {
30 readonly fromFileUrl: typeof DenoPath.fromFileUrl
31 readonly toFileUrl: typeof DenoPath.toFileUrl
32}) => ({
33 fromFileUrl: (url: URL): Effect.Effect<string, PlatformError.BadArgument> =>
34 Effect.try({
35 try: () => impl.fromFileUrl(url),
36 catch: (cause) =>
37 new PlatformError.BadArgument({
38 module: "Path",
39 method: "fromFileUrl",
40 cause
41 })
42 }),
43 toFileUrl: (path: string): Effect.Effect<URL, PlatformError.BadArgument> =>
44 Effect.try({
45 try: (): URL => impl.toFileUrl(path),
46 catch: (cause): PlatformError.BadArgument =>
47 new PlatformError.BadArgument({
48 module: "Path",
49 method: "toFileUrl",
50 cause
51 })
52 })
53})
54
55/**
56 * A {@linkplain Layer.Layer | layer} that provides POSIX path operations.
57 *
58 * @category layers
59 * @since 4.0.0
60 */
61export const layerPosix: Layer.Layer<Path.Path> = Layer.succeed(Path.Path)(
62 Path.Path.of({
63 [Path.TypeId]: Path.TypeId,
64 ...DenoPathPosix,
65 sep: DenoPathPosix.SEPARATOR,
66 ...fileUrlOps(DenoPathPosix)
67 })
68)
69
70/**
71 * A {@linkplain Layer.Layer | layer} that provides Windows path operations.
72 *
73 * @category layers
74 * @since 4.0.0
75 */
76export const layerWin32: Layer.Layer<Path.Path> = Layer.succeed(
77 Path.Path
78)(
79 Path.Path.of({
80 [Path.TypeId]: Path.TypeId,
81 ...DenoPathWin,
82 sep: DenoPathWin.SEPARATOR,
83 ...fileUrlOps(DenoPathWin)
84 })
85)
86
87/**
88 * A {@linkplain Layer.Layer | layer} that provides OS-agnostic path operations.
89 *
90 * @category layers
91 * @since 4.0.0
92 */
93export const layer: Layer.Layer<Path.Path> = Layer.succeed(
94 Path.Path
95)(
96 Path.Path.of({
97 [Path.TypeId]: Path.TypeId,
98 ...DenoPath,
99 sep: DenoPath.SEPARATOR,
100 ...fileUrlOps(DenoPath)
101 })
102)