Skip to content

Commit 9c75e9f

Browse files
committed
Deduplicate Edition enum
1 parent e785280 commit 9c75e9f

File tree

8 files changed

+65
-62
lines changed

8 files changed

+65
-62
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parser/src/edition.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! The edition of the Rust language used in a crate.
2+
// Ideally this would be defined in the span crate, but the dependency chain is all over the place
3+
// wrt to span, parser and syntax.
4+
use std::fmt;
5+
6+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7+
pub enum Edition {
8+
Edition2015,
9+
Edition2018,
10+
Edition2021,
11+
Edition2024,
12+
}
13+
14+
impl Edition {
15+
pub const CURRENT: Edition = Edition::Edition2021;
16+
pub const DEFAULT: Edition = Edition::Edition2015;
17+
}
18+
19+
#[derive(Debug)]
20+
pub struct ParseEditionError {
21+
invalid_input: String,
22+
}
23+
24+
impl std::error::Error for ParseEditionError {}
25+
impl fmt::Display for ParseEditionError {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
write!(f, "invalid edition: {:?}", self.invalid_input)
28+
}
29+
}
30+
31+
impl std::str::FromStr for Edition {
32+
type Err = ParseEditionError;
33+
34+
fn from_str(s: &str) -> Result<Self, Self::Err> {
35+
let res = match s {
36+
"2015" => Edition::Edition2015,
37+
"2018" => Edition::Edition2018,
38+
"2021" => Edition::Edition2021,
39+
"2024" => Edition::Edition2024,
40+
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
41+
};
42+
Ok(res)
43+
}
44+
}
45+
46+
impl fmt::Display for Edition {
47+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48+
f.write_str(match self {
49+
Edition::Edition2015 => "2015",
50+
Edition::Edition2018 => "2018",
51+
Edition::Edition2021 => "2021",
52+
Edition::Edition2024 => "2024",
53+
})
54+
}
55+
}

crates/parser/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern crate ra_ap_rustc_lexer as rustc_lexer;
2626
#[cfg(feature = "in-rust-tree")]
2727
extern crate rustc_lexer;
2828

29+
mod edition;
2930
mod event;
3031
mod grammar;
3132
mod input;
@@ -42,10 +43,10 @@ mod tests;
4243
pub(crate) use token_set::TokenSet;
4344

4445
pub use crate::{
46+
edition::Edition,
4547
input::Input,
4648
lexed_str::LexedStr,
4749
output::{Output, Step},
48-
parser::Edition,
4950
shortcuts::StrStep,
5051
syntax_kind::SyntaxKind,
5152
};

crates/parser/src/parser.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use limit::Limit;
88
use crate::{
99
event::Event,
1010
input::Input,
11+
Edition,
1112
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
1213
TokenSet, T,
1314
};
@@ -29,13 +30,6 @@ pub(crate) struct Parser<'t> {
2930
_edition: Edition,
3031
}
3132

32-
#[non_exhaustive]
33-
pub enum Edition {
34-
Edition2015,
35-
Edition2018,
36-
Edition2021,
37-
}
38-
3933
static PARSER_STEP_LIMIT: Limit = Limit::new(15_000_000);
4034

4135
impl<'t> Parser<'t> {

crates/span/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ la-arena.workspace = true
1414
salsa.workspace = true
1515
rustc-hash.workspace = true
1616
hashbrown.workspace = true
17+
text-size.workspace = true
1718

1819
# local deps
1920
vfs.workspace = true

crates/span/src/lib.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,10 @@ pub use self::{
1313
map::{RealSpanMap, SpanMap},
1414
};
1515

16-
pub use syntax::{TextRange, TextSize};
16+
pub use syntax::Edition;
17+
pub use text_size::{TextRange, TextSize};
1718
pub use vfs::FileId;
1819

19-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
20-
pub enum Edition {
21-
Edition2015,
22-
Edition2018,
23-
Edition2021,
24-
Edition2024,
25-
}
26-
27-
impl Edition {
28-
pub const CURRENT: Edition = Edition::Edition2021;
29-
pub const DEFAULT: Edition = Edition::Edition2015;
30-
}
31-
32-
#[derive(Debug)]
33-
pub struct ParseEditionError {
34-
invalid_input: String,
35-
}
36-
37-
impl std::error::Error for ParseEditionError {}
38-
impl fmt::Display for ParseEditionError {
39-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40-
write!(f, "invalid edition: {:?}", self.invalid_input)
41-
}
42-
}
43-
44-
impl std::str::FromStr for Edition {
45-
type Err = ParseEditionError;
46-
47-
fn from_str(s: &str) -> Result<Self, Self::Err> {
48-
let res = match s {
49-
"2015" => Edition::Edition2015,
50-
"2018" => Edition::Edition2018,
51-
"2021" => Edition::Edition2021,
52-
"2024" => Edition::Edition2024,
53-
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
54-
};
55-
Ok(res)
56-
}
57-
}
58-
59-
impl fmt::Display for Edition {
60-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61-
f.write_str(match self {
62-
Edition::Edition2015 => "2015",
63-
Edition::Edition2018 => "2018",
64-
Edition::Edition2021 => "2021",
65-
Edition::Edition2024 => "2024",
66-
})
67-
}
68-
}
6920
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7021
pub struct FilePosition {
7122
pub file_id: FileId,

crates/span/src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
use std::{fmt, hash::Hash};
55

66
use stdx::{always, itertools::Itertools};
7-
use syntax::{TextRange, TextSize};
87
use vfs::FileId;
98

109
use crate::{
11-
ErasedFileAstId, Span, SpanAnchor, SpanData, SyntaxContextId, ROOT_ERASED_FILE_AST_ID,
10+
ErasedFileAstId, Span, SpanAnchor, SpanData, SyntaxContextId, TextRange, TextSize,
11+
ROOT_ERASED_FILE_AST_ID,
1212
};
1313

1414
/// Maps absolute text ranges for the corresponding file to the relevant span data.

crates/syntax/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use crate::{
6060
},
6161
token_text::TokenText,
6262
};
63-
pub use parser::{SyntaxKind, T};
63+
pub use parser::{Edition, SyntaxKind, T};
6464
pub use rowan::{
6565
api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize,
6666
TokenAtOffset, WalkEvent,

0 commit comments

Comments
 (0)