diff options
author | eric thul <thul.eric@gmail.com> | 2015-04-08 19:49:24 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-04-12 11:19:22 -0400 |
commit | c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68 (patch) | |
tree | 5470b97ffc561915796f5a8a2a9541d9ebef50ae /src/Options.purs | |
parent | 9d38968bbe4bbf54e2ca836d9a1550d74d4da703 (diff) | |
download | purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.tar.gz purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.tar.zst purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.zip |
Rewrite using purescript for the implementation
Diffstat (limited to 'src/Options.purs')
-rw-r--r-- | src/Options.purs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Options.purs b/src/Options.purs new file mode 100644 index 0000000..b96cddc --- /dev/null +++ b/src/Options.purs | |||
@@ -0,0 +1,72 @@ | |||
1 | module PursLoader.Options | ||
2 | ( pscMakeOptions | ||
3 | , pscMakeDefaultOutput | ||
4 | , pscMakeOutputOption | ||
5 | ) where | ||
6 | |||
7 | import Data.Either (either) | ||
8 | |||
9 | import Data.Foreign (Foreign(), F()) | ||
10 | import Data.Foreign.Class (IsForeign, read, readProp) | ||
11 | import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined) | ||
12 | |||
13 | import Data.Maybe (Maybe(..), maybe) | ||
14 | |||
15 | noPreludeOpt = "no-prelude" | ||
16 | |||
17 | noOptsOpt = "no-opts" | ||
18 | |||
19 | noMagicDoOpt = "no-magic-do" | ||
20 | |||
21 | noTcoOpt = "no-tco" | ||
22 | |||
23 | verboseErrorsOpt = "verbose-errors" | ||
24 | |||
25 | outputOpt = "output" | ||
26 | |||
27 | pscMakeDefaultOutput = "output" | ||
28 | |||
29 | newtype Options | ||
30 | = Options { noPrelude :: NullOrUndefined Boolean | ||
31 | , noOpts :: NullOrUndefined Boolean | ||
32 | , noMagicDo :: NullOrUndefined Boolean | ||
33 | , noTco :: NullOrUndefined Boolean | ||
34 | , verboseErrors :: NullOrUndefined Boolean | ||
35 | , output :: NullOrUndefined String | ||
36 | } | ||
37 | |||
38 | instance isForeignOptions :: IsForeign Options where | ||
39 | read obj = (\a b c d e f -> | ||
40 | Options { noPrelude: a | ||
41 | , noOpts: b | ||
42 | , noMagicDo: c | ||
43 | , noTco: d | ||
44 | , verboseErrors: e | ||
45 | , output: f | ||
46 | }) <$> readProp noPreludeOpt obj | ||
47 | <*> readProp noOptsOpt obj | ||
48 | <*> readProp noMagicDoOpt obj | ||
49 | <*> readProp noTcoOpt obj | ||
50 | <*> readProp verboseErrorsOpt obj | ||
51 | <*> readProp outputOpt obj | ||
52 | |||
53 | booleanOpt :: String -> NullOrUndefined Boolean -> [String] | ||
54 | booleanOpt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined opt) | ||
55 | |||
56 | stringOpt :: String -> NullOrUndefined String -> [String] | ||
57 | stringOpt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined opt) | ||
58 | |||
59 | pscMakeOutputOption :: Foreign -> Maybe String | ||
60 | pscMakeOutputOption query = either (const Nothing) | ||
61 | (\(Options a) -> runNullOrUndefined a.output) | ||
62 | (read query) | ||
63 | |||
64 | pscMakeOptions :: Foreign -> [String] | ||
65 | pscMakeOptions query = either (const []) fold parsed | ||
66 | where parsed = read query :: F Options | ||
67 | fold (Options a) = booleanOpt noPreludeOpt a.noPrelude <> | ||
68 | booleanOpt noOptsOpt a.noOpts <> | ||
69 | booleanOpt noMagicDoOpt a.noMagicDo <> | ||
70 | booleanOpt noTcoOpt a.noTco <> | ||
71 | booleanOpt verboseErrorsOpt a.verboseErrors <> | ||
72 | stringOpt outputOpt a.output | ||