From f4400c2280424a16f095c6da335eb59c5f52f236 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 27 May 2020 20:51:04 -0500 Subject: [PATCH] refactor(varcon): Pull out core types --- Cargo.lock | 7 +- Cargo.toml | 2 +- .../{varcon-parser => varcon-core}/Cargo.toml | 15 +++- crates/varcon-core/src/borrowed.rs | 54 +++++++++++++ .../{varcon-parser => varcon-core}/src/lib.rs | 55 ++++++++----- .../src/parser.rs | 0 crates/varcon/Cargo.toml | 4 +- crates/varcon/codegen/Cargo.toml | 2 +- crates/varcon/codegen/src/main.rs | 2 +- crates/varcon/src/lib.rs | 79 +++---------------- 10 files changed, 122 insertions(+), 98 deletions(-) rename crates/{varcon-parser => varcon-core}/Cargo.toml (54%) create mode 100644 crates/varcon-core/src/borrowed.rs rename crates/{varcon-parser => varcon-core}/src/lib.rs (66%) rename crates/{varcon-parser => varcon-core}/src/parser.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index e7b1b73..ea264ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,7 +1095,7 @@ checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" name = "varcon" version = "0.2.1" dependencies = [ - "enumflags2", + "varcon-core", ] [[package]] @@ -1104,13 +1104,14 @@ version = "1.0.2" dependencies = [ "codegenrs", "structopt", - "varcon-parser", + "varcon-core", ] [[package]] -name = "varcon-parser" +name = "varcon-core" version = "1.0.0" dependencies = [ + "enumflags2", "nom", ] diff --git a/Cargo.toml b/Cargo.toml index e32fbc9..bc011ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = [ "crates/codespell-dict", "crates/codespell-dict/codegen", "crates/misspell-dict", "crates/misspell-dict/codegen", "crates/wikipedia-dict", "crates/wikipedia-dict/codegen", - "crates/varcon", "crates/varcon/codegen", "crates/varcon-parser", + "crates/varcon", "crates/varcon/codegen", "crates/varcon-core", ] [package] diff --git a/crates/varcon-parser/Cargo.toml b/crates/varcon-core/Cargo.toml similarity index 54% rename from crates/varcon-parser/Cargo.toml rename to crates/varcon-core/Cargo.toml index 64d784d..2b5ab2d 100644 --- a/crates/varcon-parser/Cargo.toml +++ b/crates/varcon-core/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "varcon-parser" +name = "varcon-core" version = "1.0.0" authors = ["Ed Page "] -description = "Parse varcon.txt file" +description = "Varcon-relevant data structures" repository = "https://github.com/crate-ci/typos" readme = "../../../README.md" categories = ["text-processing"] @@ -13,5 +13,14 @@ edition = "2018" azure-devops = { project = "crate-ci", pipeline = "typos" } codecov = { repository = "crate-ci/typos" } +[features] +default = [] +parser = ["nom"] +flags = ["enumflags2"] + [dependencies] -nom = "5.1.1" +nom = { version = "5.1.1", optional = true } +enumflags2 = { version = "0.6", optional = true } + +[package.metadata.docs.rs] +features = [ "parser", "flags" ] diff --git a/crates/varcon-core/src/borrowed.rs b/crates/varcon-core/src/borrowed.rs new file mode 100644 index 0000000..937c5dc --- /dev/null +++ b/crates/varcon-core/src/borrowed.rs @@ -0,0 +1,54 @@ +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +pub struct Cluster { + pub header: Option<&'static str>, + pub entries: &'static [Entry], + pub notes: &'static [&'static str], +} + +impl Cluster { + pub fn into_owned(self) -> crate::Cluster { + crate::Cluster { + header: self.header.map(|s| s.to_owned()), + entries: self.entries.iter().map(|s| s.into_owned()).collect(), + notes: self.notes.iter().map(|s| (*s).to_owned()).collect(), + } + } +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +pub struct Entry { + pub variants: &'static [Variant], + pub pos: Option, + pub archaic: bool, + pub note: bool, + pub description: Option<&'static str>, + pub comment: Option<&'static str>, +} + +impl Entry { + pub fn into_owned(self) -> crate::Entry { + crate::Entry { + variants: self.variants.iter().map(|v| v.into_owned()).collect(), + pos: self.pos, + archaic: self.archaic, + note: self.note, + description: self.description.map(|s| s.to_owned()), + comment: self.comment.map(|s| s.to_owned()), + } + } +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +pub struct Variant { + pub types: &'static [crate::Type], + pub word: &'static str, +} + +impl Variant { + pub fn into_owned(self) -> crate::Variant { + crate::Variant { + types: self.types.iter().copied().collect(), + word: self.word.to_owned(), + } + } +} diff --git a/crates/varcon-parser/src/lib.rs b/crates/varcon-core/src/lib.rs similarity index 66% rename from crates/varcon-parser/src/lib.rs rename to crates/varcon-core/src/lib.rs index 76c341d..7efd721 100644 --- a/crates/varcon-parser/src/lib.rs +++ b/crates/varcon-core/src/lib.rs @@ -1,6 +1,10 @@ +pub mod borrowed; + +#[cfg(feature = "parser")] mod parser; -pub use parser::ClusterIter; +#[cfg(feature = "parser")] +pub use crate::parser::ClusterIter; #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct Cluster { @@ -72,7 +76,7 @@ pub struct Variant { pub word: String, } -#[derive(Clone, PartialEq, Eq, Hash, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct Type { pub category: Category, pub tag: Option, @@ -80,28 +84,43 @@ pub struct Type { } #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] +#[repr(u8)] pub enum Category { - American, - BritishIse, - BritishIze, - Canadian, - Australian, - Other, + American = 0x01, + BritishIse = 0x02, + BritishIze = 0x04, + Canadian = 0x08, + Australian = 0x10, + Other = 0x20, } -#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +#[cfg(feature = "flags")] +pub type CategorySet = enumflags2::BitFlags; + +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] +#[repr(u8)] pub enum Tag { - Eq, - Variant, - Seldom, - Possible, - Improper, + Eq = 0x01, + Variant = 0x02, + Seldom = 0x04, + Possible = 0x08, + Improper = 0x10, } +#[cfg(feature = "flags")] +pub type TagSet = enumflags2::BitFlags; + #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] +#[repr(u8)] pub enum Pos { - Noun, - Verb, - Adjective, - Adverb, + Noun = 0x01, + Verb = 0x02, + Adjective = 0x04, + Adverb = 0x08, } + +#[cfg(feature = "flags")] +pub type PosSet = enumflags2::BitFlags; diff --git a/crates/varcon-parser/src/parser.rs b/crates/varcon-core/src/parser.rs similarity index 100% rename from crates/varcon-parser/src/parser.rs rename to crates/varcon-core/src/parser.rs diff --git a/crates/varcon/Cargo.toml b/crates/varcon/Cargo.toml index e8bbb7f..31d6c46 100644 --- a/crates/varcon/Cargo.toml +++ b/crates/varcon/Cargo.toml @@ -17,7 +17,7 @@ codecov = { repository = "crate-ci/typos" } [features] default = ["all"] all = ["flags"] -flags = ["enumflags2"] +flags = ["varcon-core/flags"] [dependencies] -enumflags2 = { version = "0.6", optional = true } +varcon-core = { version = "1.0", path = "../varcon-core" } diff --git a/crates/varcon/codegen/Cargo.toml b/crates/varcon/codegen/Cargo.toml index c7eeefb..7e808e4 100644 --- a/crates/varcon/codegen/Cargo.toml +++ b/crates/varcon/codegen/Cargo.toml @@ -17,4 +17,4 @@ codecov = { repository = "crate-ci/typos" } [dependencies] codegenrs = "0.1" structopt = "0.3" -varcon-parser = { version = "1.0", path = "../../varcon-parser" } +varcon-core = { version = "1.0", path = "../../varcon-core", features = ["parser"] } diff --git a/crates/varcon/codegen/src/main.rs b/crates/varcon/codegen/src/main.rs index 43721d0..524472e 100644 --- a/crates/varcon/codegen/src/main.rs +++ b/crates/varcon/codegen/src/main.rs @@ -4,7 +4,7 @@ const DICT: &[u8] = include_bytes!("../../assets/varcon.txt"); fn generate(file: &mut W) { let dict = String::from_utf8_lossy(DICT); - let clusters = varcon_parser::ClusterIter::new(&dict); + let clusters = varcon_core::ClusterIter::new(&dict); writeln!( file, diff --git a/crates/varcon/src/lib.rs b/crates/varcon/src/lib.rs index bb4be6b..8fbf870 100644 --- a/crates/varcon/src/lib.rs +++ b/crates/varcon/src/lib.rs @@ -1,75 +1,16 @@ mod codegen; pub use codegen::*; - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct Cluster { - pub header: Option<&'static str>, - pub entries: &'static [Entry], - pub notes: &'static [&'static str], -} - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct Entry { - pub variants: &'static [Variant], - pub pos: Option, - pub archaic: bool, - pub note: bool, - pub description: Option<&'static str>, - pub comment: Option<&'static str>, -} - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct Variant { - pub types: &'static [Type], - pub word: &'static str, -} - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct Type { - pub category: Category, - pub tag: Option, - pub num: Option, -} - -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] -#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] -#[repr(u8)] -pub enum Category { - American = 0x01, - BritishIse = 0x02, - BritishIze = 0x04, - Canadian = 0x08, - Australian = 0x10, - Other = 0x20, -} - +pub use varcon_core::borrowed::Cluster; +pub use varcon_core::borrowed::Entry; +pub use varcon_core::borrowed::Variant; +pub use varcon_core::Category; #[cfg(feature = "flags")] -pub type CategorySet = enumflags2::BitFlags; - -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] -#[repr(u8)] -pub enum Tag { - Eq = 0x01, - Variant = 0x02, - Seldom = 0x04, - Possible = 0x08, - Improper = 0x10, -} - +pub use varcon_core::CategorySet; +pub use varcon_core::Pos; #[cfg(feature = "flags")] -pub type TagSet = enumflags2::BitFlags; - -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] -#[cfg_attr(feature = "flags", derive(enumflags2::BitFlags))] -#[repr(u8)] -pub enum Pos { - Noun = 0x01, - Verb = 0x02, - Adjective = 0x04, - Adverb = 0x08, -} - +pub use varcon_core::PosSet; +pub use varcon_core::Tag; #[cfg(feature = "flags")] -pub type PosSet = enumflags2::BitFlags; +pub use varcon_core::TagSet; +pub use varcon_core::Type;