Skip to content

Commit

Permalink
docs: Initial version of public API surface
Browse files Browse the repository at this point in the history
Also fixed `pedantic` `clippy` lints
  • Loading branch information
alexpovel committed May 29, 2023
1 parent 8a93f60 commit f9086dd
Show file tree
Hide file tree
Showing 43 changed files with 759 additions and 356 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Alex Povel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# betterletter

Substitute alternative, ASCII-only spellings of special characters with their Unicode
equivalents.
5 changes: 5 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
name = "common"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4.17"
itertools = "0.10.5"
paste = "1.0.12"
serde = { version = "1.0.163", features = ["derive"] }

[dev-dependencies]
rstest = "0.17.0"
insta = { version = "1.29.0", features = ["yaml"] }
65 changes: 65 additions & 0 deletions common/src/itertools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use itertools::Itertools;

pub fn _power_set<C, T>(collection: C) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
power_set_impl(collection, true)
}

pub fn power_set_without_empty<C, T>(collection: C) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
power_set_impl(collection, false)
}

fn power_set_impl<C, T>(collection: C, include_empty_set: bool) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
let vec = collection.into_iter().collect_vec();

// https://en.wikipedia.org/wiki/Power_set#Properties
let mut result = Vec::with_capacity(2usize.checked_pow(vec.len() as u32).expect("Overflow"));

let start = if include_empty_set { 0 } else { 1 };

for i in start..=vec.len() {
result.extend(vec.iter().cloned().combinations(i));
}

result
}

#[cfg(test)]
mod tests {
use super::{_power_set, power_set_without_empty};
use crate::instrament;
use rstest::rstest;

instrament! {
#[rstest]
fn test_power_set(
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])]
collection: Vec<i32>
) (|data: &TestPowerSet| {
let result = _power_set(collection.clone());
insta::assert_yaml_snapshot!(data.to_string(), result);
})
}

instrament! {
#[rstest]
fn test_power_set_without_empty(
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])]
collection: Vec<i32>
) (|data: &TestPowerSetWithoutEmpty| {
let result = power_set_without_empty(collection.clone());
insta::assert_yaml_snapshot!(data.to_string(), result);
})
}
}
95 changes: 4 additions & 91 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,6 @@
use log::trace;
#![allow(clippy::cargo_common_metadata)]

pub mod instrament;

pub fn titlecase(word: &str) -> String {
let mut chars = word.chars();
let mut result = String::with_capacity(word.len());

if let Some(c) = chars.next() {
for upper in c.to_uppercase() {
result.push(upper);
}
}

for c in chars {
for lower in c.to_lowercase() {
result.push(lower);
}
}

result
}

pub fn is_compound_word(word: &str, predicate: &impl Fn(&str) -> bool) -> bool {
trace!("Checking if word is valid compound word: '{}'", word);

let indices = word.char_indices().skip(1);

// Greedily fetch the longest possible prefix. Otherwise, we short-circuit and might
// end up looking for (for example) "He" of "Heizölrechnung" and its suffix
// "izölrechnung" (not a word), whereas we could have found "Heizöl" and "Rechnung"
// instead.
let mut highest_valid_index = None;
for (i, _) in indices {
let prefix = &word[..i];

if predicate(prefix) {
highest_valid_index = Some(i);
}
}

match highest_valid_index {
Some(i) => {
let suffix = &word[i..];

trace!(
"Prefix '{}' found in word list, seeing if suffix '{}' is valid.",
&word[..i],
suffix
);

predicate(&titlecase(suffix))
|| predicate(suffix)
|| is_compound_word(&titlecase(suffix), predicate)
|| is_compound_word(suffix, predicate)
}
None => false,
}
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[rstest]
#[case("hello", "Hello")]
#[case("bItTe", "Bitte")]
#[case("dANKE", "Danke")]
#[case("übel", "Übel")]
#[case("uebel", "Uebel")]
#[case("😀", "😀")]
#[case("ßuper", "SSuper")]
#[case("ẞuperduper", "ẞuperduper")]
#[case("WOW!!", "Wow!!")]
#[case("ẞß", "ẞß")]
fn test_titlecase(#[case] word: &str, #[case] expected: &str) {
assert_eq!(titlecase(word), expected);
}

const WORDS: &[&str] = &["Süßwasser", "schwimm", "Bäder", "Mauer", "Dübel", "Kübel"];

#[rstest]
#[case("Süßwasserschwimmbäder", true)]
#[case("Mauerdübel", true)]
#[case("Mauerdübelkübel", true)]
#[case("Not a compound word", false)]
#[case("Mauer好", false)]
#[case("Mauerdjieojoid", false)]
fn test_is_compound_word(#[case] word: &str, #[case] expected: bool) {
assert_eq!(is_compound_word(word, &|w| WORDS.contains(&w)), expected);
}
}
pub mod itertools;
pub mod lookup;
pub mod strings;
62 changes: 2 additions & 60 deletions core/src/util/iteration.rs → common/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,6 @@ use itertools::Itertools;
use std::cmp::Ordering;
use std::str;

pub fn _power_set<C, T>(collection: C) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
power_set_impl(collection, true)
}

pub fn power_set_without_empty<C, T>(collection: C) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
power_set_impl(collection, false)
}

fn power_set_impl<C, T>(collection: C, include_empty_set: bool) -> Vec<Vec<T>>
where
C: IntoIterator<Item = T>,
T: Clone,
{
let vec = collection.into_iter().collect_vec();

// https://en.wikipedia.org/wiki/Power_set#Properties
let mut result = Vec::with_capacity(2usize.checked_pow(vec.len() as u32).expect("Overflow"));

let start = if include_empty_set { 0 } else { 1 };

for i in start..=vec.len() {
result.extend(vec.iter().cloned().combinations(i));
}

result
}

pub fn binary_search_uneven(needle: &str, haystack: &str, sep: char) -> bool {
if needle.is_empty() {
return true;
Expand Down Expand Up @@ -83,32 +48,9 @@ pub fn binary_search_uneven(needle: &str, haystack: &str, sep: char) -> bool {

#[cfg(test)]
mod tests {
use super::{_power_set, power_set_without_empty};
use common::instrament;
use super::binary_search_uneven;
use rstest::rstest;

instrament! {
#[rstest]
fn test_power_set(
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])]
collection: Vec<i32>
) (|data: &TestPowerSet| {
let result = _power_set(collection.clone());
insta::assert_yaml_snapshot!(data.to_string(), result);
})
}

instrament! {
#[rstest]
fn test_power_set_without_empty(
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])]
collection: Vec<i32>
) (|data: &TestPowerSetWithoutEmpty| {
let result = power_set_without_empty(collection.clone());
insta::assert_yaml_snapshot!(data.to_string(), result);
})
}

#[rstest]
// Base cases, all elements present in any position.
#[case("abc", "abc,def,ghi,jkl,mno,pqr,stu,vwx,yz", ',', true)]
Expand Down Expand Up @@ -173,6 +115,6 @@ mod tests {
#[case] sep: char,
#[case] expected: bool,
) {
assert_eq!(super::binary_search_uneven(needle, haystack, sep), expected);
assert_eq!(binary_search_uneven(needle, haystack, sep), expected);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection: []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection: []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: core/src/util/iteration.rs
source: common/src/itertools.rs
expression: result
info:
collection:
Expand Down
Loading

0 comments on commit f9086dd

Please sign in to comment.