summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud.bailly@iohk.io>2024-09-23 14:36:15 +0200
committerArnaud Bailly <arnaud.bailly@iohk.io>2024-09-23 14:36:15 +0200
commitde98bfc28feebfe112a378017c73d0e20dfb2937 (patch)
treeb5bca14d2053ace6746ef9b9c82bed6c19b20254
parent66abe3871337139e839b693ea9e290eaa9a72516 (diff)
downloadlambda-nantes-de98bfc28feebfe112a378017c73d0e20dfb2937.tar.gz
Scaffolding of main, library, and tests
-rw-r--r--rust/Cargo.toml8
-rw-r--r--rust/sample/test.txt1
-rw-r--r--rust/src/lambda.rs40
-rw-r--r--rust/src/main.rs24
-rw-r--r--rust/tests/interpret_test.rs7
5 files changed, 61 insertions, 19 deletions
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 7d75412..be173f5 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -4,3 +4,11 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+
+[lib]
+name = "lambda"
+path = "src/lambda.rs"
+
+[[bin]]
+name = "lambda"
+path = "src/main.rs"
diff --git a/rust/sample/test.txt b/rust/sample/test.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/rust/sample/test.txt
@@ -0,0 +1 @@
+1
diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs
new file mode 100644
index 0000000..a24b031
--- /dev/null
+++ b/rust/src/lambda.rs
@@ -0,0 +1,40 @@
+use std::{
+ fmt::{self, Display},
+ fs::read_to_string,
+};
+
+pub fn run(arg: &str) -> String {
+ let content = read_to_string(arg).unwrap();
+ let result = interpret(&content.to_string());
+
+ result.to_string()
+}
+
+#[derive(Debug, PartialEq)]
+pub enum Value {
+ Num(i32),
+}
+
+impl Display for Value {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Value::Num(i) => write!(f, "{}", i),
+ }
+ }
+}
+
+fn interpret(_arg: &str) -> Value {
+ Value::Num(1)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::interpret;
+ use super::Value::Num;
+
+ #[test]
+ fn it_works() {
+ let result = interpret("1");
+ assert_eq!(Num(1), result);
+ }
+}
diff --git a/rust/src/main.rs b/rust/src/main.rs
index e18788e..b6cc9bf 100644
--- a/rust/src/main.rs
+++ b/rust/src/main.rs
@@ -1,23 +1,9 @@
-fn main() {
- println!("Hello, world!");
-}
-
-#[derive(Debug, PartialEq)]
-pub enum Value {
- Num(i32),
-}
+use std::env::args;
-pub fn interpret(arg: &str) -> Value {
- Value::Num(1)
-}
+use lambda::run;
-#[cfg(test)]
-mod tests {
- use crate::{interpret, Value::Num};
+mod lambda;
- #[test]
- fn it_works() {
- let result = interpret("1");
- assert_eq!(Num(1), result);
- }
+fn main() {
+ run(&args().nth(1).unwrap());
}
diff --git a/rust/tests/interpret_test.rs b/rust/tests/interpret_test.rs
new file mode 100644
index 0000000..f0b5201
--- /dev/null
+++ b/rust/tests/interpret_test.rs
@@ -0,0 +1,7 @@
+use lambda::run;
+
+#[test]
+fn interpreter_can_read_and_interpret_file() {
+ let result = run("sample/test.txt");
+ assert_eq!("1", result);
+}