Skip to content

Commit 49e4da1

Browse files
tyao1facebook-github-bot
authored andcommitted
Make compiler results seriaizable
Reviewed By: voideanvalue Differential Revision: D52854176 fbshipit-source-id: aeca0a872d1b907b79d802380856d7e9e8634150
1 parent 08c32cf commit 49e4da1

File tree

38 files changed

+325
-46
lines changed

38 files changed

+325
-46
lines changed

compiler/crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ md-5 = "0.10"
1818
rayon = "1.2"
1919
serde = { version = "1.0.185", features = ["derive", "rc"] }
2020
serde_json = { version = "1.0.100", features = ["float_roundtrip", "unbounded_depth"] }
21+
typetag = "0.2.15"

compiler/crates/common/src/diagnostic.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fmt::Write;
1212

1313
use lsp_types::DiagnosticSeverity;
1414
use lsp_types::DiagnosticTag;
15+
use serde::ser::SerializeMap;
1516
use serde_json::Value;
1617

1718
use crate::Location;
@@ -252,6 +253,18 @@ impl fmt::Display for Diagnostic {
252253
}
253254
}
254255

256+
impl serde::Serialize for Diagnostic {
257+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
258+
where
259+
S: serde::Serializer,
260+
{
261+
let mut diagnostic = serializer.serialize_map(Some(2))?;
262+
diagnostic.serialize_entry("message", &self.0.message)?;
263+
diagnostic.serialize_entry("location", &self.0.location)?;
264+
diagnostic.end()
265+
}
266+
}
267+
255268
impl Error for Diagnostic {}
256269

257270
// statically verify that the Diagnostic type is thread safe
@@ -302,11 +315,14 @@ pub trait WithDiagnosticData {
302315

303316
/// Trait for diagnostic messages to allow structs that capture
304317
/// some data and can lazily convert it to a message.
318+
#[typetag::serialize(tag = "type")]
305319
pub trait DiagnosticDisplay: fmt::Debug + fmt::Display + Send + Sync {}
306320

307321
/// Automatically implement the trait if constraints are met, so that
308322
/// implementors don't need to.
309-
impl<T> DiagnosticDisplay for T where T: fmt::Debug + fmt::Display + Send + Sync {}
323+
#[typetag::serialize]
324+
impl<T> DiagnosticDisplay for T where T: fmt::Debug + fmt::Display + Send + Sync + typetag::Serialize
325+
{}
310326

311327
impl From<Diagnostic> for Diagnostics {
312328
fn from(diagnostic: Diagnostic) -> Self {

compiler/crates/common/src/location.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ use crate::span::Span;
1818
/// The location of a source. Could be a standalone file (e.g. test.graphql),
1919
/// an embedded source (GraphQL tag in a JS file) or generated code without a
2020
/// location.
21-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
21+
#[derive(
22+
Copy,
23+
Clone,
24+
Debug,
25+
Eq,
26+
PartialEq,
27+
Ord,
28+
PartialOrd,
29+
Hash,
30+
serde::Serialize
31+
)]
32+
#[serde(tag = "type")]
2233
pub enum SourceLocationKey {
2334
/// A source embedded within a file. The 0-based index is an index into the
2435
/// embedded sources. E.g. the second graphql tag has index 1.
@@ -72,7 +83,7 @@ impl SourceLocationKey {
7283

7384
/// An absolute source location describing both the file and position (span)
7485
/// with that file.
75-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
86+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize)]
7687
pub struct Location {
7788
/// The source containing this location (e.g. embedded or standalone file).
7889
source_location: SourceLocationKey,

compiler/crates/common/src/named_item.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,17 @@ impl fmt::Display for ScalarName {
105105
}
106106
}
107107
impl_lookup!(ArgumentName);
108-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
108+
#[derive(
109+
Clone,
110+
Copy,
111+
Debug,
112+
Eq,
113+
PartialEq,
114+
Ord,
115+
PartialOrd,
116+
Hash,
117+
serde::Serialize
118+
)]
109119
pub struct ObjectName(pub StringKey);
110120

111121
impl fmt::Display for ObjectName {
@@ -137,7 +147,17 @@ impl fmt::Display for EnumName {
137147
}
138148
impl_lookup!(EnumName);
139149

140-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
150+
#[derive(
151+
Clone,
152+
Copy,
153+
Debug,
154+
Eq,
155+
PartialEq,
156+
Ord,
157+
PartialOrd,
158+
Hash,
159+
serde::Serialize
160+
)]
141161
pub struct InterfaceName(pub StringKey);
142162

143163
impl fmt::Display for InterfaceName {

compiler/crates/common/src/span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use std::fmt;
99

10-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
10+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize)]
1111
pub struct Span {
1212
pub start: u32,
1313
pub end: u32,

compiler/crates/docblock-syntax/src/errors.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@
77

88
use thiserror::Error;
99

10-
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
10+
#[derive(
11+
Clone,
12+
Copy,
13+
Debug,
14+
Error,
15+
Eq,
16+
PartialEq,
17+
Ord,
18+
PartialOrd,
19+
Hash,
20+
serde::Serialize
21+
)]
22+
#[serde(tag = "type")]
1123
pub enum SyntaxError {
1224
#[error("Expected \"{expected}\".")]
1325
ExpectedString { expected: &'static str },

compiler/crates/graphql-ir-validations/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ graphql-ir = { path = "../graphql-ir" }
2020
graphql-text-printer = { path = "../graphql-text-printer" }
2121
intern = { path = "../intern" }
2222
schema = { path = "../schema" }
23+
serde = { version = "1.0.185", features = ["derive", "rc"] }
2324
thiserror = "1.0.49"
2425

2526
[dev-dependencies]

compiler/crates/graphql-ir-validations/src/validate_selection_conflict.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,18 @@ mod ignoring_type_and_location {
581581
}
582582
}
583583

584-
#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
584+
#[derive(
585+
Clone,
586+
Debug,
587+
Error,
588+
Eq,
589+
PartialEq,
590+
Ord,
591+
PartialOrd,
592+
Hash,
593+
serde::Serialize
594+
)]
595+
#[serde(tag = "type")]
585596
enum ValidationMessage {
586597
#[error(
587598
"Field '{response_key}' is ambiguous because it references two different fields: '{l_name}' and '{r_name}'"

compiler/crates/graphql-ir/src/errors.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ impl Display for ErrorLink {
3333
}
3434

3535
/// Fixed set of validation errors with custom display messages
36-
#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
36+
#[derive(
37+
Clone,
38+
Debug,
39+
Error,
40+
Eq,
41+
PartialEq,
42+
Ord,
43+
PartialOrd,
44+
Hash,
45+
serde::Serialize
46+
)]
47+
#[serde(tag = "type", content = "args")]
3748
pub enum ValidationMessage {
3849
#[error("Duplicate definitions for '{0}'")]
3950
DuplicateDefinition(StringKey),
@@ -528,7 +539,18 @@ pub enum ValidationMessage {
528539
ResolverInMutation,
529540
}
530541

531-
#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
542+
#[derive(
543+
Clone,
544+
Debug,
545+
Error,
546+
Eq,
547+
PartialEq,
548+
Ord,
549+
PartialOrd,
550+
Hash,
551+
serde::Serialize
552+
)]
553+
#[serde(tag = "type")]
532554
pub enum ValidationMessageWithData {
533555
#[error("Unknown type '{type_name}'.{suggestions}", suggestions = did_you_mean(suggestions))]
534556
UnknownType {

compiler/crates/graphql-ir/src/ir.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,17 @@ impl ExecutableDefinitionName {
234234
}
235235
}
236236

237-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
237+
#[derive(
238+
Clone,
239+
Copy,
240+
Debug,
241+
Eq,
242+
PartialEq,
243+
Ord,
244+
PartialOrd,
245+
Hash,
246+
serde::Serialize
247+
)]
238248
pub struct VariableName(pub StringKey);
239249

240250
impl Display for VariableName {

0 commit comments

Comments
 (0)