Skip to content

Commit 7adcef1

Browse files
authored
Merge pull request #166 from mlabs-haskell/szg251/rust-testsuites
Rust testsuites
2 parents f496625 + 3914de1 commit 7adcef1

File tree

17 files changed

+1483
-29
lines changed

17 files changed

+1483
-29
lines changed

extras/lbf-nix/lbf-rust.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let
8686
text = ''
8787
num-bigint = "0.4.4"
8888
serde_json = { version = "1.0.107", features = ["arbitrary_precision"] }
89-
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [ "lbf", ], rev = "fb93fa590908580eb40368369bf6614d42ce9a95" }
89+
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [ "lbf", ], ref = "szg251/fix-lbf", rev = "4460f2ff8af7993a24209ebe28df5ef9b3b3b397" }
9090
'';
9191
};
9292

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/LamVal.hs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,10 @@ printRefE pkgs ref = do
335335
qvn <- LV.resolveRef ref
336336
case ref of
337337
(argTy : _, builtin)
338-
| builtin
339-
== "toPlutusData"
340-
|| builtin
341-
== "fromPlutusData"
342-
|| builtin
343-
== "toJson"
344-
|| builtin
345-
== "fromJson" -> do
338+
| builtin == "toPlutusData"
339+
|| builtin == "fromPlutusData"
340+
|| builtin == "toJson"
341+
|| builtin == "fromJson" -> do
346342
lamTyDoc <- printLamTy pkgs argTy
347343
methodDoc <- R.printRsValName . R.qualifiedEntity <$> LV.importValue qvn
348344
return $ angles lamTyDoc <> R.doubleColon <> methodDoc

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/Syntax.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@ printMkCtor :: PC.TyName -> Doc ann
125125
printMkCtor = printTyName
126126

127127
printFieldName :: PC.FieldName -> Doc ann
128-
printFieldName (PC.FieldName n _) = pretty n
128+
printFieldName (PC.FieldName n _) = pretty . toSnakeCase $ n
129+
130+
toSnakeCase :: Text -> Text
131+
toSnakeCase = Text.concatMap f
132+
where
133+
f c
134+
| Char.isUpper c = "_" <> Text.toLower (Text.singleton c)
135+
| otherwise = Text.singleton c
129136

130137
printVarName :: PC.VarName -> Doc ann
131138
printVarName (PC.VarName n _) = pretty n

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/TyDef.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ printTyDef pkgs (PC.TyDef tyN tyabs _) = do
6161
else return $ group $ printTyDefKw kw <+> printTyName tyN <> generics <+> equals <+> absDoc
6262

6363
printTyDefKw :: TyDefKw -> Doc ann
64-
printTyDefKw StructTyDef = "pub struct"
65-
printTyDefKw EnumTyDef = "pub enum"
66-
printTyDefKw SynonymTyDef = "pub type"
64+
printTyDefKw StructTyDef = pub "struct"
65+
printTyDefKw EnumTyDef = pub "enum"
66+
printTyDefKw SynonymTyDef = pub "type"
67+
68+
pub :: Doc ann -> Doc ann
69+
pub doc = "pub" <+> doc
6770

6871
box :: R.QTyName
6972
box = R.qForeignRef R.MkTyName "std" ["boxed"] "Box"
@@ -164,7 +167,7 @@ printProd pkgs parentTyN tyArgs (PC.Product fields _) = do
164167
phantomFields = printPhantomData <$> phantomTyArgs
165168
if null fields && null phantomTyArgs
166169
then return semi
167-
else return $ encloseSep lparen rparen comma (tyDocs <> phantomFields) <> semi
170+
else return $ encloseSep lparen rparen comma (pub <$> (tyDocs <> phantomFields)) <> semi
168171

169172
{- | Filter out unused type arguments in order to make PhantomData fields for them
170173
This is done in a recursive manner: if we encounter a type application, we resolve the type from TyDefs, and substitute
@@ -223,7 +226,7 @@ printField :: MonadPrint m => R.PkgMap -> PC.TyName -> PC.Field -> m (Doc ann)
223226
printField pkgs parentTyN (PC.Field fn ty) = do
224227
let fnDoc = printFieldName fn
225228
tyDoc <- printTyTopLevel pkgs parentTyN ty
226-
return $ fnDoc <> colon <+> tyDoc
229+
return $ pub fnDoc <> colon <+> tyDoc
227230

228231
printPhantomData :: PC.TyArg -> Doc ann
229232
printPhantomData tyArg =

runtimes/rust/lbr-prelude/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ impl From<&Value> for JsonType {
2929
/// Error type representing Json conversion errors
3030
#[derive(thiserror::Error, Debug)]
3131
pub enum Error {
32+
#[error("Malformed JSON string, unable to parse")]
33+
MalformedJson,
3234
#[error("{parser:?} > Expected a JSON type: {wanted:?}, but got a JSON type: {got:?}")]
3335
UnexpectedJsonType {
3436
got: JsonType,

runtimes/rust/lbr-prelude/src/json.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ pub trait Json {
1818
fn from_json(value: &Value) -> Result<Self, Error>
1919
where
2020
Self: Sized;
21+
22+
fn to_json_string(&self) -> String {
23+
self.to_json().to_string()
24+
}
25+
26+
fn from_json_string(string: &str) -> Result<Self, Error>
27+
where
28+
Self: Sized,
29+
{
30+
Value::from_str(string)
31+
.map_err(|_| Error::MalformedJson)
32+
.and_then(|value| Self::from_json(&value))
33+
}
2134
}
2235

2336
// lbf-prelude::json instance rule implementations

testsuites/lbt-plutus/lbt-plutus-rust/Cargo.lock

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testsuites/lbt-plutus/lbt-plutus-rust/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [
8+
"lbf",
9+
], ref = "szg251/fix-lbf", rev = "4460f2ff8af7993a24209ebe28df5ef9b3b3b397" }
710
lbf-plutus-rust-golden-api = { path = ".extras/lbf-plutus-rust-golden-api" }
811
lbf-prelude = { path = ".extras/lbf-prelude" }
12+
lbr-prelude = { path = ".extras/lbr-prelude" }
913
serde_json = "1.0.108"
1014
num-bigint = "0.4.4"
11-
plutus-ledger-api = { git = "https://github.com/mlabs-haskell/plutus-ledger-api-rust", features = [
12-
"lbf",
13-
], rev = "fb93fa590908580eb40368369bf6614d42ce9a95" }

testsuites/lbt-plutus/lbt-plutus-rust/src/main.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)