pub enum Operation<'a> {
Append(&'a str),
Delete(usize),
Print(usize),
Undo,
Invalid,
}
Expand description
Contains all possible operations, as interpreted from commands via input.
Invalid
is a catch-all for any unrecognized commands.
Variants§
Append(&'a str)
The append command, denotated by 1
in the program’s input. The associated &'a str
contains the string data to be appended to the buffer.
use simple_text_editor::ops::*;
let op = "1 append this text".into();
match op {
Operation::Append(val) => {
assert_eq!(val, "append this text");
}
_ => panic!("should have matched Operation::Append"),
}
Delete(usize)
The delete command, denotated by 2
in the program’s input. The associated usize
is the
number of characters to be delete from the back of the buffer.
use simple_text_editor::ops::*;
let op = "2 5".into();
match op {
Operation::Delete(n) => {
assert_eq!(n, 5);
}
_ => panic!("should have matched Operation::Delete"),
}
Print(usize)
The print command, denotated by 3
in the program’s input. The associated usize
is the
1-based index at which the character from the buffer should be printed.
use simple_text_editor::ops::*;
let op = "3 1".into();
match op {
Operation::Print(i) => {
assert_eq!(i, 1);
}
_ => panic!("should have matched Operation::Print"),
}
Undo
The undo command, denotated by 4
in the program’s input. There is no associated data, and
thus simply pops a command from a maintained stack of undo-eligible operations, either being
append or delete.
use simple_text_editor::ops::*;
let op = "4".into();
match op {
Operation::Undo => {
assert!(true);
}
_ => panic!("should have matched Operation::Undo"),
}
Invalid
Invalid is a catch-all for any unrecognized commands, and is ignored by the program.
use simple_text_editor::ops::*;
let op = "__BADOPERATION__".into();
match op {
Operation::Invalid => {
assert!(true);
}
_ => panic!("should have matched Operation::Invalid"),
}
Trait Implementations§
Source§impl<'a> From<&'a str> for Operation<'a>
Convert a line of input into an Operation.
impl<'a> From<&'a str> for Operation<'a>
Convert a line of input into an Operation.
use simple_text_editor::ops::*;
let op = "1 abc".into();
assert!(matches!(op, Operation::Append("abc")));