@@ -13,55 +13,29 @@ import Data.Text qualified as Text
1313import Data.Traversable (for )
1414import LambdaBuffers.Codegen.LamVal qualified as LV
1515import LambdaBuffers.Codegen.LamVal.MonadPrint qualified as LV
16+ import LambdaBuffers.Codegen.Rust.Print.Refs qualified as RR
1617import LambdaBuffers.Codegen.Rust.Print.Syntax qualified as R
1718import LambdaBuffers.Codegen.Rust.Print.TyDef qualified as TD
1819import LambdaBuffers.Compiler.LamTy qualified as LT
1920import LambdaBuffers.ProtoCompat qualified as PC
2021import Prettyprinter (Doc , Pretty (pretty ), align , angles , braces , brackets , colon , comma , dot , dquotes , encloseSep , equals , group , langle , lbracket , line , lparen , parens , pipe , punctuate , rangle , rbracket , rparen , semi , space , vsep , (<+>) )
2122import Proto.Codegen_Fields qualified as P
2223
23- caseIntERef :: R. QValName
24- caseIntERef = R. qForeignRef R. MkValueName " lbr-prelude" [" lamval" ] " case_int"
25-
26- bigInt :: R. QValName
27- bigInt = R. qForeignRef R. MkValueName " num-bigint" [] " BigInt"
28-
29- vecAsSlice :: R. QValName
30- vecAsSlice = R. qForeignRef R. MkValueName " lbr-prelude" [" lamval" ] " case_int"
31-
32- vecMacro :: R. QValName
33- vecMacro = R. qForeignRef R. MkValueName " std" [] " vec!"
34-
35- fromU32Trait :: R. QTraitName
36- fromU32Trait = R. qForeignRef R. MkTraitName " std" [" convert" ] " From<u32>"
37-
38- fromStrTrait :: R. QTraitName
39- fromStrTrait = R. qForeignRef R. MkTraitName " std" [" convert" ] " From<&str>"
40-
41- cloneTrait :: R. QTraitName
42- cloneTrait = R. qForeignRef R. MkTraitName " std" [" clone" ] " Clone"
43-
44- boxNew :: R. QValName
45- boxNew = R. qForeignRef R. MkValueName " std" [" boxed" ] " Box::new"
46-
47- phantomData :: R. QTyName
48- phantomData = R. qForeignRef R. MkTyName " std" [" marker" ] " PhantomData"
49-
5024{- | Clone a value (converting a type to owned)
5125 As in codegen we cannot know whether a type is owned or borrowed, we make sure to have an owned type by making a clone.
5226 We must also borrow it first for the same reason (worst case it's a double && which Rust knows how to deal with)
5327-}
5428clone :: Doc ann -> Doc ann
55- clone = useTraitMethod cloneTrait " clone" . borrow
29+ clone = useTraitMethod RR. cloneTrait " clone" . borrow
5630
5731borrow :: Doc ann -> Doc ann
5832borrow doc = " &" <> doc
5933
6034fromU32 :: Doc ann -> Doc ann
61- fromU32 = useTraitMethod fromU32Trait " from"
35+ fromU32 = useTraitMethod RR. fromU32Trait " from"
6236
6337fromStr :: Doc ann -> Doc ann
64- fromStr = useTraitMethod fromStrTrait " from"
38+ fromStr = useTraitMethod RR. fromStrTrait " from"
6539
6640useTraitMethod :: R. QTraitName -> Text -> Doc ann -> Doc ann
6741useTraitMethod trait method d = angles (" _" <+> " as" <+> R. printRsQTraitName trait) <> R. doubleColon <> pretty method <> parens d
@@ -132,7 +106,7 @@ printLamE iTyDefs lamVal = do
132106 bodyDoc <- printValueE iTyDefs body
133107 argDoc <- printValueE iTyDefs arg
134108
135- return $ R. printRsQValName boxNew <> parens (" move" <+> pipe <> argDoc <> colon <+> " &_" <> pipe <+> braces (space <> group bodyDoc))
109+ return $ R. printRsQValName RR. boxNew <> parens (" move" <+> pipe <> argDoc <> colon <+> " &_" <> pipe <+> braces (space <> group bodyDoc))
136110
137111printAppE :: MonadPrint m => PC. TyDefs -> LV. ValueE -> LV. ValueE -> m (Doc ann )
138112printAppE iTyDefs funVal argVal = do
@@ -192,8 +166,8 @@ printOtherCase iTyDefs otherCase = do
192166--- | `printCaseIntE i [(1, x), (2,y)] (\other -> z)` translates into `LambdaBuffers.Runtime.Plutus.LamValcaseIntE i [(1,x), (2,y)] (\other -> z)`
193167printCaseIntE :: MonadPrint m => PC. TyDefs -> LV. ValueE -> [(LV. ValueE , LV. ValueE )] -> (LV. ValueE -> LV. ValueE ) -> m (Doc ann )
194168printCaseIntE iTyDefs caseIntVal cases otherCase = do
195- caseIntERefDoc <- R. printRsQValName <$> LV. importValue caseIntERef
196- _ <- LV. importValue bigInt
169+ caseIntERefDoc <- R. printRsQValName <$> LV. importValue RR. caseIntERef
170+ _ <- LV. importValue RR. bigInt
197171 caseValDoc <- printValueE iTyDefs caseIntVal
198172 caseDocs <-
199173 for
@@ -204,7 +178,7 @@ printCaseIntE iTyDefs caseIntVal cases otherCase = do
204178 return $ group $ parens (fromU32 conditionDoc <> " ," <+> bodyDoc)
205179 )
206180 otherDoc <- printLamE iTyDefs otherCase
207- return $ group $ caseIntERefDoc <> encloseSep lparen rparen comma [caseValDoc, align (R. printRsQValName vecMacro <> encloseSep lbracket rbracket comma caseDocs), otherDoc]
181+ return $ group $ caseIntERefDoc <> encloseSep lparen rparen comma [caseValDoc, align (R. printRsQValName RR. vecMacro <> encloseSep lbracket rbracket comma caseDocs), otherDoc]
208182
209183printListE :: MonadPrint m => PC. TyDefs -> [LV. ValueE ] -> m (Doc ann )
210184printListE iTyDefs vals = do
@@ -214,12 +188,12 @@ printListE iTyDefs vals = do
214188printNewListE :: MonadPrint m => PC. TyDefs -> [LV. ValueE ] -> m (Doc ann )
215189printNewListE iTyDefs vals = do
216190 lst <- printListE iTyDefs vals
217- return $ R. printRsQValName vecMacro <> lst
191+ return $ R. printRsQValName RR. vecMacro <> lst
218192
219193printCaseListE :: MonadPrint m => PC. TyDefs -> LV. ValueE -> [(Int , [LV. ValueE ] -> LV. ValueE )] -> (LV. ValueE -> LV. ValueE ) -> m (Doc ann )
220194printCaseListE iTyDefs caseListVal cases otherCase = do
221195 caseValDoc <- printValueE iTyDefs caseListVal
222- vecAsSliceDoc <- R. printRsQValName <$> LV. importValue vecAsSlice
196+ vecAsSliceDoc <- R. printRsQValName <$> LV. importValue RR. vecAsSlice
223197 caseDocs <-
224198 for
225199 cases
@@ -282,7 +256,7 @@ printRecordE iTyDefs (qtyN@(mn', tyN'), _) vals = do
282256
283257printPhantomDataField :: PC. TyArg -> Doc ann
284258printPhantomDataField tyArg =
285- TD. phantomFieldIdent tyArg <> colon <+> R. printRsQTyName phantomData
259+ TD. phantomFieldIdent tyArg <> colon <+> R. printRsQTyName RR. phantomData
286260
287261printProductE :: MonadPrint m => PC. TyDefs -> LV. QProduct -> [LV. ValueE ] -> m (Doc ann )
288262printProductE iTyDefs (qtyN@ (mn', tyN'), _) vals = do
@@ -294,7 +268,7 @@ printProductE iTyDefs (qtyN@(mn', tyN'), _) vals = do
294268 case Map. lookup qtyN iTyDefs of
295269 Just (PC. TyDef _ (PC. TyAbs tyArgs (PC. ProductI (PC. Product fields _)) _) _) -> return (toList tyArgs, fields)
296270 _ -> throwInternalError " Expected a ProductE but got something else (TODO(szg251): Print got)"
297- let phantomFieldDocs = R. printRsQTyName phantomData <$ TD. collectPhantomTyArgs fieldTys tyArgs
271+ let phantomFieldDocs = R. printRsQTyName RR. phantomData <$ TD. collectPhantomTyArgs fieldTys tyArgs
298272 mayBoxedFields = zip vals $ TD. isRecursive iTyDefs mn tyN <$> fieldTys
299273
300274 fieldDocs <- for mayBoxedFields (printMaybeBoxed iTyDefs)
@@ -305,7 +279,7 @@ printMaybeBoxed :: MonadPrint m => PC.TyDefs -> (LV.ValueE, Bool) -> m (Doc ann)
305279printMaybeBoxed iTyDefs (val, False ) = clone <$> printValueE iTyDefs val
306280printMaybeBoxed iTyDefs (val, True ) = do
307281 valDoc <- clone <$> printValueE iTyDefs val
308- return $ R. printRsQValName boxNew <> parens valDoc
282+ return $ R. printRsQValName RR. boxNew <> parens valDoc
309283
310284printTupleE :: MonadPrint m => PC. TyDefs -> LV. ValueE -> LV. ValueE -> m (Doc ann )
311285printTupleE iTyDefs l r = do
@@ -395,4 +369,4 @@ printInstanceLamE (argTy : argTys) iTyDefs lamVal = do
395369 argDoc <- printValueE iTyDefs arg
396370 let argTy' = " &'a" <+> R. printRsQTyName argTy
397371
398- return $ R. printRsQValName boxNew <> parens (" move" <+> pipe <> argDoc <> colon <+> argTy' <> pipe <+> braces (space <> group bodyDoc))
372+ return $ R. printRsQValName RR. boxNew <> parens (" move" <+> pipe <> argDoc <> colon <+> argTy' <> pipe <+> braces (space <> group bodyDoc))
0 commit comments