aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs2
-rw-r--r--src/rope.rs49
2 files changed, 28 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index bb3797d..10d7647 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,7 @@ fn main() {
println!("Usage: edit <filename>");
std::process::exit(1);
}
- if let Err(err) = edit(&args[0]) {
+ if let Err(err) = edit(&args[1]) {
println!("FAIL: {}", err);
std::process::exit(1);
}
diff --git a/src/rope.rs b/src/rope.rs
index 437d5e3..f26457c 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -4,7 +4,8 @@ use std::io::{ErrorKind, Read};
use std::path::Path;
use std::rc::Rc;
-const MAX_NODE_SIZE: usize = 512;
+// Set to a small value to help identify bugs in the implementation.
+const MAX_NODE_SIZE: usize = 4;
struct Branch {
len: usize,
@@ -17,40 +18,37 @@ enum Node {
Branch(Branch),
}
-impl Display for Node {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+impl Node {
+ fn len(&self) -> usize {
+ return match self {
+ Node::Leaf(v) => v.len(),
+ Node::Branch(b) => b.len,
+ };
+ }
+
+ fn write(&self, out: &mut Vec<u8>) {
match self {
- Node::Leaf(s) => {
- return write!(f, "{}", String::from_utf8_lossy(s));
+ Node::Leaf(v) => {
+ out.extend(v);
}
Node::Branch(b) => {
- if let Err(err) = write!(f, "{}", b.left) {
- return Err(err);
- }
- return write!(f, "{}", b.right);
+ b.left.write(out);
+ b.right.write(out);
}
}
}
}
-impl Node {
- fn len(&self) -> usize {
- return match self {
- Node::Leaf(v) => v.len(),
- Node::Branch(b) => b.len,
- };
+impl Display for Node {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+ let mut buf = Vec::new();
+ self.write(&mut buf);
+ return write!(f, "{}", String::from_utf8_lossy(&buf));
}
}
pub struct Rope(Rc<Node>);
-impl Display for Rope {
- fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
- let Rope(n) = self;
- return write!(f, "{}", n);
- }
-}
-
impl Rope {
fn concat(self, Rope(other): Rope) -> Rope {
let Rope(me) = self;
@@ -94,3 +92,10 @@ impl Rope {
return Ok(rope);
}
}
+
+impl Display for Rope {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ let Rope(n) = self;
+ return write!(f, "{}", n);
+ }
+}