Skip to content

Commit

Permalink
feat(biome_deserialize): implement necessary traits for `#[deserializ…
Browse files Browse the repository at this point in the history
…able(rest)]` (#2857)
  • Loading branch information
NicholasLYang committed May 14, 2024
1 parent 9c920a1 commit 753ed41
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/biome_deserialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ bitflags = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
smallvec = { workspace = true, optional = true }

[features]
schema = ["schemars", "schemars/indexmap"]
serde = ["serde_json"]
smallvec = ["dep:smallvec"]

[lints]
Expand Down
13 changes: 13 additions & 0 deletions crates/biome_deserialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ impl Text {
self.0.text()
}
}

impl PartialOrd for Text {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Text {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.text().cmp(other.text())
}
}

impl Deref for Text {
type Target = str;
fn deref(&self) -> &Self::Target {
Expand Down
96 changes: 96 additions & 0 deletions crates/biome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,102 @@ impl DeserializableValue for AnyJsonValue {
}
}

#[cfg(feature = "serde")]
impl Deserializable for serde_json::Value {
fn deserialize(
value: &impl DeserializableValue,
name: &str,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self> {
struct Visitor;
impl DeserializationVisitor for Visitor {
type Output = serde_json::Value;
const EXPECTED_TYPE: VisitableType = VisitableType::all();
fn visit_null(
self,
_range: biome_rowan::TextRange,
_name: &str,
_diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
Some(serde_json::Value::Null)
}

fn visit_bool(
self,
value: bool,
_range: biome_rowan::TextRange,
_name: &str,
_diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
Some(serde_json::Value::Bool(value))
}

fn visit_number(
self,
value: TextNumber,
_range: biome_rowan::TextRange,
_name: &str,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
match serde_json::from_str(value.text()) {
Ok(num) => Some(serde_json::Value::Number(num)),
Err(err) => {
diagnostics.push(DeserializationDiagnostic::new(err.to_string()));
None
}
}
}

fn visit_str(
self,
value: Text,
_range: biome_rowan::TextRange,
_name: &str,
_diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
Some(serde_json::Value::String(value.text().to_string()))
}

fn visit_array(
self,
values: impl Iterator<Item = Option<impl DeserializableValue>>,
_range: biome_rowan::TextRange,
_name: &str,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
Some(serde_json::Value::Array(
values
.filter_map(|value| Deserializable::deserialize(&value?, "", diagnostics))
.collect(),
))
}

fn visit_map(
self,
members: impl Iterator<
Item = Option<(impl DeserializableValue, impl DeserializableValue)>,
>,
_range: biome_rowan::TextRange,
_name: &str,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
Some(serde_json::Value::Object(
members
.filter_map(|entry| {
let (key, value) = entry?;
let key = Deserializable::deserialize(&key, "", diagnostics)?;
let value = value.deserialize(Visitor, "", diagnostics)?;
Some((key, value))
})
.collect(),
))
}
}

value.deserialize(Visitor, name, diagnostics)
}
}

impl DeserializableValue for JsonMemberName {
fn range(&self) -> biome_rowan::TextRange {
AstNode::range(self)
Expand Down

0 comments on commit 753ed41

Please sign in to comment.