Skip to content

Commit

Permalink
oops — AbsoluteLocation should be an enum, just like Location
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed Jan 5, 2025
1 parent bc162b7 commit 9a69a0e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
61 changes: 46 additions & 15 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,64 @@ impl RelativeEmbeddedLocation {
}

#[derive(Debug)]
pub struct AbsoluteLocation {
pub relative_location: Location,
pub enum AbsoluteLocation {
Embedded(AbsoluteEmbeddedLocation),
Generated,
}

impl From<AbsoluteLocation> for Location {
fn from(value: AbsoluteLocation) -> Self {
match value {
AbsoluteLocation::Embedded(absolute_embedded_location) => {
Location::Embedded(absolute_embedded_location.relative_embedded_location)
}
AbsoluteLocation::Generated => Location::Generated,
}
}
}

impl fmt::Display for AbsoluteLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.relative_location {
Location::Embedded(relative_embedded_location) => {
let wrapper = AbsoluteEmbeddedLocation {
relative_embedded_location,
};
wrapper.fmt(f)
match self {
AbsoluteLocation::Embedded(absolute_embedded_location) => {
absolute_embedded_location.fmt(f)
}
Location::Generated => {
AbsoluteLocation::Generated => {
write!(f, "<generated>")
}
}
}
}

impl AbsoluteLocation {
pub fn from_relative_location(relative_location: Location) -> Self {
match relative_location {
Location::Embedded(relative_embedded_location) => {
AbsoluteLocation::Embedded(AbsoluteEmbeddedLocation {
relative_embedded_location,
})
}
Location::Generated => AbsoluteLocation::Generated,
}
}
}

// TODO rename WithRelativeLocation
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct WithLocation<T> {
pub relative_location: Location,
pub item: T,
}

impl<T> From<WithAbsoluteLocation<T>> for WithLocation<T> {
fn from(value: WithAbsoluteLocation<T>) -> Self {
WithLocation {
relative_location: value.absolute_location.into(),
item: value.item,
}
}
}

impl<T: Error> Error for WithAbsoluteLocation<T> {
fn description(&self) -> &str {
#[allow(deprecated)]
Expand All @@ -142,9 +173,9 @@ pub struct WithAbsoluteLocation<T> {
impl<T> WithAbsoluteLocation<T> {
pub fn from_with_location(with_location: WithLocation<T>) -> Self {
WithAbsoluteLocation {
absolute_location: AbsoluteLocation {
relative_location: with_location.relative_location,
},
absolute_location: AbsoluteLocation::from_relative_location(
with_location.relative_location,
),
item: with_location.item,
}
}
Expand All @@ -153,9 +184,9 @@ impl<T> WithAbsoluteLocation<T> {
with_location: &WithLocation<T>,
) -> WithAbsoluteLocation<&T> {
WithAbsoluteLocation {
absolute_location: AbsoluteLocation {
relative_location: with_location.relative_location,
},
absolute_location: AbsoluteLocation::from_relative_location(
with_location.relative_location,
),
item: &with_location.item,
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/graphql_lang_types/src/graphql_sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ pub struct GraphQLFieldDefinition {
impl fmt::Display for GraphQLFieldDefinition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name_absolute_location = WithAbsoluteLocation {
absolute_location: AbsoluteLocation {
relative_location: self.name.relative_location,
},
absolute_location: AbsoluteLocation::from_relative_location(
self.name.relative_location,
),
item: self.name.item,
};
write!(f, "{}", name_absolute_location)?;
Expand All @@ -257,9 +257,9 @@ impl fmt::Display for GraphQLFieldDefinition {
.arguments
.iter()
.map(|input_value_definition| WithAbsoluteLocation {
absolute_location: AbsoluteLocation {
relative_location: input_value_definition.relative_location,
},
absolute_location: AbsoluteLocation::from_relative_location(
input_value_definition.relative_location,
),
item: input_value_definition.item.clone(),
})
.collect::<Vec<_>>();
Expand Down
6 changes: 2 additions & 4 deletions crates/isograph_schema/src/process_type_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,7 @@ fn set_and_validate_id_field(
// in the warn case, we'll print. But we return a WithLocation, because that
// is the type in ProcessTypeDefinitionResult. In the end, ProcessTypeDefinitionResult
// should include WithAbsoluteLocation. That's a bigger change!
.map_err(|e| {
WithLocation::new(e.item, e.absolute_location.relative_location)
})?;
.map_err(WithLocation::from)?;
}
Ok(())
}
Expand All @@ -782,7 +780,7 @@ fn set_and_validate_id_field(
// in the warn case, we'll print. But we return a WithLocation, because that
// is the type in ProcessTypeDefinitionResult. In the end, ProcessTypeDefinitionResult
// should include WithAbsoluteLocation. That's a bigger change!
.map_err(|e| WithLocation::new(e.item, e.absolute_location.relative_location))?;
.map_err(WithLocation::from)?;
Ok(())
}
}
Expand Down

0 comments on commit 9a69a0e

Please sign in to comment.