Skip to content

Commit cc6663a

Browse files
mdbook-slide-evaluator: refactor main content size evaluation into function and remove the actual size from the result
1 parent 7d2507e commit cc6663a

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

mdbook-slide-evaluator/src/evaluator.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ impl From<(f64, f64, f64, f64)> for ElementSize {
6868
pub struct EvaluationResult {
6969
/// metadata about the slide
7070
slide: Slide,
71-
/// the size of the main content element
72-
element_size: ElementSize,
7371
/// all policy violations
7472
policy_violations: Vec<PolicyViolation>,
7573
}
@@ -85,8 +83,6 @@ pub struct EvaluationResults {
8583
#[derive(Serialize)]
8684
struct ExportFormat {
8785
filename: PathBuf,
88-
element_width: usize,
89-
element_height: usize,
9086
policy_violations: String,
9187
}
9288

@@ -113,8 +109,6 @@ impl EvaluationResults {
113109
}
114110
csv_writer.serialize(ExportFormat {
115111
filename: (*result.slide.filename).to_path_buf(),
116-
element_width: result.element_size.width.round() as usize,
117-
element_height: result.element_size.height.round() as usize,
118112
policy_violations: result
119113
.policy_violations
120114
.iter()
@@ -133,10 +127,8 @@ impl EvaluationResults {
133127
continue;
134128
}
135129
println!(
136-
"{}: {}x{} [{}]",
130+
"{}: [{}]",
137131
result.slide.filename.display(),
138-
result.element_size.width,
139-
result.element_size.height,
140132
result
141133
.policy_violations
142134
.iter()
@@ -181,23 +173,6 @@ impl<'a> Evaluator<'_> {
181173
Ok(())
182174
}
183175

184-
/// evaluate the currently opened webpage return the selected content
185-
/// element if available
186-
async fn get_content_element_from_slide(
187-
&self,
188-
) -> anyhow::Result<Option<Element>> {
189-
match self.webclient.find(self.element_selector).await {
190-
Result::Ok(result) => Ok(Some(result)),
191-
Result::Err(fantoccini::error::CmdError::Standard(
192-
fantoccini::error::WebDriver {
193-
error: fantoccini::error::ErrorStatus::NoSuchElement,
194-
..
195-
},
196-
)) => anyhow::Ok(None),
197-
Result::Err(error) => Err(anyhow!(error))?,
198-
}
199-
}
200-
201176
/// extract the element coordinates from this element
202177
async fn get_element_coordinates(
203178
&self,
@@ -235,6 +210,28 @@ impl<'a> Evaluator<'_> {
235210
Ok(())
236211
}
237212

213+
/// evalutes the main element of the page if there is one and returns violations of the policy
214+
async fn eval_main_element(
215+
&self,
216+
slide: &Slide,
217+
) -> anyhow::Result<Vec<PolicyViolation>> {
218+
// get every main content element - should only be one but find_all makes
219+
// the code easier as we just care about violations of any main element
220+
let content_elements =
221+
self.webclient.find_all(self.element_selector).await?;
222+
223+
let mut violations = vec![];
224+
for element in content_elements {
225+
let element_size = self.get_element_coordinates(&element).await?;
226+
violations.append(&mut self.slide_policy.eval_size(&element_size));
227+
if self.screenshot_dir.is_some() {
228+
let screenshot = element.screenshot().await?;
229+
self.store_screenshot(screenshot, &slide.filename)?;
230+
}
231+
}
232+
Ok(violations)
233+
}
234+
238235
/// evaluate a single slide
239236
pub async fn eval_slide(
240237
&self,
@@ -245,21 +242,11 @@ impl<'a> Evaluator<'_> {
245242
let url = self.html_base_url.join(&slide.filename.display().to_string())?;
246243
self.webdriver_open_url(&url).await?;
247244

248-
let Some(content_element) = self.get_content_element_from_slide().await?
249-
else {
250-
return Ok(None);
251-
};
252-
let element_size = self.get_element_coordinates(&content_element).await?;
253-
if self.screenshot_dir.is_some() {
254-
let screenshot = content_element.screenshot().await?;
255-
self.store_screenshot(screenshot, &slide.filename)?;
256-
}
257-
let policy_violations = self.slide_policy.eval_size(&element_size);
258-
let result = EvaluationResult {
259-
slide: slide.clone(),
260-
element_size,
261-
policy_violations,
262-
};
245+
let mut policy_violations = vec![];
246+
// evaluate main content element size
247+
policy_violations.append(&mut self.eval_main_element(slide).await?);
248+
249+
let result = EvaluationResult { slide: slide.clone(), policy_violations };
263250
debug!("information about element: {:?}", result);
264251
Ok(Some(result))
265252
}

0 commit comments

Comments
 (0)