blob: 316896af925473c70409c9ef17f7e8cc5fc66b8f (
plain)
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
61
62
63
64
65
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- : dep regex-applicative
module Biz.Namespace
( Namespace (..),
Ext (..),
fromPath,
toPath,
toHaskellModule,
toSchemeModule,
match,
)
where
import Alpha
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Text.Regex.Applicative as Regex
data Ext = Hs | Scm | Nix | Md | Css | Py | Sh | Keys | Json | None
deriving (Eq, Show)
data Namespace = Namespace [String] Ext
deriving (Eq, Show)
match :: String -> Maybe Namespace
match = Regex.match <| Namespace </ path <* Regex.sym '.' <*> ext
where
name =
Regex.many (Regex.psym Char.isUpper)
<> Regex.many (Regex.psym Char.isAlphaNum)
path = Regex.many (name <* Regex.string "/" <|> name)
ext =
Nix <$ Regex.string "nix"
<|> Hs <$ Regex.string "hs"
<|> Scm <$ Regex.string "scm"
<|> Md <$ Regex.string "md"
<|> Css <$ Regex.string "css"
<|> Py <$ Regex.string "py"
<|> Sh <$ Regex.string "sh"
<|> Keys <$ Regex.string "pub"
<|> Json <$ Regex.string "json"
fromPath :: String -> String -> Maybe Namespace
fromPath bizRoot absPath =
List.stripPrefix bizRoot absPath
+> List.stripPrefix "/"
+> match
toHaskellModule :: Namespace -> String
toHaskellModule (Namespace parts Hs) = joinWith "." parts
toHaskellModule (Namespace _ ext) =
panic <| "can't convert " <> show ext <> " to a Haskell module"
toPath :: Namespace -> FilePath
toPath (Namespace parts ext) =
joinWith "/" parts
<> "."
<> lowercase (show ext)
toSchemeModule :: Namespace -> String
toSchemeModule (Namespace parts Scm) = "(" ++ joinWith " " parts ++ ")"
toSchemeModule (Namespace _ ext) =
panic <| "can't convert " <> show ext <> " to a Scheme module"
|