This commit is contained in:
Ivan Akimov 2023-07-18 13:54:58 -05:00
parent b5998810c4
commit aafb477633
3 changed files with 24 additions and 16 deletions

View File

@ -28,15 +28,13 @@ jobs:
- name: Lint - name: Lint
run: | run: |
rustfmt +nightly **/*.rs rustfmt +nightly **/*.rs
cargo clippy --all -- -D warnings -A clippy::redundant_async_block cargo clippy --all -- -D warnings
- name: Install cargo check tools - name: Install cargo check tools
run: | run: |
cargo install --locked cargo-deny || true
cargo install --locked cargo-outdated || true cargo install --locked cargo-outdated || true
cargo install --locked cargo-udeps || true cargo install --locked cargo-udeps || true
- name: Check - name: Check
run: | run: |
cargo deny check
cargo outdated --exit-code 1 cargo outdated --exit-code 1
cargo udeps cargo udeps
rm -rf ~/.cargo/advisory-db rm -rf ~/.cargo/advisory-db

View File

@ -1,6 +1,6 @@
use derive_more::Display; use derive_more::Display;
use std::collections::HashSet; use std::collections::HashSet;
use std::result::Result; use std::result;
#[derive(Display, Debug)] #[derive(Display, Debug)]
pub enum Error { pub enum Error {
@ -8,13 +8,15 @@ pub enum Error {
AlphabetLength, AlphabetLength,
#[display(fmt = "Alphabet must contain unique characters")] #[display(fmt = "Alphabet must contain unique characters")]
AlphabetUniqueCharacters, AlphabetUniqueCharacters,
#[display(fmt = "Minimum length has to be between {min} and {max}")]
MinLength { min: usize, max: usize },
#[display(fmt = "Encoding supports numbers between {min} and {max}")] #[display(fmt = "Encoding supports numbers between {min} and {max}")]
EncodingRange { min: u64, max: u64 }, EncodingRange { min: u64, max: u64 },
#[display(fmt = "Ran out of range checking against the blocklist")] #[display(fmt = "Ran out of range checking against the blocklist")]
BlocklistOutOfRange, BlocklistOutOfRange,
} }
pub type SqidsResult<T> = Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)] #[derive(Debug)]
pub struct Options { pub struct Options {
@ -41,7 +43,7 @@ pub struct Sqids {
} }
impl Sqids { impl Sqids {
pub fn new(options: Option<Options>) -> SqidsResult<Self> { pub fn new(options: Option<Options>) -> Result<Self> {
let options = options.unwrap_or_default(); let options = options.unwrap_or_default();
let alphabet: Vec<char> = options.alphabet.chars().collect(); let alphabet: Vec<char> = options.alphabet.chars().collect();
@ -73,11 +75,20 @@ impl Sqids {
blocklist: filtered_blocklist, blocklist: filtered_blocklist,
}; };
if options.min_length < sqids.min_value() as usize
|| options.min_length > options.alphabet.len()
{
return Err(Error::MinLength {
min: sqids.min_value() as usize,
max: options.alphabet.len(),
});
}
sqids.alphabet = sqids.shuffle(&sqids.alphabet); sqids.alphabet = sqids.shuffle(&sqids.alphabet);
Ok(sqids) Ok(sqids)
} }
pub fn encode(&self, numbers: &[u64]) -> SqidsResult<String> { pub fn encode(&self, numbers: &[u64]) -> Result<String> {
if numbers.is_empty() { if numbers.is_empty() {
return Ok(String::new()); return Ok(String::new());
} }
@ -165,12 +176,12 @@ impl Sqids {
u64::MAX u64::MAX
} }
fn encode_numbers(&self, numbers: &[u64], partitioned: bool) -> SqidsResult<String> { fn encode_numbers(&self, numbers: &[u64], partitioned: bool) -> Result<String> {
let offset = numbers let offset = numbers
.iter() .iter()
.enumerate() .enumerate()
.fold(numbers.len(), |a, (i, &v)| { .fold(numbers.len(), |a, (i, &v)| {
(v as usize % self.alphabet.len()) + i + a self.alphabet[v as usize % self.alphabet.len()] as usize + i + a
}) })
% self.alphabet.len(); % self.alphabet.len();

View File

@ -1,12 +1,11 @@
// use sqids::*; use sqids::*;
#[test] #[test]
fn simple() { fn simple() {
assert_eq!(1, 1); let id = "8QRLaD".to_string();
// let id = "8QRLaD".to_string(); let numbers = vec![1, 2, 3];
// let numbers = vec![1, 2, 3];
// let sqids = Sqids::new(None).unwrap(); let sqids = Sqids::new(None).unwrap();
// assert_eq!(sqids.encode(&numbers).unwrap(), id); assert_eq!(sqids.encode(&numbers).unwrap(), id);
// assert_eq!(sqids.decode(&id), numbers); assert_eq!(sqids.decode(&id), numbers);
} }