1
1
module LambdaBuffers.Compiler.Cli.Compile (CompileOpts (.. ), compile ) where
2
2
3
- import Control.Lens (makeLenses )
3
+ import Control.Lens (makeLenses , (&) , (.~) )
4
4
import Control.Lens.Getter ((^.) )
5
5
import Data.ByteString qualified as BS
6
+ import Data.ProtoLens (Message (defMessage ))
6
7
import Data.ProtoLens qualified as Pb
7
8
import Data.ProtoLens.TextFormat qualified as PbText
8
9
import Data.Text.Lazy qualified as Text
9
10
import Data.Text.Lazy.IO qualified as Text
10
- import LambdaBuffers.Compiler.KindCheck (check )
11
- import LambdaBuffers.Compiler.ProtoCompat (
12
- FromProtoErr (NamingError , ProtoError ),
13
- IsMessage (fromProto , toProto ),
14
- )
15
- import LambdaBuffers.Compiler.ProtoCompat.Types qualified as ProtoCompat
16
- import Proto.Compiler as ProtoLib (CompilerInput , CompilerOutput )
11
+ import LambdaBuffers.Compiler (runCompiler )
12
+ import Proto.Compiler (CompilerError , CompilerInput , CompilerOutput )
13
+ import Proto.Compiler_Fields (compilerError , compilerResult )
17
14
import System.FilePath.Lens (extension )
18
15
19
16
data CompileOpts = CompileOpts
@@ -24,20 +21,19 @@ data CompileOpts = CompileOpts
24
21
25
22
makeLenses ''CompileOpts
26
23
27
- -- NOTE(cstml) - let 's use Katip instead of print.
24
+ -- NOTE(cstml): Let 's use Katip instead of print.
28
25
29
26
-- | Compile LambdaBuffers modules
30
27
compile :: CompileOpts -> IO ()
31
28
compile opts = do
32
- compIn <- readCompilerInput (opts ^. input)
33
- case fromProto @ CompilerInput @ ProtoCompat. CompilerInput compIn of
34
- Left err -> case err of
35
- NamingError ne -> print $ " Encountered a naming error " <> show ne
36
- ProtoError pe -> print $ " Encountered a proto error " <> show pe
37
- Right compIn' -> do
38
- print @ String " Successfully processed the CompilerInput"
39
- let result = check compIn'
40
- writeCompilerOutput (opts ^. output) (toProto result)
29
+ compInp <- readCompilerInput (opts ^. input)
30
+ case runCompiler compInp of
31
+ Left compErr -> do
32
+ putStrLn " Encountered errors during Compilation"
33
+ writeCompilerError (opts ^. output) compErr
34
+ Right compRes -> do
35
+ putStrLn " Compilation succeeded"
36
+ writeCompilerOutput (opts ^. output) (defMessage & compilerResult .~ compRes)
41
37
return ()
42
38
43
39
readCompilerInput :: FilePath -> IO CompilerInput
@@ -52,7 +48,10 @@ readCompilerInput fp = do
52
48
return $ PbText. readMessageOrDie content
53
49
_ -> error $ " Unknown CompilerInput format " <> ext
54
50
55
- writeCompilerOutput :: FilePath -> ProtoLib. CompilerOutput -> IO ()
51
+ writeCompilerError :: FilePath -> CompilerError -> IO ()
52
+ writeCompilerError fp err = writeCompilerOutput fp (defMessage & compilerError .~ err)
53
+
54
+ writeCompilerOutput :: FilePath -> CompilerOutput -> IO ()
56
55
writeCompilerOutput fp cr = do
57
56
let ext = fp ^. extension
58
57
case ext of
0 commit comments