Skip to content

Commit

Permalink
Make compiler results seriaizable
Browse files Browse the repository at this point in the history
Reviewed By: voideanvalue

Differential Revision: D52854176

fbshipit-source-id: aeca0a872d1b907b79d802380856d7e9e8634150
  • Loading branch information
tyao1 authored and facebook-github-bot committed Jan 19, 2024
1 parent 08c32cf commit 49e4da1
Show file tree
Hide file tree
Showing 38 changed files with 325 additions and 46 deletions.
1 change: 1 addition & 0 deletions compiler/crates/common/Cargo.toml
Expand Up @@ -18,3 +18,4 @@ md-5 = "0.10"
rayon = "1.2"
serde = { version = "1.0.185", features = ["derive", "rc"] }
serde_json = { version = "1.0.100", features = ["float_roundtrip", "unbounded_depth"] }
typetag = "0.2.15"
18 changes: 17 additions & 1 deletion compiler/crates/common/src/diagnostic.rs
Expand Up @@ -12,6 +12,7 @@ use std::fmt::Write;

use lsp_types::DiagnosticSeverity;
use lsp_types::DiagnosticTag;
use serde::ser::SerializeMap;
use serde_json::Value;

use crate::Location;
Expand Down Expand Up @@ -252,6 +253,18 @@ impl fmt::Display for Diagnostic {
}
}

impl serde::Serialize for Diagnostic {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut diagnostic = serializer.serialize_map(Some(2))?;
diagnostic.serialize_entry("message", &self.0.message)?;
diagnostic.serialize_entry("location", &self.0.location)?;
diagnostic.end()
}
}

impl Error for Diagnostic {}

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

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

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

impl From<Diagnostic> for Diagnostics {
fn from(diagnostic: Diagnostic) -> Self {
Expand Down
15 changes: 13 additions & 2 deletions compiler/crates/common/src/location.rs
Expand Up @@ -18,7 +18,18 @@ use crate::span::Span;
/// The location of a source. Could be a standalone file (e.g. test.graphql),
/// an embedded source (GraphQL tag in a JS file) or generated code without a
/// location.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Copy,
Clone,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
pub enum SourceLocationKey {
/// A source embedded within a file. The 0-based index is an index into the
/// embedded sources. E.g. the second graphql tag has index 1.
Expand Down Expand Up @@ -72,7 +83,7 @@ impl SourceLocationKey {

/// An absolute source location describing both the file and position (span)
/// with that file.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize)]
pub struct Location {
/// The source containing this location (e.g. embedded or standalone file).
source_location: SourceLocationKey,
Expand Down
24 changes: 22 additions & 2 deletions compiler/crates/common/src/named_item.rs
Expand Up @@ -105,7 +105,17 @@ impl fmt::Display for ScalarName {
}
}
impl_lookup!(ArgumentName);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
pub struct ObjectName(pub StringKey);

impl fmt::Display for ObjectName {
Expand Down Expand Up @@ -137,7 +147,17 @@ impl fmt::Display for EnumName {
}
impl_lookup!(EnumName);

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
pub struct InterfaceName(pub StringKey);

impl fmt::Display for InterfaceName {
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/common/src/span.rs
Expand Up @@ -7,7 +7,7 @@

use std::fmt;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize)]
pub struct Span {
pub start: u32,
pub end: u32,
Expand Down
14 changes: 13 additions & 1 deletion compiler/crates/docblock-syntax/src/errors.rs
Expand Up @@ -7,7 +7,19 @@

use thiserror::Error;

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Error,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
pub enum SyntaxError {
#[error("Expected \"{expected}\".")]
ExpectedString { expected: &'static str },
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/graphql-ir-validations/Cargo.toml
Expand Up @@ -20,6 +20,7 @@ graphql-ir = { path = "../graphql-ir" }
graphql-text-printer = { path = "../graphql-text-printer" }
intern = { path = "../intern" }
schema = { path = "../schema" }
serde = { version = "1.0.185", features = ["derive", "rc"] }
thiserror = "1.0.49"

[dev-dependencies]
Expand Down
Expand Up @@ -581,7 +581,18 @@ mod ignoring_type_and_location {
}
}

#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Debug,
Error,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
enum ValidationMessage {
#[error(
"Field '{response_key}' is ambiguous because it references two different fields: '{l_name}' and '{r_name}'"
Expand Down
26 changes: 24 additions & 2 deletions compiler/crates/graphql-ir/src/errors.rs
Expand Up @@ -33,7 +33,18 @@ impl Display for ErrorLink {
}

/// Fixed set of validation errors with custom display messages
#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Debug,
Error,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type", content = "args")]
pub enum ValidationMessage {
#[error("Duplicate definitions for '{0}'")]
DuplicateDefinition(StringKey),
Expand Down Expand Up @@ -528,7 +539,18 @@ pub enum ValidationMessage {
ResolverInMutation,
}

#[derive(Clone, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Debug,
Error,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
pub enum ValidationMessageWithData {
#[error("Unknown type '{type_name}'.{suggestions}", suggestions = did_you_mean(suggestions))]
UnknownType {
Expand Down
12 changes: 11 additions & 1 deletion compiler/crates/graphql-ir/src/ir.rs
Expand Up @@ -234,7 +234,17 @@ impl ExecutableDefinitionName {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
pub struct VariableName(pub StringKey);

impl Display for VariableName {
Expand Down
14 changes: 13 additions & 1 deletion compiler/crates/graphql-syntax/src/lexer.rs
Expand Up @@ -19,7 +19,19 @@ pub struct TokenKindExtras {
}

/// Lexer for the GraphQL specification: <http://spec.graphql.org/>
#[derive(Logos, Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Logos,
Copy,
Clone,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
#[logos(extras = TokenKindExtras)]
pub enum TokenKind {
#[regex(r"[ \t\r\n\f,\ufeff]+|#[^\n\r]*", logos::skip)]
Expand Down
12 changes: 11 additions & 1 deletion compiler/crates/graphql-syntax/src/node/executable.rs
Expand Up @@ -98,7 +98,17 @@ impl OperationDefinition {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
pub enum OperationKind {
Query,
Mutation,
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/graphql-syntax/src/node/type_system.rs
Expand Up @@ -213,7 +213,7 @@ impl fmt::Display for OperationTypeDefinition {
}
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone, serde::Serialize)]
pub enum OperationType {
Query,
Mutation,
Expand Down
14 changes: 13 additions & 1 deletion compiler/crates/graphql-syntax/src/syntax_error.rs
Expand Up @@ -9,7 +9,19 @@ use thiserror::Error;

use crate::lexer::TokenKind;

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(
Clone,
Copy,
Debug,
Error,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize
)]
#[serde(tag = "type")]
pub enum SyntaxError {
#[error("Expected a {0}")]
Expected(TokenKind),
Expand Down

0 comments on commit 49e4da1

Please sign in to comment.