Skip to content

Commit

Permalink
Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
T0mstone committed Feb 3, 2025
1 parent a806979 commit a3a16a7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
27 changes: 7 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ use std::path::Path;

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

/// A module of definitions.
struct Module<'a>(Vec<(&'a str, Binding<'a>)>);
include!("src/shared.rs");

declare_types!{
<'a>
str = &'a str,
List = Vec<_>
}

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

/// A definition bound in a module, with metadata.
struct Binding<'a> {
def: Def<'a>,
deprecation: Option<&'a str>,
}

/// A definition in a module.
enum Def<'a> {
Symbol(Symbol<'a>),
Module(Module<'a>),
}

/// A symbol, either a leaf or with modifiers.
enum Symbol<'a> {
Single(char),
Multi(Vec<(&'a str, char)>),
}

/// A single line during parsing.
#[derive(Debug, Copy, Clone)]
enum Line<'a> {
Expand Down
45 changes: 8 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
Human-friendly notation for Unicode symbols.
*/

/// A module of definitions.
#[derive(Debug, Copy, Clone)]
pub struct Module(&'static [(&'static str, Binding)]);
include!("shared.rs");

type StaticSlice<T> = &'static [T];
declare_types! {
derive(Debug, Copy, Clone),
str = &'static str,
List = StaticSlice<_>
}

impl Module {
/// Try to get a bound definition in the module.
Expand All @@ -21,40 +26,6 @@ impl Module {
}
}

/// A definition bound in a module, with metadata.
#[derive(Debug, Copy, Clone)]
pub struct Binding {
/// The bound definition.
pub def: Def,
/// A deprecation message for the definition, if it is deprecated.
pub deprecation: Option<&'static str>,
}

impl Binding {
/// Create a new bound definition.
pub const fn new(definition: Def) -> Self {
Self { def: definition, deprecation: None }
}
}

/// A definition in a module.
#[derive(Debug, Copy, Clone)]
pub enum Def {
/// A symbol, potentially with modifiers.
Symbol(Symbol),
/// A nested module.
Module(Module),
}

/// A symbol, either a leaf or with modifiers.
#[derive(Debug, Copy, Clone)]
pub enum Symbol {
/// A symbol without modifiers.
Single(char),
/// A symbol with named modifiers. The symbol defaults to its first variant.
Multi(&'static [(&'static str, char)]),
}

/// A module that contains the other top-level modules.
pub const ROOT: Module = Module(&[
("emoji", Binding::new(Def::Module(EMOJI))),
Expand Down
45 changes: 45 additions & 0 deletions src/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
macro_rules! declare_types {
($(<$lt:lifetime>)?
$(derive($($Der:ident),*),)?
str = $s:ty,
List = $L:ident<_>
) => {
/// A module of definitions.
$(#[derive($($Der),*)])?
pub struct Module<$($lt)?>($L<($s, Binding<$($lt)?>)>);

/// A definition bound in a module, with metadata.
$(#[derive($($Der),*)])?
pub struct Binding<$($lt)?> {
/// The bound definition.
pub def: Def<$($lt)?>,
/// A deprecation message for the definition, if it is deprecated.
pub deprecation: Option<$s>,
}

impl<$($lt)?> Binding<$($lt)?> {
/// Create a new bound definition.
pub const fn new(definition: Def<$($lt)?>) -> Self {
Self { def: definition, deprecation: None }
}
}

/// A definition in a module.
$(#[derive($($Der),*)])?
pub enum Def<$($lt)?> {
/// A symbol, potentially with modifiers.
Symbol(Symbol<$($lt)?>),
/// A nested module.
Module(Module<$($lt)?>),
}

/// A symbol, either a leaf or with modifiers.
$(#[derive($($Der),*)])?
pub enum Symbol<$($lt)?> {
/// A symbol without modifiers.
Single(char),
/// A symbol with named modifiers. The symbol defaults to its first variant.
Multi($L<($s, char)>),
}
};
}

0 comments on commit a3a16a7

Please sign in to comment.