Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Mbackup.TypedFilePath
open System
open System.IO
// use Discriminated Unions to represent windows path and mingw style linux path.
type TypedFilePath =
| WinPath of path: string
| MingwPath of path: string
| PortablePath of path: string
let joinPath (part1: TypedFilePath) (part2: TypedFilePath) =
match part1 with
| WinPath(p1) ->
match part2 with
| WinPath(p2) | PortablePath(p2) ->
WinPath(Lib.ensureWinDir(p1) + p2)
| _ ->
failwith "Coding error. trying to join different Path type"
| MingwPath(p1) ->
match part2 with
| MingwPath(p2) | PortablePath(p2) ->
MingwPath(Lib.ensureDir(p1) + p2)
| _ ->
failwith "Coding error. trying to join different Path type"
| _ ->
failwith "Coding error. joinPath first path should not be PortablePath"
let joinPortablePath (part1: TypedFilePath) (part2: string) =
joinPath part1 (PortablePath part2)
let toString (tpath: TypedFilePath) =
match tpath with
| WinPath(path) | MingwPath(path) | PortablePath(path) -> path
let toWinPath (tpath: TypedFilePath): string =
match tpath with
| WinPath(path) | PortablePath(path) -> path
| MingwPath(path) -> Lib.toWinPath(path)
let toWin (tpath: TypedFilePath): TypedFilePath =
match tpath with
| MingwPath(path) -> WinPath(Lib.toWinPath(path))
| x -> x
let toMingwPath (tpath: TypedFilePath): string =
match tpath with
| WinPath(path) | PortablePath(path) -> Lib.toMingwPath(path)
| MingwPath(path) -> path
let toMingw (tpath: TypedFilePath): TypedFilePath =
match tpath with
| WinPath(path) -> MingwPath(Lib.toMingwPath(path))
| x -> x
let ensureDir (tpath: TypedFilePath) =
match tpath with
| WinPath(path) -> WinPath(if path.EndsWith "\\" then path else path + "\\")
| MingwPath(path) -> MingwPath(if path.EndsWith "/" then path else path + "/")
| PortablePath(path) -> failwith "Coding error. ensureDir should not be called on PortablePath"