Skip to content

Commit

Permalink
Minor corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Nov 9, 2023
1 parent b6a555b commit 1c5874e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
2 changes: 1 addition & 1 deletion juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- Removed lifetime parameter from `ParseError`, `GraphlQLError`, `GraphQLBatchRequest` and `GraphQLRequest`. ([#1081], [#528])
- Upgraded [GraphiQL] to 3.0.9 version (requires new [`graphql-transport-ws` GraphQL over WebSocket Protocol] integration on server, see `juniper_warp/examples/subscription.rs`). ([#1188], [#1193], [#1204])
- Made `LookAheadMethods::children()` method to return slice instead of `Vec`. ([#1200])
- Abstract `(start, end)` from the `Spanning<T>` struct into separate struct `Span`. ([#1207])
- Abstracted `Spanning::start` and `Spanning::end` fields into separate struct `Span`. ([#1207])

### Added

Expand Down
19 changes: 11 additions & 8 deletions juniper/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ where
&start_pos.clone(),
&directives
.as_ref()
.map_or(frag_name.end(), Spanning::end)
.map_or(&frag_name.span.end, |s| &s.span.end)
.clone(),
FragmentSpread {
name: frag_name,
Expand Down Expand Up @@ -326,10 +326,10 @@ where
&alias.as_ref().unwrap_or(&name).span.start,
&selection_set
.as_ref()
.map(Spanning::end)
.or_else(|| directives.as_ref().map(Spanning::end))
.or_else(|| arguments.as_ref().map(Spanning::end))
.unwrap_or(name.end())
.map(|s| &s.span.end)
.or_else(|| directives.as_ref().map(|s| &s.span.end))
.or_else(|| arguments.as_ref().map(|s| &s.span.end))
.unwrap_or(&name.span.end)
.clone(),
Field {
alias,
Expand Down Expand Up @@ -449,10 +449,10 @@ where
&start_pos,
&default_value
.as_ref()
.map_or(var_type.end(), Spanning::end)
.map_or(&var_type.span.end, |s| &s.span.end)
.clone(),
(
Spanning::start_end(&start_pos, var_name.end(), var_name.item),
Spanning::start_end(&start_pos, &var_name.span.end, var_name.item),
VariableDefinition {
var_type,
default_value,
Expand Down Expand Up @@ -501,7 +501,10 @@ where

Ok(Spanning::start_end(
&start_pos,
&arguments.as_ref().map_or(name.end(), Spanning::end).clone(),
&arguments
.as_ref()
.map_or(&name.span.end, |s| &s.span.end)
.clone(),
Directive { name, arguments },
))
}
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'a> Parser<'a> {
Ok(Some(self.next_token()?))
} else if self.peek().item == Token::EndOfFile {
Err(Spanning::zero_width(
self.peek().start(),
&self.peek().span.start,
ParseError::UnexpectedEndOfFile,
))
} else {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<'a> Parser<'a> {
items.push(parser(self)?);

if let Some(end_spanning) = self.skip(closing)? {
return Ok(Spanning::start_end(start_pos, end_spanning.end(), items));
return Ok(Spanning::start_end(start_pos, &end_spanning.end(), items));
}
}
}
Expand All @@ -196,7 +196,7 @@ impl<'a> Parser<'a> {
items.push(parser(self)?);

if let Some(end_spanning) = self.skip(closing)? {
return Ok(Spanning::start_end(start_pos, end_spanning.end(), items));
return Ok(Spanning::start_end(start_pos, &end_spanning.end(), items));
}
}
}
Expand Down
67 changes: 37 additions & 30 deletions juniper/src/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,41 @@ pub struct SourcePosition {
col: usize,
}

/// A "span" is a range of characters in the input source, starting at the
/// character pointed by the `start` field and ending just before the `end`
/// marker.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
/// Range of characters in the input source, starting at the character pointed by the `start` field
/// and ending just before the `end` marker.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Span {
/// Start position of the span
/// Start position of this [`Span`].
pub start: SourcePosition,

/// End position of the span
/// End position of this [`Span`].
///
/// This points to the first source position _after_ the span.
/// > __NOTE__: This points to the first source position __after__ this [`Span`].
pub end: SourcePosition,
}

impl Span {
#[doc(hidden)]
pub fn new(start: &SourcePosition, end: &SourcePosition) -> Span {
#[inline]
pub fn zero_width(pos: SourcePosition) -> Self {
Self {
start: *start,
end: *end,
start: pos,
end: pos,
}
}

#[doc(hidden)]
pub fn zero_width(pos: &SourcePosition) -> Span {
Self::new(pos, pos)
}

#[doc(hidden)]
pub fn single_width(pos: &SourcePosition) -> Span {
let mut end = *pos;
#[inline]
pub fn single_width(pos: SourcePosition) -> Self {
let mut end = pos;
end.advance_col();

Self { start: *pos, end }
Self { start: pos, end }
}

#[doc(hidden)]
pub fn unlocated() -> Span {
#[inline]
pub fn unlocated() -> Self {
Self {
start: SourcePosition::new_origin(),
end: SourcePosition::new_origin(),
Expand All @@ -70,25 +67,31 @@ impl<T> Spanning<T> {
}

#[doc(hidden)]
pub fn zero_width(pos: &SourcePosition, item: T) -> Spanning<T> {
pub fn zero_width(&pos: &SourcePosition, item: T) -> Spanning<T> {
Self::new(Span::zero_width(pos), item)
}

#[doc(hidden)]
pub fn single_width(pos: &SourcePosition, item: T) -> Spanning<T> {
pub fn single_width(&pos: &SourcePosition, item: T) -> Spanning<T> {
Self::new(Span::single_width(pos), item)
}

#[doc(hidden)]
pub fn start_end(start: &SourcePosition, end: &SourcePosition, item: T) -> Spanning<T> {
Self::new(Span::new(start, end), item)
pub fn start_end(&start: &SourcePosition, &end: &SourcePosition, item: T) -> Spanning<T> {
Self::new(Span { start, end }, item)
}

#[doc(hidden)]
#[allow(clippy::self_named_constructors)]
pub fn spanning(v: Vec<Spanning<T>>) -> Option<Spanning<Vec<Spanning<T>>>> {
if let (Some(start), Some(end)) = (v.first().map(|s| s.span), v.last().map(|s| s.span)) {
Some(Spanning::new(Span::new(&start.start, &end.end), v))
Some(Spanning::new(
Span {
start: start.start,
end: end.end,
},
v,
))
} else {
None
}
Expand All @@ -99,14 +102,18 @@ impl<T> Spanning<T> {
Self::new(Span::unlocated(), item)
}

#[doc(hidden)]
pub fn start(&self) -> &SourcePosition {
&self.span.start
/// Returns start position of the item.
#[inline]
pub fn start(&self) -> SourcePosition {
self.span.start
}

#[doc(hidden)]
pub fn end(&self) -> &SourcePosition {
&self.span.end
/// Returns end position of the item.
///
/// > __NOTE__: This points to the first source position __after__ the item.
#[inline]
pub fn end(&self) -> SourcePosition {
self.span.end
}

/// Modify the contents of the spanned item.
Expand Down

0 comments on commit 1c5874e

Please sign in to comment.