aboutsummaryrefslogtreecommitdiffstats
path: root/src/rope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rope.rs')
-rw-r--r--src/rope.rs64
1 files changed, 8 insertions, 56 deletions
diff --git a/src/rope.rs b/src/rope.rs
index ea37e6c..c5e9aa1 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::{ErrorKind, Read, Write};
use std::path::Path;
+use std::error::Error;
use std::rc::Rc;
// Set to a small value to help identify bugs in the implementation.
@@ -167,13 +168,8 @@ impl Rope {
}
}
- pub fn open(path: &Path) -> Result<Rope, String> {
- let mut f = match File::open(path) {
- Ok(f) => f,
- Err(err) => {
- return Err(format!("open file {}: {}", path.display(), err));
- }
- };
+ pub fn open(path: &Path) -> Result<Rope, Box<dyn Error>> {
+ let mut f = File::open(path).map_err(|err| format!("open file {}: {}", path.display(), err))?;
let mut rope = Rope::leaf(Vec::new());
loop {
let mut buf = vec![0; MAX_NODE_SIZE];
@@ -183,7 +179,7 @@ impl Rope {
if err.kind() == ErrorKind::Interrupted {
continue;
}
- return Err(format!("read file {}: {}", path.display(), err));
+ return Err(Box::from(format!("read file {}: {}", path.display(), err)));
}
};
if n == 0 {
@@ -196,19 +192,10 @@ impl Rope {
return Ok(rope);
}
- pub fn save(&self, path: &Path) -> Result<(), String> {
- let mut f = match File::create(path) {
- Ok(f) => f,
- Err(err) => {
- return Err(format!("save: create file {}: {}", path.display(), err));
- }
- };
- if let Err(err) = self.print(&mut f) {
- return Err(format!("save: write file {}: {}", path.display(), err));
- }
- if let Err(err) = f.sync_all() {
- return Err(format!("save: write file {}: {}", path.display(), err));
- }
+ pub fn save(&self, path: &Path) -> Result<(), Box<dyn Error>> {
+ let mut f = File::create(path).map_err(|err| format!("save: create file {}: {}", path.display(), err))?;
+ self.print(&mut f).map_err(|err| format!("save: write file {}: {}", path.display(), err))?;
+ f.sync_all().map_err(|err| format!("save: write file {}: {}", path.display(), err))?;
return Ok(());
}
@@ -260,41 +247,6 @@ impl Rope {
return self.slice(start, end-1);
}
- pub fn char(&self, n: usize) -> Result<usize, usize> {
- match self {
- Rope(Node::Leaf(l)) => {
- let mut count = 0;
- for (i, &b) in l.bytes().iter().enumerate() {
- if b & 0xc0 == 0x80 {
- continue;
- }
- if count == n {
- return Ok(i);
- }
- count += 1;
- }
- return Err(count);
- }
- Rope(Node::Branch(b)) => {
- match b.left.char(n) {
- Ok(i) => {
- return Ok(i);
- }
- Err(len) => {
- match b.right.char(n - len) {
- Ok(i) => {
- return Ok(b.left.len() + i);
- }
- Err(right_len) => {
- return Err(len + right_len);
- }
- }
- }
- }
- }
- }
- }
-
fn is_char_boundary(&self, index: usize) -> bool {
match self {
Rope(Node::Leaf(l)) => {