Skip to content

Commit

Permalink
feat: poc v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Oct 31, 2023
1 parent 6681743 commit 694783f
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 159 deletions.
28 changes: 28 additions & 0 deletions crates/biome_deserialize/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,31 @@ impl Advices for DeserializationAdvice {
Ok(())
}
}

#[derive(Default)]
pub struct DeserializationDiagnostics {
pub diagnostics: Vec<DeserializationDiagnostic>,
range: Option<TextRange>,
}

impl DeserializationDiagnostics {
pub fn set_range(&mut self, range: TextRange) {
self.range = Some(range);
}

pub fn report_incorrect_type(&mut self, _expected_type: impl Display) {
todo!();
}

pub fn report_unknown_key(&mut self, _allowed_keys: &[&str]) {
todo!();
}

pub fn report_unknown_variant(&mut self, _allowed_variants: &[&str]) {
todo!();
}

pub fn report_number_out_of_bounds(&mut self, _min: i64, _max: u64) {
todo!()
}
}
54 changes: 28 additions & 26 deletions crates/biome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
DeserializableLanguage, DeserializationDiagnostic, Deserialized, NodeVisitor, VisitNode,
diagnostics::DeserializationDiagnostics, DeserializableLanguage, DeserializationDiagnostic,
DeserializationVisitor, Deserialized, VisitNode,
};
use biome_console::markup;
use biome_diagnostics::{DiagnosticExt, Error};
Expand Down Expand Up @@ -628,33 +629,40 @@ where
}

impl DeserializableLanguage for JsonLanguage {
fn deserialize(
visitor: &mut impl NodeVisitor<JsonLanguage>,
value: SyntaxNode<JsonLanguage>,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) {
fn deserialize_value<V: DeserializationVisitor<Self>>(
value: SyntaxNode<Self>,
diagnostics: &mut DeserializationDiagnostics,
) -> Option<V> {
let range = value.text_trimmed_range();
let Some(value) = AnyJsonValue::cast(value) else {
return;
};
let value = AnyJsonValue::cast(value)?;
diagnostics.set_range(range);
match value {
AnyJsonValue::JsonArrayValue(array) => {
let items = array
.elements()
.into_iter()
.filter_map(|x| x.ok())
.map(|x| x.into_syntax());
visitor.visit_array(items, range, diagnostics)
V::visit_array(items, diagnostics)
}
AnyJsonValue::JsonBogusValue(_) => visitor.fallback_on_error(range, diagnostics),
AnyJsonValue::JsonBogusValue(_) => V::fallback(diagnostics),
AnyJsonValue::JsonBooleanValue(value) => {
let Ok(value) = value.value_token() else {
return;
};
visitor.visit_bool(value.kind() == T![true], range, diagnostics)
let value = value.value_token().ok()?;
V::visit_bool(value.kind() == T![true], diagnostics)
}
AnyJsonValue::JsonNullValue(_) => V::visit_null(diagnostics),
AnyJsonValue::JsonNumberValue(value) => {
let value = value.value_token().ok()?;
let value = value.text_trimmed();
if let Ok(value) = value.parse::<u64>() {
V::visit_u64(value, diagnostics)
} else if let Ok(value) = value.parse::<i64>() {
V::visit_i64(value, diagnostics)
} else {
diagnostics.report_number_out_of_bounds(i64::MIN, u64::MAX);
None
}
}
AnyJsonValue::JsonNullValue(_) => visitor.visit_unit(range, diagnostics),
AnyJsonValue::JsonNumberValue(_) => todo!(),
AnyJsonValue::JsonObjectValue(object) => {
let members = object
.json_member_list()
Expand All @@ -663,18 +671,12 @@ impl DeserializableLanguage for JsonLanguage {
.filter_map(|x| {
Some((x.name().ok()?.into_syntax(), x.value().ok()?.into_syntax()))
});
visitor.visit_map(members, range, diagnostics)
V::visit_map(members, diagnostics)
}
AnyJsonValue::JsonStringValue(value) => {
let Ok(value) = value.inner_string_text() else {
return;
};
visitor.visit_str(value.text(), range, diagnostics)
let value = value.inner_string_text().ok()?;
V::visit_token_text(value, diagnostics)
}
}
}

fn map_to_str(value: SyntaxNode<JsonLanguage>) -> Option<TokenText> {
JsonStringValue::cast(value).and_then(|value| value.inner_string_text().ok())
}
}
4 changes: 3 additions & 1 deletion crates/biome_deserialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ pub mod json;
pub mod string_set;

use biome_diagnostics::{Error, Severity};
pub use diagnostics::{DeserializationAdvice, DeserializationDiagnostic};
pub use diagnostics::{
DeserializationAdvice, DeserializationDiagnostic, DeserializationDiagnostics,
};
use std::fmt::Debug;
pub use string_set::{deserialize_string_set, serialize_string_set, StringSet};
pub use visitor::*;
Expand Down
Loading

0 comments on commit 694783f

Please sign in to comment.