Skip to content

Commit

Permalink
mdbook-slide-evaluator: better webclient error handling.
Browse files Browse the repository at this point in the history
Errors previously have been ignored and next slide was evaluated
  • Loading branch information
michael-kerscher committed Aug 9, 2024
1 parent 3247d3b commit 152c292
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 25 additions & 12 deletions mdbook-slide-evaluator/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Ok};
use anyhow::anyhow;
use fantoccini::elements::Element;
use fantoccini::Client;
use log::{debug, info, warn};
use log::{debug, warn};
use serde::Serialize;
use tokio_util::sync::CancellationToken;
use url::Url;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct EvaluationResult {
/// holds all evaluation results for a book
pub struct EvaluationResults {
/// metadata about the book
book: Book,
_book: Book,
/// the collected evaluation results
results: Vec<EvaluationResult>,
}
Expand Down Expand Up @@ -151,10 +151,20 @@ impl<'a> Evaluator<'_> {
}

/// evaluate the currently opened webpage return the selected content
/// element
async fn get_content_element_from_slide(&self) -> anyhow::Result<Element> {
let result = self.webclient.find(self.element_selector).await?;
Ok(result)
/// element if available
async fn get_content_element_from_slide(
&self,
) -> anyhow::Result<Option<Element>> {
match self.webclient.find(self.element_selector).await {
Result::Ok(result) => Ok(Some(result)),
Result::Err(fantoccini::error::CmdError::Standard(
fantoccini::error::WebDriver {
error: fantoccini::error::ErrorStatus::NoSuchElement,
..
},
)) => anyhow::Ok(None),
Result::Err(error) => Err(anyhow!(error))?,
}
}

/// extract the element coordinates from this element
Expand Down Expand Up @@ -198,21 +208,24 @@ impl<'a> Evaluator<'_> {
pub async fn eval_slide(
&self,
slide: &Slide,
) -> anyhow::Result<EvaluationResult> {
) -> anyhow::Result<Option<EvaluationResult>> {
debug!("evaluating {:?}", slide);

let url = self.html_base_url.join(&slide.filename.display().to_string())?;
self.webdriver_open_url(&url).await?;

let content_element = self.get_content_element_from_slide().await?;
let Some(content_element) = self.get_content_element_from_slide().await?
else {
return Ok(None);
};
let size = self.get_element_coordinates(&content_element).await?;
if self.screenshot_dir.is_some() {
let screenshot = content_element.screenshot().await?;
self.store_screenshot(screenshot, &slide.filename)?;
}
let result = EvaluationResult { slide: slide.clone(), element_size: size };
debug!("information about element: {:?}", result);
Ok(result)
Ok(Some(result))
}

/// evaluate an entire book
Expand All @@ -224,12 +237,12 @@ impl<'a> Evaluator<'_> {
debug!("received cancel request, return already completed results");
break;
}
let Result::Ok(result) = self.eval_slide(slide).await else {
let Some(result) = self.eval_slide(slide).await? else {
warn!("slide with no content - ignore: {:?}", slide);
continue;
};
results.push(result);
}
Ok(EvaluationResults { book, results })
Ok(EvaluationResults { _book: book, results })
}
}
4 changes: 2 additions & 2 deletions mdbook-slide-evaluator/src/slides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Slide {
/// a book is a collection of slides
pub struct Book {
/// the path to the root directory of this book
source_dir: PathBuf,
_source_dir: PathBuf,
/// the collection of slides
slides: Vec<Slide>,
}
Expand All @@ -44,7 +44,7 @@ impl Book {
debug!("add {:?}", slide);
slides.push(slide);
}
Ok(Book { source_dir, slides })
Ok(Book { _source_dir: source_dir, slides })
}

/// return a reference to the slides of this book
Expand Down

0 comments on commit 152c292

Please sign in to comment.