Skip to content

Commit

Permalink
refactor: simplify transformation of name elements
Browse files Browse the repository at this point in the history
Instead of manually constructing the `Name` instances, use the From/Into
traits to do so automatically and with less code.
  • Loading branch information
dnaka91 committed Oct 27, 2023
1 parent 9c88e32 commit 2acd7b7
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use stef_parser::{
Comment, Const, DataType, Definition, Enum, ExternalType, Fields, Generics, Import, Literal,
Module, NamedField, Schema, Struct, TypeAlias, UnnamedField, Variant, Name,
Module, Name, NamedField, Schema, Struct, TypeAlias, UnnamedField, Variant,
};

use super::{decode, encode};
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ pub(super) fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ pub(super) fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
10 changes: 6 additions & 4 deletions crates/stef-parser/src/parser/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use winnow::{
};

use super::{comments, ids, types, ws, Input, ParserExt, Result};
use crate::{highlight, location, Fields, NamedField, UnnamedField};
use crate::{highlight, location, Fields, Name, NamedField, UnnamedField};

/// Encountered an invalid field declaration.
#[derive(Debug, ParserError)]
Expand Down Expand Up @@ -130,7 +130,7 @@ fn parse_named_field<'i>(input: &mut Input<'i>) -> Result<NamedField<'i>, Cause>
(
ws(comments::parse.map_err(Cause::from)),
(
delimited(space0, parse_field_name.with_span(), ':'),
delimited(space0, parse_field_name, ':'),
preceded(space0, types::parse.map_err(Cause::from)),
preceded(space0, ids::parse.map_err(Cause::from)),
)
Expand All @@ -139,20 +139,22 @@ fn parse_named_field<'i>(input: &mut Input<'i>) -> Result<NamedField<'i>, Cause>
.parse_next(input)
.map(|(comment, ((name, ty, id), span))| NamedField {
comment,
name: name.into(),
name,
ty,
id,
span: span.into(),
})
}

fn parse_field_name<'i>(input: &mut Input<'i>) -> Result<&'i str, Cause> {
fn parse_field_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
(
one_of('a'..='z'),
take_while(0.., ('a'..='z', '0'..='9', '_')),
)
.recognize()
.with_span()
.parse_next(input)
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(name, span)| Name {
value: name,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ pub(super) fn parse_segment<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidSegmentName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ pub(super) fn parse_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
.map_err(|e| {
e.map(|()| Cause::InvalidName {
at: input.location(),
Expand Down
5 changes: 1 addition & 4 deletions crates/stef-parser/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,5 @@ fn parse_external_name<'i>(input: &mut Input<'i>) -> Result<Name<'i>, Cause> {
.recognize()
.with_span()
.parse_next(input)
.map(|(value, span)| Name {
value,
span: span.into(),
})
.map(Into::into)
}

0 comments on commit 2acd7b7

Please sign in to comment.