Skip to content

Commit

Permalink
add rune command
Browse files Browse the repository at this point in the history
  • Loading branch information
geremachek committed Jan 16, 2022
1 parent 06adccf commit 2fdb7b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum Command {
Bottle,
Disenchant,
Smash,
Rune,
Merlin,
Summon,
Dub,
Expand Down Expand Up @@ -98,6 +99,7 @@ impl FromStr for Command {
"bottle" => Ok(Command::Bottle),
"disenchant" => Ok(Command::Disenchant),
"smash" => Ok(Command::Smash),
"rune" => Ok(Command::Rune),
"merlin" => Ok(Command::Merlin),
"summon" => Ok(Command::Summon),
"dub" => Ok(Command::Dub),
Expand Down Expand Up @@ -137,7 +139,7 @@ impl Command {
Command::Volumes | Command::Carved | Command::Atoms | Command::Tether | Command::Stitch | Command::Fray => 0,
Command::Focus | Command::Traverse | Command::Appear | Command::Shave | Command::Shelve | Command::Inscribe | Command::Trample | Command::Incant |
Command::Summon | Command::Dub | Command::Spellbook | Command::Shift | Command::Infix | Command::Spine | Command::Merlin | Command::Disenchant |
Command::Smash | Command::Decant => 1,
Command::Smash | Command::Decant | Command::Rune => 1,
Command::Infuse | Command::Peer | Command::Defuse => 2,
Command::Bottle => choose_mm(2, 1),
Command::Genesis => choose_mm(1, 0),
Expand Down
39 changes: 23 additions & 16 deletions src/plane/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ impl Plane {
fn run_command(&mut self, command: Command, mut data: Vec<String>) -> Result<Option<String>, MerlinError> {
match command { // check what command is being used
Command::Genesis => if data.len() > 0 { self.genesis(&data[0]); } else { self.genesis(""); },
Command::Spine => return Ok(Some(self.spine(parse_pos::<usize>(&data[0])?)?)),
Command::Incant => return Ok(Some(commands::incant(&data[0])?)),
Command::Decant => return Ok(Some(commands::decant(&data[0])?)),
Command::Infuse => return Ok(Some(commands::infuse(&data[0], &data[1])?)),
Command::Defuse => return Ok(Some(commands::defuse(&data[0], &data[1])?)),
Command::Spine => return ok_some(self.spine(parse_pos::<usize>(&data[0])?)?),
Command::Incant => return ok_some(commands::incant(&data[0])?),
Command::Decant => return ok_some(commands::decant(&data[0])?),
Command::Infuse => return ok_some(commands::infuse(&data[0], &data[1])?),
Command::Defuse => return ok_some(commands::defuse(&data[0], &data[1])?),
Command::Molecule => self.stack.molecule(),
Command::Pen => self.stack.pen(),
Command::Orbit => self.stack.orbit()?,
Expand All @@ -134,6 +134,7 @@ impl Plane {
}
Command::Disenchant => self.disenchant(&data[0])?,
Command::Smash => self.smash(&data[0])?,
Command::Rune => return ok_some(self.rune(&data[0])?),
Command::Merlin => {
// make sure we are always *starting* in atom mode, but preserving the original mode
// for when we finish parsing
Expand All @@ -151,29 +152,29 @@ impl Plane {
}
Command::Summon => self.summon(&data[0])?,
Command::Spellbook => self.spellbook(&data[0])?,
Command::Volume => return Ok(Some(self.volume().to_string())),
Command::Volumes => return Ok(Some(self.volumes.len().to_string())),
Command::Atoms => return Ok(Some(self.stack.len().to_string())),
Command::Volume => return ok_some(self.volume().to_string()),
Command::Volumes => return ok_some(self.volumes.len().to_string()),
Command::Atoms => return ok_some(self.stack.len().to_string()),
_ => { // the following commands require buffers to be open
if self.volumes.len() > 0 { // buffers / files are open
let cvol = &mut self.volumes[self.current_volume]; // current volume

match command {
Command::Shelve => self.shelve(parse_pos::<usize>(&data[0])?)?,
Command::Focus => self.focus(parse_pos::<usize>(&data[0])?)?,
Command::Spot => return Ok(Some(cvol.spot().to_string())),
Command::Span => return Ok(Some(cvol.span().to_string())),
Command::Pin => return Ok(Some(cvol.pin().to_string())),
Command::Columns => return Ok(Some(cvol.columns().to_string())),
Command::Spot => return ok_some(cvol.spot().to_string()),
Command::Span => return ok_some(cvol.span().to_string()),
Command::Pin => return ok_some(cvol.pin().to_string()),
Command::Columns => return ok_some(cvol.columns().to_string()),
Command::Traverse => cvol.traverse(parse_pos::<isize>(&data[0])?),
Command::Shift => cvol.shift(parse_pos::<isize>(&data[0])?),
Command::Appear => cvol.appear(parse_pos::<usize>(&data[0])?),
Command::Infix => cvol.infix(parse_pos::<usize>(&data[0])?),
Command::Peer => return Ok(Some(cvol.peer(parse_pos::<usize>(&data[0])?,
parse_pos::<usize>(&data[1])?)?)),
Command::Peer => return ok_some(cvol.peer(parse_pos::<usize>(&data[0])?,
parse_pos::<usize>(&data[1])?)?),
Command::Dub => cvol.dub(&data[0])?,
Command::Carve => cvol.carve()?,
Command::Carved => return Ok(Some(cvol.carved())),
Command::Carved => return ok_some(cvol.carved()),
_ => { // we are modifying the buffer...
cvol.written = false;

Expand All @@ -196,7 +197,7 @@ impl Plane {
};

if n == 0 {
return Ok(Some("0".to_string()));
return ok_some("0".to_string());
}

return Err(MerlinError::NoVolumes);
Expand All @@ -213,3 +214,9 @@ impl Plane {
fn parse_pos<T: std::str::FromStr>(s: &str) -> Result<T, MerlinError> {
s.trim().parse::<T>().or(Err(MerlinError::InvalidSyntax))
}

// wrap ok and some

fn ok_some<T, E>(v: T) -> Result<Option<T>, E> {
Ok(Some(v))
}
8 changes: 8 additions & 0 deletions src/plane/plane_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,13 @@ impl Plane {
_ => self.current_volume + 1,
}
}

// return the expansion of a nomen listing

pub fn rune(&self, nomen: &str) -> Result<String, MerlinError> {
Ok(self.nomens.get(nomen)
.ok_or(MerlinError::UnknownNomen)?
.join(" "))
}
}

0 comments on commit 2fdb7b2

Please sign in to comment.