Skip to content

Commit a3a16a7

Browse files
committed
Reduce code duplication
1 parent a806979 commit a3a16a7

File tree

3 files changed

+60
-57
lines changed

3 files changed

+60
-57
lines changed

build.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ use std::path::Path;
44

55
type StrResult<T> = Result<T, String>;
66

7-
/// A module of definitions.
8-
struct Module<'a>(Vec<(&'a str, Binding<'a>)>);
7+
include!("src/shared.rs");
8+
9+
declare_types!{
10+
<'a>
11+
str = &'a str,
12+
List = Vec<_>
13+
}
914

1015
impl<'a> Module<'a> {
1116
fn new(mut list: Vec<(&'a str, Binding<'a>)>) -> Self {
@@ -14,24 +19,6 @@ impl<'a> Module<'a> {
1419
}
1520
}
1621

17-
/// A definition bound in a module, with metadata.
18-
struct Binding<'a> {
19-
def: Def<'a>,
20-
deprecation: Option<&'a str>,
21-
}
22-
23-
/// A definition in a module.
24-
enum Def<'a> {
25-
Symbol(Symbol<'a>),
26-
Module(Module<'a>),
27-
}
28-
29-
/// A symbol, either a leaf or with modifiers.
30-
enum Symbol<'a> {
31-
Single(char),
32-
Multi(Vec<(&'a str, char)>),
33-
}
34-
3522
/// A single line during parsing.
3623
#[derive(Debug, Copy, Clone)]
3724
enum Line<'a> {

src/lib.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
Human-friendly notation for Unicode symbols.
33
*/
44

5-
/// A module of definitions.
6-
#[derive(Debug, Copy, Clone)]
7-
pub struct Module(&'static [(&'static str, Binding)]);
5+
include!("shared.rs");
6+
7+
type StaticSlice<T> = &'static [T];
8+
declare_types! {
9+
derive(Debug, Copy, Clone),
10+
str = &'static str,
11+
List = StaticSlice<_>
12+
}
813

914
impl Module {
1015
/// Try to get a bound definition in the module.
@@ -21,40 +26,6 @@ impl Module {
2126
}
2227
}
2328

24-
/// A definition bound in a module, with metadata.
25-
#[derive(Debug, Copy, Clone)]
26-
pub struct Binding {
27-
/// The bound definition.
28-
pub def: Def,
29-
/// A deprecation message for the definition, if it is deprecated.
30-
pub deprecation: Option<&'static str>,
31-
}
32-
33-
impl Binding {
34-
/// Create a new bound definition.
35-
pub const fn new(definition: Def) -> Self {
36-
Self { def: definition, deprecation: None }
37-
}
38-
}
39-
40-
/// A definition in a module.
41-
#[derive(Debug, Copy, Clone)]
42-
pub enum Def {
43-
/// A symbol, potentially with modifiers.
44-
Symbol(Symbol),
45-
/// A nested module.
46-
Module(Module),
47-
}
48-
49-
/// A symbol, either a leaf or with modifiers.
50-
#[derive(Debug, Copy, Clone)]
51-
pub enum Symbol {
52-
/// A symbol without modifiers.
53-
Single(char),
54-
/// A symbol with named modifiers. The symbol defaults to its first variant.
55-
Multi(&'static [(&'static str, char)]),
56-
}
57-
5829
/// A module that contains the other top-level modules.
5930
pub const ROOT: Module = Module(&[
6031
("emoji", Binding::new(Def::Module(EMOJI))),

src/shared.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
macro_rules! declare_types {
2+
($(<$lt:lifetime>)?
3+
$(derive($($Der:ident),*),)?
4+
str = $s:ty,
5+
List = $L:ident<_>
6+
) => {
7+
/// A module of definitions.
8+
$(#[derive($($Der),*)])?
9+
pub struct Module<$($lt)?>($L<($s, Binding<$($lt)?>)>);
10+
11+
/// A definition bound in a module, with metadata.
12+
$(#[derive($($Der),*)])?
13+
pub struct Binding<$($lt)?> {
14+
/// The bound definition.
15+
pub def: Def<$($lt)?>,
16+
/// A deprecation message for the definition, if it is deprecated.
17+
pub deprecation: Option<$s>,
18+
}
19+
20+
impl<$($lt)?> Binding<$($lt)?> {
21+
/// Create a new bound definition.
22+
pub const fn new(definition: Def<$($lt)?>) -> Self {
23+
Self { def: definition, deprecation: None }
24+
}
25+
}
26+
27+
/// A definition in a module.
28+
$(#[derive($($Der),*)])?
29+
pub enum Def<$($lt)?> {
30+
/// A symbol, potentially with modifiers.
31+
Symbol(Symbol<$($lt)?>),
32+
/// A nested module.
33+
Module(Module<$($lt)?>),
34+
}
35+
36+
/// A symbol, either a leaf or with modifiers.
37+
$(#[derive($($Der),*)])?
38+
pub enum Symbol<$($lt)?> {
39+
/// A symbol without modifiers.
40+
Single(char),
41+
/// A symbol with named modifiers. The symbol defaults to its first variant.
42+
Multi($L<($s, char)>),
43+
}
44+
};
45+
}

0 commit comments

Comments
 (0)