Skip to content

Commit

Permalink
Move macros into their calling modules
Browse files Browse the repository at this point in the history
This causes some duplication but these macros are very simple and short.
The issue is that we can't describe a macro as available throughout a
crate but not exported from that crate. Basically what we want is a
`pub(crate)` macro, and that doesn't seem to be possible in the current
Rust edition. If that's fixed in the future we could reintroduce this
macros module and move all of the macros there.

This removes the macros from the `cargo doc` output notably.
  • Loading branch information
the-mikedavis committed Sep 3, 2024
1 parent 7fd0e8b commit 521e7cf
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 43 deletions.
28 changes: 26 additions & 2 deletions src/aff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ use crate::{
string::{String, ToString},
vec::Vec,
},
has_flag, AffixingMode, Flag, FlagSet, AT_COMPOUND_BEGIN, AT_COMPOUND_END, FULL_WORD,
AffixingMode, Flag, FlagSet, AT_COMPOUND_BEGIN, AT_COMPOUND_END, FULL_WORD,
};

use core::{marker::PhantomData, num::NonZeroU16, str::Chars};

pub(crate) const HIDDEN_HOMONYM_FLAG: Flag = unsafe { Flag::new_unchecked(u16::MAX) };
pub(crate) const MAX_SUGGESTIONS: usize = 16;

macro_rules! has_flag {
( $flags:expr, $flag:expr ) => {{
match $flag {
Some(flag) => $flags.contains(&flag),
None => false,
}
}};
}

/// The representation of a flag in a `.dic` or `.aff` file.
///
/// This representation also decides how we encode flags into `Flag`. This is controlled by the
Expand Down Expand Up @@ -1247,7 +1256,22 @@ impl Default for AffOptions {
#[cfg(test)]
mod test {
use super::*;
use crate::*;

macro_rules! flag {
( $x:expr ) => {{
Flag::new($x as u16).unwrap()
}};
}
macro_rules! flagset {
() => {{
FlagSet::empty()
}};
( $( $x:expr ),* ) => {
{
FlagSet::from( $crate::alloc::vec![ $( Flag::new( $x as u16 ).unwrap() ),* ] )
}
};
}

#[test]
fn condition_matches() {
Expand Down
27 changes: 21 additions & 6 deletions src/aff/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ use crate::{
string::{String, ToString},
vec::Vec,
},
erase_chars, has_flag, Casing, WordList,
erase_chars, Casing, Flag, FlagSet, WordList,
};

use crate::{Flag, FlagSet};

use super::{
AffData, AffOptions, BreakTable, CaseHandling, CompoundPattern, CompoundRule, Condition,
FlagType, Prefix, Suffix, HIDDEN_HOMONYM_FLAG,
Expand Down Expand Up @@ -197,7 +195,10 @@ pub(crate) fn parse<'dic, 'aff, S: BuildHasher + Clone>(
// converting them to titlecase and setting the hidden homonym flag.
let casing = crate::classify_casing(&word);
if (matches!(casing, Casing::Pascal | Casing::Camel)
&& !has_flag!(flagset, cx.options.forbidden_word_flag))
&& !cx
.options
.forbidden_word_flag
.is_some_and(|flag| flagset.contains(&flag)))
|| (matches!(casing, Casing::All) && !flagset.is_empty())
{
let word = cx.options.case_handling.titlecase(&word).into();
Expand Down Expand Up @@ -1747,10 +1748,24 @@ impl FromStr for Condition {

#[cfg(test)]
mod test {
use crate::{flag, flagset};

use super::*;

macro_rules! flag {
( $x:expr ) => {{
Flag::new($x as u16).unwrap()
}};
}
macro_rules! flagset {
() => {{
FlagSet::empty()
}};
( $( $x:expr ),* ) => {
{
FlagSet::from( $crate::alloc::vec![ $( Flag::new( $x as u16 ).unwrap() ),* ] )
}
};
}

#[test]
fn naive_word_flagset_split_test() {
assert_eq!(
Expand Down
18 changes: 16 additions & 2 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ use crate::{
AffData, Affix, AffixKind, CompoundPattern, Pfx, Prefix, Sfx, Suffix, HIDDEN_HOMONYM_FLAG,
},
alloc::{string::String, vec::Vec},
classify_casing, erase_chars, flag, has_flag, AffixingMode, Casing, Dictionary, Flag, FlagSet,
WordList, AT_COMPOUND_BEGIN, AT_COMPOUND_END, AT_COMPOUND_MIDDLE, FULL_WORD,
classify_casing, erase_chars, AffixingMode, Casing, Dictionary, Flag, FlagSet, WordList,
AT_COMPOUND_BEGIN, AT_COMPOUND_END, AT_COMPOUND_MIDDLE, FULL_WORD,
};

// Nuspell limits the length of the input word:
// <https://github.com/nuspell/nuspell/blob/349e0d6bc68b776af035ca3ff664a7fc55d69387/src/nuspell/dictionary.cxx#L156>
const MAX_WORD_LEN: usize = 360;

macro_rules! has_flag {
( $flags:expr, $flag:expr ) => {{
match $flag {
Some(flag) => $flags.contains(&flag),
None => false,
}
}};
}
macro_rules! flag {
( $x:expr ) => {{
Flag::new($x as u16).unwrap()
}};
}

// TODO: expose type and add options to it?
pub(crate) struct Checker<'a, S: BuildHasher> {
words: &'a WordList<S>,
Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extern crate alloc;
pub(crate) mod aff;
pub(crate) mod checker;
mod hash_bag;
pub(crate) mod macros;

pub use aff::parser::{ParseDictionaryError, ParseDictionaryErrorKind, ParseDictionaryErrorSource};

Expand Down Expand Up @@ -377,6 +376,22 @@ pub(crate) fn erase_chars<'a>(word: &'a str, ignore: &[char]) -> Cow<'a, str> {
mod test {
use super::*;

macro_rules! flag {
( $x:expr ) => {{
Flag::new($x as u16).unwrap()
}};
}
macro_rules! flagset {
() => {{
FlagSet::empty()
}};
( $( $x:expr ),* ) => {
{
FlagSet::from( $crate::alloc::vec![ $( Flag::new( $x as u16 ).unwrap() ),* ] )
}
};
}

#[test]
fn flagset_display() {
assert_eq!("flagset![1]", &alloc::format!("{:?}", flagset![1]));
Expand Down
32 changes: 0 additions & 32 deletions src/macros.rs

This file was deleted.

0 comments on commit 521e7cf

Please sign in to comment.