Skip to content

Commit dd23d1e

Browse files
authored
use newer cargo workspace features (#49)
1 parent 420004a commit dd23d1e

File tree

9 files changed

+50
-46
lines changed

9 files changed

+50
-46
lines changed

Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ categories = ["compression", "encoding", "parsing"]
99
repository = "https://github.com/danieleades/arithmetic-coding"
1010

1111
[workspace]
12-
members = [
13-
".",
14-
"arithmetic-coding-core",
15-
"fenwick-model",
16-
]
12+
members = [".", "arithmetic-coding-core", "fenwick-model"]
13+
14+
[workspace.dependencies]
15+
thiserror = "1.0.30"
16+
17+
[workspace.lints.clippy]
18+
cargo = "deny"
19+
all = "deny"
20+
nursery = "warn"
21+
pedantic = "warn"
1722

1823
[dependencies]
1924
arithmetic-coding-core = { path = "./arithmetic-coding-core", version = "0.3.0" }
2025
bitstream-io = "1.2.0"
21-
thiserror = "1.0.30"
26+
thiserror = { workspace = true }
2227

2328
[dev-dependencies]
2429
fenwick-model = { path = "./fenwick-model" }
@@ -28,3 +33,6 @@ test-case = "3.0.0"
2833
[[bench]]
2934
name = "sherlock"
3035
harness = false
36+
37+
[lints]
38+
workspace = true

arithmetic-coding-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ repository = "https://github.com/danieleades/arithmetic-coding"
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
thiserror = "1.0.30"
14+
thiserror = { workspace = true }
1515

16+
[lints]
17+
workspace = true

arithmetic-coding-core/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
//! Core traits for the [`arithmetic-coding`](https://github.com/danieleades/arithmetic-coding) crate
22
3-
#![deny(
4-
missing_docs,
5-
clippy::all,
6-
missing_debug_implementations,
7-
clippy::cargo
8-
)]
9-
#![warn(clippy::pedantic)]
3+
#![deny(missing_docs, missing_debug_implementations)]
104
#![feature(associated_type_defaults)]
115

126
mod bitstore;

arithmetic-coding-core/src/model/fixed_length.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ where
163163
symbol: Option<&Self::Symbol>,
164164
) -> Result<Range<Self::B>, Self::ValueError> {
165165
if self.remaining > 0 {
166-
if let Some(s) = symbol {
167-
// Expected a symbol and got one. return the probability.
168-
self.model.probability(s).map_err(Self::ValueError::Value)
169-
} else {
166+
symbol.map_or(
170167
// We are expecting more symbols, but got an EOF
171-
Err(Self::ValueError::UnexpectedEof)
172-
}
168+
Err(Self::ValueError::UnexpectedEof),
169+
// Expected a symbol and got one. return the probability.
170+
|s| self.model.probability(s).map_err(Self::ValueError::Value),
171+
)
173172
} else if symbol.is_some() {
174173
// we should be finished, but got an extra symbol
175174
Err(Error::UnexpectedSymbol)

examples/sherlock.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,20 @@ impl Model for StringModel {
2828
type Symbol = char;
2929
type ValueError = Error;
3030

31+
#[allow(clippy::range_plus_one)]
3132
fn probability(&self, symbol: Option<&Self::Symbol>) -> Result<Range<usize>, Error> {
32-
if let Some(char) = symbol {
33-
match self.alphabet.iter().position(|x| x == char) {
34-
Some(index) => Ok(index..(index + 1)),
35-
None => Err(Error(*char)),
36-
}
37-
} else {
38-
let alphabet_length = self.alphabet.len();
39-
Ok(alphabet_length..(alphabet_length + 1))
40-
}
33+
symbol.map_or_else(
34+
|| {
35+
let alphabet_length = self.alphabet.len();
36+
Ok(alphabet_length..(alphabet_length + 1))
37+
},
38+
|char| {
39+
self.alphabet
40+
.iter()
41+
.position(|x| x == char)
42+
.map_or(Err(Error(*char)), |index| Ok(index..(index + 1)))
43+
},
44+
)
4145
}
4246

4347
fn symbol(&self, value: usize) -> Option<Self::Symbol> {

src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
//! Arithmetic coding library
22
3-
#![deny(
4-
missing_docs,
5-
clippy::all,
6-
missing_debug_implementations,
7-
clippy::cargo
8-
)]
9-
#![warn(clippy::pedantic)]
3+
#![deny(missing_docs, missing_debug_implementations)]
104

115
pub use arithmetic_coding_core::{fixed_length, max_length, one_shot, BitStore, Model};
126

tests/fixed_length.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn round_trip() {
5555
}
5656

5757
#[test]
58-
#[should_panic]
58+
#[should_panic(expected = "UnexpectedSymbol")]
5959
fn round_trip_fail() {
6060
// this is too many symbols for this model
6161
let input = &[

tests/max_length.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl max_length::Model for MyModel {
5252

5353
#[test_case(&[Symbol::A, Symbol::B] ; "shorter")]
5454
#[test_case(&[Symbol::A, Symbol::B, Symbol::C] ; "exact")]
55-
#[test_case(&[Symbol::A, Symbol::B, Symbol::C, Symbol::C] => panics ; "longer")]
55+
#[test_case(&[Symbol::A, Symbol::B, Symbol::C, Symbol::C] => panics "UnexpectedSymbol" ; "longer")]
5656
fn round_trip(input: &[Symbol]) {
5757
common::round_trip(max_length::Wrapper::new(MyModel), input);
5858
}

tests/sherlock.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ impl Model for StringModel {
1919
type Symbol = char;
2020
type ValueError = Error;
2121

22+
#[allow(clippy::range_plus_one)]
2223
fn probability(&self, symbol: Option<&Self::Symbol>) -> Result<Range<Self::B>, Error> {
23-
if let Some(char) = symbol {
24-
match ALPHABET.chars().position(|x| &x == char) {
25-
Some(index) => Ok(index..(index + 1)),
26-
None => Err(Error(*char)),
27-
}
28-
} else {
29-
Ok(ALPHABET.len()..(ALPHABET.len() + 1))
30-
}
24+
symbol.map_or_else(
25+
|| Ok(ALPHABET.len()..(ALPHABET.len() + 1)),
26+
|char| {
27+
ALPHABET
28+
.chars()
29+
.position(|x| &x == char)
30+
.ok_or(Error(*char))
31+
.map(|index| index..(index + 1))
32+
},
33+
)
3134
}
3235

3336
fn symbol(&self, value: Self::B) -> Option<Self::Symbol> {

0 commit comments

Comments
 (0)