mod document;
mod finding;
mod lints;
mod rules;
mod size;
pub use document::{Document, DuplicateKey};
pub use finding::{Finding, Report, Rule, Severity};
pub use size::{
OVERHEAD_TOKENS, QUESTION_BUDGET_TOKENS, SizeEstimate, TOTAL_BUDGET_TOKENS,
estimate_text_tokens, estimate_value_tokens,
};
use crate::request::Request;
pub const MAX_CHOICE_OPTIONS: usize = 255;
pub const MIN_SCORE_LEVELS: usize = 2;
pub const MAX_SCORE_LEVELS: usize = 10;
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Options {
strict: bool,
skip_size_check: bool,
model_optional: bool,
state_optional: bool,
}
impl Options {
#[must_use]
pub const fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
#[must_use]
pub const fn skip_size_check(mut self, skip: bool) -> Self {
self.skip_size_check = skip;
self
}
#[must_use]
pub const fn model_optional(mut self, optional: bool) -> Self {
self.model_optional = optional;
self
}
#[must_use]
pub const fn state_optional(mut self, optional: bool) -> Self {
self.state_optional = optional;
self
}
#[must_use]
pub const fn is_strict(&self) -> bool {
self.strict
}
}
#[must_use]
pub fn check_document(document: &Document, options: &Options) -> Report {
let mut findings = Vec::new();
let mut size = None;
if let Some(request) = rules::check_shape(document, options, &mut findings) {
lints::check(&request, &mut findings);
if let (false, Some(state)) = (options.skip_size_check, request.state) {
let questions = request
.questions
.iter()
.map(|question| (question.id, question.value));
let estimate = SizeEstimate::of(state, questions);
rules::check_size(&estimate, &mut findings);
size = Some(estimate);
}
}
if options.strict {
for finding in &mut findings {
finding.severity = Severity::Error;
}
}
Report { findings, size }
}
#[must_use]
pub fn check(request: &Request, options: &Options) -> Report {
check_document(&Document::from(request), options)
}