use super::types::DiagnosticProjection;
use super::{
IntrinsicRefused, Line, LineBody, LineSite, Placement, Refused, RelatedSet, Route, Site,
SiteCoordinate,
};
use crate::bounded::Capping;
use crate::request::Door;
use crate::token::CoordinateRole;
const fn coordinate_role_word(role: CoordinateRole) -> &'static str {
match role {
CoordinateRole::Byte => "byte",
CoordinateRole::SemanticOrigin => "semantic-origin position",
}
}
fn body_clause(body: LineBody) -> String {
let (further, capping) = match body {
LineBody::SingleCause => return String::new(),
LineBody::Body { further, capping } => (further, capping),
};
let more = if further > 0 {
format!(" (and {further} further established issues)")
} else {
String::new()
};
let kept = match capping {
Capping::Complete => String::new(),
Capping::Truncated { omitted } => format!(
" (every issue was established; {omitted} of them do not fit the declared issue bound)"
),
};
format!("{more}{kept}")
}
fn site_clause(site: LineSite) -> String {
match site {
LineSite::WholeDeclaration => String::new(),
LineSite::At(SiteCoordinate::Resolved(coordinate)) => {
let word = coordinate_role_word(coordinate.role);
let position = coordinate.position;
format!(" (at {word} {position})")
}
LineSite::At(SiteCoordinate::NotReached(refusal)) => format!(" ({refusal})"),
}
}
#[must_use]
pub fn composed(door: &Door, line: &Line<'_>, site: LineSite) -> String {
let prefix = door.prefix();
let class = line.class.described();
let first = line.first;
let stated_body = body_clause(line.body);
let stated_site = site_clause(site);
format!("{prefix}: {class}: {first}{stated_body}{stated_site}")
}
pub(crate) fn witnessed(line: &str, capping: Capping) -> String {
match capping {
Capping::Complete => line.to_owned(),
Capping::Truncated { omitted } => format!(
"{line} (the related set was capped at the declared issue bound: one identity over the \
complete body is carried and {omitted} per-issue identities are not)"
),
}
}
pub(super) fn diagnostic<E: Refused>(refusal: &E, door: &Door, site: Site) -> DiagnosticProjection {
let related = RelatedSet::derived_over(E::FAMILY, &refusal.related());
let first = refusal.first();
let line = Line {
class: refusal.class(),
first: &first,
body: refusal.body(),
};
let composed_line = composed(door, &line, line_site(site));
DiagnosticProjection {
phase: E::PHASE,
site,
observed: refusal.observed(),
summary: witnessed(&composed_line, related.capping()),
expected: door.grammar(),
related,
repairs: refusal.repairs(),
route: Route::through(door.entry()),
}
}
pub(super) fn placement_site(placement: &Placement<'_>) -> Site {
match *placement {
Placement::WholeDeclaration => Site::whole_declaration(),
Placement::AtToken { token, spans } => {
Site::at_token(token, SiteCoordinate::answered(spans.coordinate_of(token)))
}
}
}
pub(super) fn intrinsic_site<E: IntrinsicRefused>(refusal: &E) -> Site {
refusal.site()
}
fn line_site(site: Site) -> LineSite {
match site {
Site::WholeDeclaration => LineSite::WholeDeclaration,
Site::AtToken { coordinate, .. } => LineSite::At(coordinate),
Site::BeforeCapture { coordinate } => LineSite::At(SiteCoordinate::Resolved(coordinate)),
}
}