Refactor the schema for granularity

This commit is contained in:
Ayaz Hafiz 2023-07-16 13:54:10 -05:00
parent 358681a464
commit 271d2a3219
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
4 changed files with 649 additions and 656 deletions

View file

@ -1,9 +1,9 @@
use std::fs;
use roc_checkmate_schema::Event;
use roc_checkmate_schema::AllEvents;
fn main() {
let schema = Event::schema();
let schema = AllEvents::schema();
fs::write(
"schema.json",
serde_json::to_string_pretty(&schema).unwrap(),

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,10 @@
use roc_checkmate_schema::{Event, VariableEvent};
use roc_checkmate_schema::{AllEvents, Event, VariableEvent};
use roc_types::subs as s;
use crate::convert::AsSchema;
pub struct Collector {
events: Event,
events: AllEvents,
current_event_path: Vec<usize>,
}
@ -17,7 +17,7 @@ impl Default for Collector {
impl Collector {
pub fn new() -> Self {
Self {
events: Event::Top(Vec::new()),
events: AllEvents(Vec::new()),
current_event_path: Vec::new(),
}
}
@ -88,12 +88,12 @@ impl Collector {
) {
let current_event = self.get_path_event();
match current_event {
Event::Unification {
EventW::Sub(Event::Unification {
left: l,
right: r,
success: s,
..
} => {
}) => {
assert_eq!(left.as_schema(subs), *l);
assert_eq!(right.as_schema(subs), *r);
assert!(s.is_none());
@ -101,13 +101,14 @@ impl Collector {
}
_ => panic!("end_unification called when not in a unification"),
}
assert!(matches!(current_event, Event::Unification { .. }));
self.current_event_path.pop();
}
fn add_event(&mut self, event: impl Into<Event>) {
let event = event.into();
let is_appendable = event.appendable();
let mut event = event.into();
let is_appendable = EventW::Sub(&mut event).appendable();
let event = event;
let path_event = self.get_path_event();
let new_event_index = path_event.append(event);
if is_appendable {
@ -115,11 +116,52 @@ impl Collector {
}
}
fn get_path_event(&mut self) -> &mut Event {
let mut event = &mut self.events;
fn get_path_event(&mut self) -> EventW {
let mut event = EventW::Top(&mut self.events);
for i in &self.current_event_path {
event = event.index(*i);
}
event
}
}
enum EventW<'a> {
Top(&'a mut AllEvents),
Sub(&'a mut Event),
}
impl<'a> EventW<'a> {
fn append(self, event: Event) -> usize {
let list = self.subevents_mut().unwrap();
let index = list.len();
list.push(event);
index
}
fn appendable(self) -> bool {
self.subevents().is_some()
}
fn index(self, index: usize) -> EventW<'a> {
Self::Sub(&mut self.subevents_mut().unwrap()[index])
}
}
impl<'a> EventW<'a> {
fn subevents(self) -> Option<&'a Vec<Event>> {
use EventW::*;
match self {
Top(events) => Some(&events.0),
Sub(Event::Unification { subevents, .. }) => Some(subevents),
Sub(Event::VariableEvent(_)) => None,
}
}
fn subevents_mut(self) -> Option<&'a mut Vec<Event>> {
use EventW::*;
match self {
Top(events) => Some(&mut events.0),
Sub(Event::Unification { subevents, .. }) => Some(subevents),
Sub(Event::VariableEvent(_)) => None,
}
}
}

View file

@ -4,6 +4,7 @@ use schemars::{schema::RootSchema, schema_for, JsonSchema};
use serde::Serialize;
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum Constraint {}
#[derive(Serialize, JsonSchema, Debug, PartialEq)]
@ -12,6 +13,7 @@ pub struct Variable(pub u32);
macro_rules! impl_content {
($($name:ident { $($arg:ident: $ty:ty,)* },)*) => {
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum Content {
$(
$name {
@ -116,6 +118,7 @@ pub struct UnspecializedClosureType {
}
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum AliasKind {
Structural,
Opaque,
@ -135,7 +138,7 @@ pub struct RecordField {
}
#[derive(Serialize, JsonSchema)]
#[serde(tag = "kind")]
#[serde(tag = "type")]
pub enum RecordFieldKind {
Demanded,
Required { rigid: bool },
@ -143,7 +146,7 @@ pub enum RecordFieldKind {
}
#[derive(Serialize, JsonSchema)]
#[serde(tag = "kind")]
#[serde(tag = "type")]
pub enum TagUnionExtension {
Openness(Variable),
Any(Variable),
@ -157,6 +160,7 @@ pub struct NumericRange {
}
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum NumericRangeKind {
Int,
AnyNum,
@ -178,6 +182,7 @@ pub struct Symbol(
);
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum UnificationMode {
Eq,
Present,
@ -185,8 +190,8 @@ pub enum UnificationMode {
}
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum Event {
Top(Vec<Event>),
VariableEvent(VariableEvent),
Unification {
left: Variable,
@ -198,6 +203,10 @@ pub enum Event {
}
#[derive(Serialize, JsonSchema)]
pub struct AllEvents(pub Vec<Event>);
#[derive(Serialize, JsonSchema)]
#[serde(tag = "type")]
pub enum VariableEvent {
Unify {
from: Variable,
@ -216,47 +225,8 @@ impl From<VariableEvent> for Event {
}
}
impl Event {
pub fn append(&mut self, event: Event) -> usize {
let list = self.subevents_mut().unwrap();
let index = list.len();
list.push(event);
index
}
pub fn appendable(&self) -> bool {
self.subevents().is_some()
}
pub fn index(&mut self, index: usize) -> &mut Event {
&mut self.subevents_mut().unwrap()[index]
}
impl AllEvents {
pub fn schema() -> RootSchema {
schema_for!(Event)
schema_for!(AllEvents)
}
}
macro_rules! impl_subevents {
($($pat:pat => $body:expr,)*) => {
impl Event {
fn subevents(&self) -> Option<&Vec<Event>> {
match self {$(
$pat => $body,
)*}
}
fn subevents_mut(&mut self) -> Option<&mut Vec<Event>> {
match self {$(
$pat => $body,
)*}
}
}
};
}
impl_subevents! {
Event::Top(events) => Some(events),
Event::Unification { subevents, .. } => Some(subevents),
Event::VariableEvent(_) => None,
}