summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Dudley2024-02-28 22:00:26 -0700
committerRory Dudley2024-02-28 22:00:26 -0700
commitb8db4a0cda9eb080c9e89c635be4c9abfc9f88d6 (patch)
tree06337724ea2f7d056b5e6aa676579bf37a99b62a
parent2d2d5af3553b732237937549dc9b6df28b7c00cb (diff)
downloaddwarvish-b8db4a0cda9eb080c9e89c635be4c9abfc9f88d6.tar.gz
Refactor status variables in Poem::read()
Moved the 'couplet' and 'metered' variables into functions impl'd for Verse. This cuts down on the boilerplate in the parsing loop, and also makes it so that their match statements are only ran when needed, rather than being called at every iteration of the loop.
-rw-r--r--src/recite.rs57
1 files changed, 29 insertions, 28 deletions
diff --git a/src/recite.rs b/src/recite.rs
index f75cd61..074f723 100644
--- a/src/recite.rs
+++ b/src/recite.rs
@@ -289,6 +289,28 @@ impl Verse {
fn clause(&self) -> Vec<String> {
self.stanza.clause.clone()
}
+
+ /// Check if this verse is piping output
+ fn couplet(verse: Option<&Verse>) -> bool {
+ match verse {
+ Some(verse) => match verse.meter {
+ Meter::Couplet | Meter::Quiet | Meter::And | Meter::String => true,
+ Meter::None => false,
+ },
+ None => false,
+ }
+ }
+
+ /// Check if this verse has a meter
+ fn cadence(verse: Option<&Verse>) -> bool {
+ match verse {
+ Some(verse) => match verse.meter {
+ Meter::Couplet | Meter::Quiet | Meter::And | Meter::String => true,
+ Meter::None => false,
+ },
+ None => false,
+ }
+ }
}
/// An entire shell command parsed into [verse's][Verse]
@@ -428,34 +450,13 @@ impl Poem {
// Get the next character in the input string
let char = chars.next();
- // Check if the previous verse is piping output to current
- // TODO: Don't need to run this on each iteration of the loop, just when
- // a stanza is pushed to a verse
- let couplet = match prev {
- Some(prev) => match prev.meter {
- Meter::Couplet => true,
- Meter::Quiet | Meter::And | Meter::String | Meter::None => false,
- },
- None => false,
- };
-
- // Check if the previous verse was metered
- // Need this to check for parse/input errors
- let metered = match prev {
- Some(prev) => match prev.meter {
- Meter::Couplet | Meter::Quiet | Meter::And | Meter::String => true,
- Meter::None => false,
- },
- None => false,
- };
-
// Do something depending on what the character is
match char {
// Print an error, and return None if a Meter was used without
// a Stanza before it
Some(meter)
if (meter == '|' || meter == '&' || meter == ';')
- && metered
+ && Verse::cadence(prev)
&& stanza.is_empty() =>
{
// TODO: Add more verbose error message
@@ -475,7 +476,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::Couplet,
- couplet,
+ Verse::couplet(prev),
));
// Clear the stacks
@@ -504,7 +505,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::And,
- couplet,
+ Verse::couplet(prev),
));
}
@@ -514,7 +515,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::Quiet,
- couplet,
+ Verse::couplet(prev),
));
}
@@ -525,7 +526,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::Quiet,
- couplet,
+ Verse::couplet(prev),
));
// We can break out of the loop here, since it's
@@ -551,7 +552,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::String,
- couplet,
+ Verse::couplet(prev),
));
stanza.clear();
@@ -584,7 +585,7 @@ impl Poem {
verses.push(Verse::new(
Stanza::new(stanza.clone()),
Meter::None,
- couplet,
+ Verse::couplet(prev),
));
}