From ae05ebb5ffff191bf1a485124096eece0e3fca83 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 10 Apr 2023 11:10:27 -0700 Subject: [PATCH 1/5] Cleanup, added to_string_outer --- src/entry/unix/unix.rs | 4 ++-- src/quantity/quantity.rs | 10 ++++++++++ src/tests.rs | 2 +- src/tokens/mod.rs | 21 +++++++++++++++++---- src/tokens/operator.rs | 6 +++--- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/entry/unix/unix.rs b/src/entry/unix/unix.rs index 56a8ead..c2ea378 100644 --- a/src/entry/unix/unix.rs +++ b/src/entry/unix/unix.rs @@ -81,7 +81,7 @@ pub fn main() -> Result<(), std::io::Error> { Ok(g) => { #[cfg(debug_assertions)] RawTerminal::suspend_raw_mode(&stdout)?; - let out_str = g.print(); + let out_str = g.to_string(); let g = evaluate::evaluate(g); #[cfg(debug_assertions)] RawTerminal::activate_raw_mode(&stdout)?; @@ -100,7 +100,7 @@ pub fn main() -> Result<(), std::io::Error> { style::Bold, color::Fg(color::Green), style::Reset, - q.print(), + q.to_string_outer(), color::Fg(color::Reset) )?; }, diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index a4e0f14..dfff1b4 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -36,6 +36,16 @@ impl ToString for Quantity { } impl Quantity { + pub fn to_string_outer(&self) -> String { + let n = self.v.to_string(); + if self.unitless() { return n; } + + let u = self.u.to_string(); + return format!("{n} {u}"); + } + + + pub fn new_float(f: f64) -> Option { let v = Scalar::new_float(f); if v.is_none() { return None; } diff --git a/src/tests.rs b/src/tests.rs index 2fbe217..376a9f9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -10,7 +10,7 @@ fn eval_to_str(s: &str) -> Result { //let out_str = g.print(); return match evaluate::evaluate(g) { - Ok(x) => Ok(x.print()), + Ok(x) => Ok(x.to_string()), Err(_) => Err(()) }; } diff --git a/src/tokens/mod.rs b/src/tokens/mod.rs index 12cdb3c..baad63f 100644 --- a/src/tokens/mod.rs +++ b/src/tokens/mod.rs @@ -26,15 +26,28 @@ pub enum Token { ), } -impl Token { - - pub fn print(&self) -> String { +impl ToString for Token { + fn to_string(&self) -> String { match self { Token::Number(v) => v.to_string(), - Token::Constant(_,s) => s.clone(), + Token::Constant(_, s) => s.clone(), Token::Operator(o,a) => o.print(a) } } +} + +impl Token { + + // This is called only when this is the outermost token. + // This sometimes leads to different--usually more verbose--behavior. + pub fn to_string_outer(&self) -> String { + match self { + Token::Number(v) => v.to_string_outer(), + Token::Constant(_, s) => s.clone(), + Token::Operator(o,a) => o.print(a) + } + } + #[inline(always)] pub fn get_args(&self) -> Option<&VecDeque> { diff --git a/src/tokens/operator.rs b/src/tokens/operator.rs index fca87e7..2053691 100644 --- a/src/tokens/operator.rs +++ b/src/tokens/operator.rs @@ -54,7 +54,7 @@ impl Operator { #[inline(always)] fn add_parens_to_arg(&self, arg: &Token) -> String { - let mut astr: String = arg.print(); + let mut astr: String = arg.to_string(); if let Token::Operator(o,_) = arg { if o < self { astr = format!("({})", astr); @@ -65,7 +65,7 @@ impl Operator { #[inline(always)] fn add_parens_to_arg_strict(&self, arg: &Token) -> String { - let mut astr: String = arg.print(); + let mut astr: String = arg.to_string(); if let Token::Operator(o,_) = arg { if o <= self { astr = format!("({})", astr); @@ -193,7 +193,7 @@ impl Operator { }, Operator::Function(s) => { - return format!("{}({})", s.to_string(), args[0].print()); + return format!("{}({})", s.to_string(), args[0].to_string()); } }; } From 37206f23f8e8d28d7da1ca15575391cc4763825e Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 10 Apr 2023 11:15:03 -0700 Subject: [PATCH 2/5] Added action --- .github/workflows/build.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..f7a9d25 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,17 @@ +on: [push] + +name: CI + +jobs: + build_and_test: + name: Daisy + runs-on: debian-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + - uses: actions-rs/cargo@v1 + with: + command: build + args: --release --all-features \ No newline at end of file From 856f9ae7c71216397b7d97aa29777c10ad56392d Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 10 Apr 2023 11:26:26 -0700 Subject: [PATCH 3/5] Updated runner --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f7a9d25..3d61c64 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ name: CI jobs: build_and_test: name: Daisy - runs-on: debian-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 From 3c6aeb123007c5ba10aa6af7ba926066f01abdbd Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 10 Apr 2023 11:40:38 -0700 Subject: [PATCH 4/5] Added test to action --- .github/workflows/build.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3d61c64..82f7134 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,4 +14,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: build - args: --release --all-features \ No newline at end of file + args: --release --all-features + with: + command: test + args: --release \ No newline at end of file From 6786c680e9ae4c381acd4e2918e4935f6a86231e Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 10 Apr 2023 11:41:08 -0700 Subject: [PATCH 5/5] Action edit --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 82f7134..da5f0ae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,6 +15,7 @@ jobs: with: command: build args: --release --all-features + - uses: actions-rs/cargo@v1 with: command: test args: --release \ No newline at end of file