muffy/
document_output.rs

1use crate::{Metrics, element_output::ElementOutput};
2use serde::Serialize;
3use url::Url;
4
5/// A document output.
6#[derive(Debug, Serialize)]
7pub struct DocumentOutput {
8    url: Url,
9    elements: Vec<ElementOutput>,
10    metrics: Metrics,
11}
12
13impl DocumentOutput {
14    /// Creates a document output.
15    pub fn new(url: Url, elements: Vec<ElementOutput>) -> Self {
16        Self {
17            url,
18            metrics: Metrics::new(
19                elements
20                    .iter()
21                    .flat_map(ElementOutput::results)
22                    .filter(|result| result.is_ok())
23                    .count(),
24                elements
25                    .iter()
26                    .flat_map(ElementOutput::results)
27                    .filter(|result| result.is_err())
28                    .count(),
29            ),
30            elements,
31        }
32    }
33
34    /// Returns a URL.
35    pub const fn url(&self) -> &Url {
36        &self.url
37    }
38
39    /// Returns elements with their validation results.
40    pub fn elements(&self) -> impl Iterator<Item = &ElementOutput> {
41        self.elements.iter()
42    }
43
44    /// Returns metrics of document validation.
45    pub const fn metrics(&self) -> Metrics {
46        self.metrics
47    }
48}