Skip to content

Commit

Permalink
Return an Option in vertebra_coordinates_with
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Apr 16, 2019
1 parent 8567f92 commit cdfb35e
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/document/epub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,35 +167,31 @@ impl EpubDocument {
self.offset(self.spine.len())
}

fn vertebra_coordinates_with<F>(&self, test: F) -> (usize, usize) where F: Fn(usize, usize) -> bool {
fn vertebra_coordinates_with<F>(&self, test: F) -> Option<(usize, usize)>
where F: Fn(usize, usize) -> bool {
let mut start_offset = 0;
let mut end_offset = start_offset;
let mut index = 0;

while index < self.spine.len() {
end_offset += self.spine[index].size;
if test(index, end_offset) {
break;
return Some((index, start_offset))
}
start_offset = end_offset;
index += 1;
}

if index == self.spine.len() {
index -= 1;
start_offset -= self.spine[index].size;
}

(index, start_offset)
None
}

fn vertebra_coordinates(&self, offset: usize) -> (usize, usize) {
fn vertebra_coordinates(&self, offset: usize) -> Option<(usize, usize)> {
self.vertebra_coordinates_with(|_, end_offset| {
offset < end_offset
})
}

fn vertebra_coordinates_from_name(&self, name: &str) -> (usize, usize) {
fn vertebra_coordinates_from_name(&self, name: &str) -> Option<(usize, usize)> {
self.vertebra_coordinates_with(|index, _| {
self.spine[index].path == name
})
Expand Down Expand Up @@ -296,7 +292,7 @@ impl EpubDocument {
let frag_index_opt = uri.find('#');
let name = &uri[..frag_index_opt.unwrap_or_else(|| uri.len())];

let (index, start_offset) = self.vertebra_coordinates_from_name(name);
let (index, start_offset) = self.vertebra_coordinates_from_name(name)?;

if frag_index_opt.is_some() {
let mut text = String::new();
Expand Down Expand Up @@ -335,7 +331,7 @@ impl EpubDocument {
}

let offset = self.resolve_location(loc)?;
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;

self.cache.get(&index).map(|display_list| {
Expand Down Expand Up @@ -1862,7 +1858,7 @@ impl Document for EpubDocument {
fn chapter<'a>(&mut self, offset: usize, toc: &'a [TocEntry]) -> Option<&'a TocEntry> {
let next_offset = self.resolve_location(Location::Next(offset))
.unwrap_or(usize::max_value());
let (index, _) = self.vertebra_coordinates(offset);
let (index, _) = self.vertebra_coordinates(offset)?;
let path = self.spine[index].path.clone();
let mut chap_before = None;
let mut chap_after = None;
Expand Down Expand Up @@ -1902,14 +1898,14 @@ impl Document for EpubDocument {

match loc {
Location::Exact(offset) => {
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;
self.cache.get(&index)
.and_then(|display_list| display_list[page_index].first())
.map(|dc| dc.offset())
},
Location::Previous(offset) => {
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;
if page_index > 0 {
self.cache.get(&index)
Expand All @@ -1928,7 +1924,7 @@ impl Document for EpubDocument {
}
},
Location::Next(offset) => {
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;
if page_index < self.cache.get(&index).map(|display_list| display_list.len())? - 1 {
self.cache.get(&index).and_then(|display_list| display_list[page_index+1].first().map(|dc| dc.offset()))
Expand All @@ -1946,12 +1942,9 @@ impl Document for EpubDocument {
}
},
Location::LocalUri(offset, ref uri) => {
if uri.starts_with("http://") || uri.starts_with("https://") {
return None;
}
let mut cache = FnvHashMap::default();
let normalized_uri: String = {
let (index, _) = self.vertebra_coordinates(offset);
let (index, _) = self.vertebra_coordinates(offset)?;
let path = &self.spine[index].path;
if uri.starts_with('#') {
format!("{}{}", path, uri)
Expand All @@ -1965,9 +1958,6 @@ impl Document for EpubDocument {
self.resolve_link(&normalized_uri, &mut cache)
},
Location::Uri(ref uri) => {
if uri.starts_with("http://") || uri.starts_with("https://") {
return None;
}
let mut cache = FnvHashMap::default();
self.resolve_link(uri, &mut cache)
},
Expand All @@ -1980,7 +1970,7 @@ impl Document for EpubDocument {
}

let offset = self.resolve_location(loc)?;
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;

self.cache.get(&index).map(|display_list| {
Expand Down Expand Up @@ -2008,7 +1998,7 @@ impl Document for EpubDocument {
}

let offset = self.resolve_location(loc)?;
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;
let page_index = self.page_index(offset, index, start_offset)?;

self.cache.get(&index).map(|display_list| {
Expand All @@ -2033,7 +2023,7 @@ impl Document for EpubDocument {
}

let offset = self.resolve_location(loc)?;
let (index, start_offset) = self.vertebra_coordinates(offset);
let (index, start_offset) = self.vertebra_coordinates(offset)?;

let page_index = self.page_index(offset, index, start_offset)?;
let page = self.cache.get(&index)?.get(page_index)?.clone();
Expand Down

0 comments on commit cdfb35e

Please sign in to comment.