use super::{NativeSyntax, NativeTemplateFile};
use crate::file::{FileArtifact, ScopeId, ScriptUnit, ScriptUnitId};
use oxc_parser::AdmittedProgram;
use vize_l0::Span;
use vize_l1::markup::NativeScriptSelection;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSetupIssueKind {
MissingOwner,
IncompleteFile,
Source,
Profile,
MissingUnit,
ProgramOrigin,
EmptyProgram,
Scope,
UnsupportedSyntax,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeSetupIssue {
pub span: Span,
pub kind: NativeSetupIssueKind,
}
pub struct NativeSelectedSetup<'owner, 'arena> {
owner: &'owner NativeTemplateFile<'arena>,
syntax: &'owner NativeSyntax<'arena>,
selected: NativeScriptSelection<'owner, 'arena>,
program: AdmittedProgram<'owner, 'arena>,
unit: &'owner ScriptUnit,
file: &'owner FileArtifact<'arena>,
}
impl<'owner, 'arena> NativeSelectedSetup<'owner, 'arena> {
#[must_use]
pub fn owner(&self) -> &'owner NativeTemplateFile<'arena> {
self.owner
}
#[must_use]
pub fn syntax(&self) -> &'owner NativeSyntax<'arena> {
self.syntax
}
#[must_use]
pub fn selected(&self) -> &NativeScriptSelection<'owner, 'arena> {
&self.selected
}
#[must_use]
pub fn program(&self) -> &AdmittedProgram<'owner, 'arena> {
&self.program
}
#[must_use]
pub fn unit(&self) -> ScriptUnitId {
self.unit.id
}
#[must_use]
pub fn unit_record(&self) -> &'owner ScriptUnit {
self.unit
}
#[must_use]
pub fn scope(&self) -> ScopeId {
self.unit.scope
}
#[must_use]
pub fn file(&self) -> &'owner FileArtifact<'arena> {
self.file
}
}
pub(super) fn checked<'owner, 'arena>(
owner: &'owner NativeTemplateFile<'arena>,
) -> Result<NativeSelectedSetup<'owner, 'arena>, NativeSetupIssue> {
use NativeSetupIssueKind as Kind;
let span = owner.selected().component().block().span();
let reject = |kind| NativeSetupIssue { span, kind };
let syntax = owner
.retained_setup()
.ok_or_else(|| reject(Kind::MissingOwner))?;
let selected = owner
.selected()
.setup()
.ok_or_else(|| reject(Kind::MissingUnit))?;
let span = selected.block().span();
let reject = |kind| NativeSetupIssue { span, kind };
owner.view().map_err(|_| reject(Kind::IncompleteFile))?;
let file = owner.file().ok_or_else(|| reject(Kind::IncompleteFile))?;
let joined = super::identity::checked(
owner.selected(),
syntax,
file.units(),
file.scopes(),
file.artifact().source(),
)?;
Ok(NativeSelectedSetup {
owner,
syntax,
selected: joined.selected,
program: joined.program,
unit: joined.unit,
file,
})
}