Skip to content
TypedFilePath.fs 1.89 KiB
Newer Older
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"