Skip to content

Commit

Permalink
coding style: Group struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
anordal committed Aug 28, 2022
1 parent 09a85e3 commit 1d28e6b
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 81 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Code organisation
* reduce perilous boilerplate
* make flush an error for easier propagation
* approach agreement with rust-fmt (struct grouping)
* approach agreement with rust-fmt

## Write about:
* errexit → errtrace ?
Expand Down
5 changes: 3 additions & 2 deletions src/commonargcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ pub fn common_token_quoting_unneeded(
CommonStrCmdResult::None => None,
CommonStrCmdResult::Some(x) => Some(x),
CommonStrCmdResult::OnlyWithQuotes(x) => {
if let Some(replacement) = x.alt {
if replacement.len() >= x.len {
let (_, len, alt) = x.transform;
if let Some(replacement) = alt {
if replacement.len() >= len {
#[allow(clippy::collapsible_if)] // Could be expanded.
if horizon[i] == b'`' {
return Some(push(
Expand Down
7 changes: 4 additions & 3 deletions src/commonstrcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ fn is_variable_of_numeric_content(c: u8) -> bool {
}

fn bail_doubledigit(context: &[u8], pos: usize) -> CommonStrCmdResult {
CommonStrCmdResult::Some(WhatNow{
tri: Transition::Err(UnsupportedSyntax{
CommonStrCmdResult::Some(WhatNow {
transform: (0, 0, None),
transition: Transition::Err(UnsupportedSyntax {
typ: "Unsupported syntax: Syntactic pitfall",
ctx: context.to_owned(),
pos,
Expand All @@ -223,6 +224,6 @@ fn bail_doubledigit(context: &[u8], pos: usize) -> CommonStrCmdResult {
* Fixing what it does would be 100% subtle \
and might slip through code review unnoticed.\n\
* Fixing its look would make a likely bug look intentional."
}), pre: 0, len: 0, alt: None
}),
})
}
18 changes: 10 additions & 8 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ fn treatfile_fallible(
let mut fill :usize = 0;
let mut buf = [0; MAXHORIZON];

let mut state :Vec<Box<dyn Situation>> = vec!{Box::new(SitNormal{
end_trigger: 0x100, end_replace: None,
let mut state :Vec<Box<dyn Situation>> = vec!{Box::new(SitNormal {
end_trigger: 0x100,
end_replace: None,
})};

loop {
Expand Down Expand Up @@ -140,21 +141,22 @@ fn stackmachine(
let curstate = statebox.as_mut();
let color_pre = if sett.syntax { curstate.get_color() } else { COLOR_NORMAL };
let whatnow = curstate.whatnow(horizon, is_horizon_lengthenable);
let (pre, len, alt) = whatnow.transform;

if whatnow.alt.is_some() {
if alt.is_some() {
out.change = true;
if sett.osel == OutputSelector::Check {
return Err(Error::Check);
}
}

write_colored_slice(
out, color_cur, color_pre, &horizon[.. whatnow.pre]
out, color_cur, color_pre, &horizon[.. pre]
).map_err(Error::Stdio)?;
let replaceable = &horizon[whatnow.pre .. whatnow.pre + whatnow.len];
let progress = whatnow.pre + whatnow.len;
let progress = pre + len;
let replaceable = &horizon[pre .. progress];

match (whatnow.tri, eof) {
match (whatnow.transition, eof) {
(Transition::Err(e), _) => {
return Err(Error::Syntax(e));
}
Expand All @@ -180,7 +182,7 @@ fn stackmachine(
state.last().unwrap().as_ref().get_color()
};
write_transition(
out, color_cur, color_trans, sett, replaceable, whatnow.alt
out, color_cur, color_trans, sett, replaceable, alt
).map_err(Error::Stdio)?;

pos += progress;
Expand Down
16 changes: 8 additions & 8 deletions src/sitcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl Situation for SitCase {
}
let word = &horizon[i..i+len];
if word == b"in" {
return WhatNow{
tri: Transition::Replace(Box::new(SitCaseIn{})),
pre: i + len, len: 0, alt: None
return WhatNow {
transform: (i + len, 0, None),
transition: Transition::Replace(Box::new(SitCaseIn {})),
};
}
return flush(i + len);
Expand Down Expand Up @@ -124,9 +124,9 @@ impl Situation for SitCaseArm {
}

fn pop_kw(pre: usize, len: usize) -> WhatNow {
WhatNow{
tri: Transition::Replace(Box::new(SitExtent{color: COLOR_KWD})),
pre, len, alt: None,
WhatNow {
transform: (pre, len, None),
transition: Transition::Replace(Box::new(SitExtent { color: COLOR_KWD })),
}
}

Expand All @@ -141,8 +141,8 @@ fn test_sit_case() {
sit_expect!(SitCase{}, b" ", &flush(1));
sit_expect!(SitCase{}, b"i", &flush(0), &flush(1));
let found_the_in_word = WhatNow{
tri: Transition::Replace(Box::new(SitCaseIn{})),
pre: 2, len: 0, alt: None
transform: (2, 0, None),
transition: Transition::Replace(Box::new(SitCaseIn {})),
};
sit_expect!(SitCase{}, b"in ", &found_the_in_word);
sit_expect!(SitCase{}, b"in", &flush(0), &found_the_in_word);
Expand Down
8 changes: 5 additions & 3 deletions src/sitcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ impl Situation for SitCmd {
return res;
}
if is_whitespace(a) {
return WhatNow{
tri: Transition::Replace(Box::new(SitArg{end_trigger: self.end_trigger})),
pre: i, len: 1, alt: None
return WhatNow {
transform: (i, 1, None),
transition: Transition::Replace(Box::new(SitArg {
end_trigger: self.end_trigger,
})),
};
}
if a == b'(' {
Expand Down
12 changes: 6 additions & 6 deletions src/sitfor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ fn push_varident(pre: usize, len: usize) -> WhatNow {
}

fn become_for_in_necessarily_array(pre: usize) -> WhatNow {
WhatNow{
tri: Transition::Replace(Box::new(SitVarIdentNecessarilyArray{})),
pre, len: 1, alt: Some(b"\"${"),
WhatNow {
transform: (pre, 1, Some(b"\"${")),
transition: Transition::Replace(Box::new(SitVarIdentNecessarilyArray {})),
}
}

fn become_for_in_anything_else(pre: usize) -> WhatNow {
WhatNow{
tri: Transition::Replace(Box::new(SitForInAnythingElse{})),
pre, len: 0, alt: None,
WhatNow {
transform: (pre, 0, None),
transition: Transition::Replace(Box::new(SitForInAnythingElse {})),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sitmagic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Situation for SitMagic {
}

pub fn push_magic(pre: usize, len: usize, end_trigger: u8) -> WhatNow {
WhatNow{
tri: Transition::Push(Box::new(SitMagic{end_trigger})),
pre, len, alt: None
WhatNow {
transform: (pre, len, None),
transition: Transition::Push(Box::new(SitMagic { end_trigger })),
}
}
3 changes: 2 additions & 1 deletion src/sitstrdq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl Situation for SitStrDq {
}
CommonStrCmdResult::Some(x) |
CommonStrCmdResult::OnlyWithQuotes(x) => {
let progress = x.pre + x.len;
let (pre, len, _) = x.transform;
let progress = pre + len;
if progress != 0 {
self.interpolation_detection = QuotingCtx::Interpolation;
}
Expand Down
8 changes: 4 additions & 4 deletions src/sitstrphantom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Situation for SitStrPhantom {
CommonStrCmdResult::None => {}
CommonStrCmdResult::Some(consult) |
CommonStrCmdResult::OnlyWithQuotes(consult) => {
match consult.tri {
match consult.transition {
Transition::Flush | Transition::FlushPopOnEof => {
if is_horizon_lengthenable {
return flush(0);
Expand Down Expand Up @@ -72,9 +72,9 @@ fn is_phantomstringfood(c: u8) -> bool {
}

fn become_real(pre: usize) -> WhatNow {
WhatNow{
tri: Transition::Replace(Box::new(SitStrDq::new())),
pre, len: 1, alt: Some(b"")
WhatNow {
transform: (pre, 1, Some(b"")),
transition: Transition::Replace(Box::new(SitStrDq::new())),
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/sittest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Situation for SitTest {
if is_emptystringtest || is_nonemptystringtest {
let suggest = common_token(self.end_trigger, horizon, 3, is_horizon_lengthenable);
if let Some(ref exciting) = suggest {
if let Transition::Push(_) = &exciting.tri {
if let Transition::Push(_) = &exciting.transition {
let end_replace: &'static [u8] = if is_emptystringtest {
b" = \"\""
} else {
Expand All @@ -48,10 +48,11 @@ impl Situation for SitTest {
}
} else if prefixlen(horizon, b"x") == 1 {
if let Some(mut suggest) = common_token(self.end_trigger, horizon, 1, is_horizon_lengthenable) {
if let Transition::Push(_) = &suggest.tri {
let transition = std::mem::replace(&mut suggest.tri, Transition::Flush);
if let Transition::Push(_) = &suggest.transition {
let transition = std::mem::replace(&mut suggest.transition, Transition::Flush);
if let Transition::Push(state) = transition {
let progress = suggest.pre + suggest.len;
let (pre, len, _) = suggest.transform;
let progress = pre + len;
if let Ok(found) = find_xyes_comparison(&horizon[progress ..], state) {
if found {
return push_xyes(self.end_trigger);
Expand All @@ -77,9 +78,9 @@ impl Situation for SitTest {
}

fn become_regular_args(end_trigger :u16) -> WhatNow {
WhatNow{
tri: Transition::Replace(Box::new(SitArg{end_trigger})),
pre: 0, len: 0, alt: None
WhatNow {
transform: (0, 0, None),
transition: Transition::Replace(Box::new(SitArg { end_trigger })),
}
}

Expand Down Expand Up @@ -112,12 +113,14 @@ impl Situation for SitHiddenTest {
fn whatnow(&mut self, _horizon: &[u8], _is_horizon_lengthenable: bool) -> WhatNow {
let initial_adventure = std::mem::replace(&mut self.inner, None);
if let Some(mut exciting) = initial_adventure {
exciting.pre = 0;
exciting.transform.0 = 0;
exciting
} else {
WhatNow{
tri: Transition::Replace(Box::new(SitArg{end_trigger: self.end_trigger})),
pre: 0, len: 0, alt: Some(self.end_replace)
WhatNow {
transform: (0, 0, Some(self.end_replace)),
transition: Transition::Replace(Box::new(SitArg {
end_trigger: self.end_trigger,
})),
}
}
}
Expand All @@ -142,10 +145,11 @@ impl Situation for SitXyes {
} else if i > 0 || is_horizon_lengthenable {
return flush(i);
}
return WhatNow{
tri: Transition::Replace(Box::new(SitArg{
return WhatNow {
transform: (i, 1, Some(replacement)),
transition: Transition::Replace(Box::new(SitArg {
end_trigger: self.end_trigger,
})), pre: i, len: 1, alt: Some(replacement)
})),
};
}
if let Some(res) = common_arg(self.end_trigger, horizon, i, is_horizon_lengthenable) {
Expand Down
30 changes: 17 additions & 13 deletions src/situation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,35 @@ pub enum Transition {
}

pub struct WhatNow {
pub tri :Transition,
pub pre :usize,
pub len :usize,
pub alt :Option<&'static [u8]>,
pub transform: (usize, usize, Option<&'static [u8]>), // pre, len, alt
pub transition: Transition,
}

pub fn flush(pre: usize) -> WhatNow {
WhatNow{tri: Transition::Flush, pre, len: 0, alt: None}
WhatNow {
transform: (pre, 0, None),
transition: Transition::Flush,
}
}

pub fn flush_or_pop(pre: usize) -> WhatNow {
WhatNow{tri: Transition::FlushPopOnEof, pre, len: 0, alt: None}
WhatNow {
transform: (pre, 0, None),
transition: Transition::FlushPopOnEof,
}
}

pub fn pop(pre: usize, len: usize, alt: Option<&'static [u8]>) -> WhatNow {
WhatNow{tri: Transition::Pop, pre, len, alt}
WhatNow {
transform: (pre, len, alt),
transition: Transition::Pop,
}
}

pub fn push(transform: (usize, usize, Option<&'static [u8]>), sit: Box<dyn Situation>) -> WhatNow {
let (pre, len, alt) = transform;
WhatNow{
tri: Transition::Push(sit),
pre,
len,
alt,
WhatNow {
transform,
transition: Transition::Push(sit),
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/sitvarident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ pub struct SitVarIdent {
impl Situation for SitVarIdent {
fn whatnow(&mut self, horizon: &[u8], _is_horizon_lengthenable: bool) -> WhatNow {
let len = predlen(is_identifiertail, horizon);
if len < horizon.len() {
return WhatNow{tri: Transition::Pop, pre: len, len: 0, alt: self.end_insert};
}
WhatNow{
tri: Transition::FlushPopOnEof,
pre: horizon.len(), len: 0, alt: self.end_insert
WhatNow {
transform: (len, 0, self.end_insert),
transition: if len < horizon.len() {
Transition::Pop
} else {
Transition::FlushPopOnEof
},
}
}
fn get_color(&self) -> u32 {
Expand Down
6 changes: 3 additions & 3 deletions src/sitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use crate::situation::flush;
use crate::situation::pop;
use crate::situation::Situation;
use crate::situation::Transition;
use crate::situation::WhatNow;
use crate::situation::flush;

pub struct SitVec {
pub terminator :Vec<u8>,
Expand All @@ -26,7 +26,7 @@ impl Situation for SitVec {
}
}
else if horizon[0 .. self.terminator.len()] == self.terminator[..] {
WhatNow{tri: Transition::Pop, pre: 0, len: self.terminator.len(), alt: None}
pop(0, self.terminator.len(), None)
} else {
flush(1)
}
Expand Down
12 changes: 6 additions & 6 deletions src/testhelpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ use crate::situation::Transition::Push;
use crate::situation::Transition::Pop;

pub fn whatnow_eq(a: &WhatNow, b: &WhatNow) -> bool {
if a.pre != b.pre {
eprintln!("WhatNow.pre: {} != {}", a.pre, b.pre);
if a.transform.0 != b.transform.0 {
eprintln!("WhatNow.pre: {} != {}", a.transform.0, b.transform.0);
false
} else if a.len != b.len {
eprintln!("WhatNow.len: {} != {}", a.len, b.len);
} else if a.transform.1 != b.transform.1 {
eprintln!("WhatNow.len: {} != {}", a.transform.1, b.transform.1);
false
} else if a.alt != b.alt {
} else if a.transform.2 != b.transform.2 {
eprintln!("WhatNow.alt mismatch");
false
} else {
transition_eq(&a.tri, &b.tri)
transition_eq(&a.transition, &b.transition)
}
}

Expand Down

0 comments on commit 1d28e6b

Please sign in to comment.