Skip to content

Commit

Permalink
rename more structs to include Embedded in their name
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed Jan 3, 2025
1 parent 07de136 commit 760b2c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
47 changes: 25 additions & 22 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ impl RelativeTextSource {
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EmbeddedLocation {
pub struct EmbeddedRelativeLocation {
pub text_source: RelativeTextSource,
/// The span is relative to the Source's span, not to the
/// entire source file.
pub span: Span,
}

impl std::fmt::Display for EmbeddedLocation {
impl std::fmt::Display for EmbeddedRelativeLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (file_path, read_out_text) = self.text_source.read_to_string();
let text_with_carats = text_with_carats(&read_out_text, self.span);
Expand All @@ -49,15 +49,15 @@ impl std::fmt::Display for EmbeddedLocation {
}
}

impl From<EmbeddedLocation> for Location {
fn from(value: EmbeddedLocation) -> Self {
impl From<EmbeddedRelativeLocation> for Location {
fn from(value: EmbeddedRelativeLocation) -> Self {
Location::Embedded(value)
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Location {
Embedded(EmbeddedLocation),
Embedded(EmbeddedRelativeLocation),
Generated,
}

Expand All @@ -66,7 +66,7 @@ impl Location {
Location::Generated
}
pub fn new(text_source: RelativeTextSource, span: Span) -> Self {
Location::Embedded(EmbeddedLocation::new(text_source, span))
Location::Embedded(EmbeddedRelativeLocation::new(text_source, span))
}

pub fn span(&self) -> Option<Span> {
Expand All @@ -76,9 +76,9 @@ impl Location {
}
}
}
impl EmbeddedLocation {
impl EmbeddedRelativeLocation {
pub fn new(text_source: RelativeTextSource, span: Span) -> Self {
EmbeddedLocation { text_source, span }
EmbeddedRelativeLocation { text_source, span }
}
}

Expand Down Expand Up @@ -131,7 +131,7 @@ impl<T> WithLocation<T> {
/// EmbeddedLocation or WithEmbeddedLocation
pub fn hack_to_with_span(self) -> WithSpan<T> {
let span = match self.location {
Location::Embedded(EmbeddedLocation { span, .. }) => span,
Location::Embedded(EmbeddedRelativeLocation { span, .. }) => span,
Location::Generated => Span::todo_generated(),
};
WithSpan {
Expand All @@ -142,47 +142,50 @@ impl<T> WithLocation<T> {
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct WithEmbeddedLocation<T> {
pub location: EmbeddedLocation,
pub struct WithEmbeddedRelativeLocation<T> {
pub location: EmbeddedRelativeLocation,
pub item: T,
}

impl<T: Error> Error for WithEmbeddedLocation<T> {
impl<T: Error> Error for WithEmbeddedRelativeLocation<T> {
fn description(&self) -> &str {
#[allow(deprecated)]
self.item.description()
}
}

impl<T: fmt::Display> fmt::Display for WithEmbeddedLocation<T> {
impl<T: fmt::Display> fmt::Display for WithEmbeddedRelativeLocation<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}\n{}", self.item, self.location)
}
}

impl<T> WithEmbeddedLocation<T> {
pub fn new(item: T, location: EmbeddedLocation) -> Self {
WithEmbeddedLocation { item, location }
impl<T> WithEmbeddedRelativeLocation<T> {
pub fn new(item: T, location: EmbeddedRelativeLocation) -> Self {
WithEmbeddedRelativeLocation { item, location }
}

pub fn map<U>(self, map: impl FnOnce(T) -> U) -> WithEmbeddedLocation<U> {
WithEmbeddedLocation::new(map(self.item), self.location)
pub fn map<U>(self, map: impl FnOnce(T) -> U) -> WithEmbeddedRelativeLocation<U> {
WithEmbeddedRelativeLocation::new(map(self.item), self.location)
}

pub fn and_then<U, E>(
self,
map: impl FnOnce(T) -> Result<U, E>,
) -> Result<WithEmbeddedLocation<U>, E> {
Ok(WithEmbeddedLocation::new(map(self.item)?, self.location))
) -> Result<WithEmbeddedRelativeLocation<U>, E> {
Ok(WithEmbeddedRelativeLocation::new(
map(self.item)?,
self.location,
))
}

pub fn into_with_location(self) -> WithLocation<T> {
self.into()
}
}

impl<T> From<WithEmbeddedLocation<T>> for WithLocation<T> {
fn from(value: WithEmbeddedLocation<T>) -> Self {
impl<T> From<WithEmbeddedRelativeLocation<T>> for WithLocation<T> {
fn from(value: WithEmbeddedRelativeLocation<T>) -> Self {
WithLocation {
location: Location::Embedded(value.location),
item: value.item,
Expand Down
15 changes: 12 additions & 3 deletions crates/common_lang_types/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{fmt, ops::Range};

use crate::{EmbeddedLocation, Location, RelativeTextSource, WithEmbeddedLocation, WithLocation};
use crate::{
EmbeddedRelativeLocation, Location, RelativeTextSource, WithEmbeddedRelativeLocation,
WithLocation,
};

// Invariant: end >= start
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand Down Expand Up @@ -105,8 +108,14 @@ impl<T> WithSpan<T> {
WithLocation::new(self.item, Location::new(text_source, self.span))
}

pub fn to_with_embedded_location(self, text_source: RelativeTextSource) -> WithEmbeddedLocation<T> {
WithEmbeddedLocation::new(self.item, EmbeddedLocation::new(text_source, self.span))
pub fn to_with_embedded_location(
self,
text_source: RelativeTextSource,
) -> WithEmbeddedRelativeLocation<T> {
WithEmbeddedRelativeLocation::new(
self.item,
EmbeddedRelativeLocation::new(text_source, self.span),
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/graphql_lang_types/src/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use super::{write::write_arguments, NameValuePair};
use crate::GraphQLConstantValue;
use common_lang_types::{DirectiveArgumentName, DirectiveName, WithEmbeddedLocation};
use common_lang_types::{DirectiveArgumentName, DirectiveName, WithEmbeddedRelativeLocation};
use intern::Lookup;
use serde::{
de::{self, value::SeqDeserializer, IntoDeserializer, MapAccess},
Expand All @@ -13,7 +13,7 @@ use thiserror::Error;
// TODO maybe this should be NameAndArguments and a field should be the same thing...?
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct GraphQLDirective<T> {
pub name: WithEmbeddedLocation<DirectiveName>,
pub name: WithEmbeddedRelativeLocation<DirectiveName>,
pub arguments: Vec<NameValuePair<DirectiveArgumentName, T>>,
}

Expand Down

0 comments on commit 760b2c1

Please sign in to comment.