Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use end of range instead of size for custom-query with preceeding zeros #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions crates/producer/src/custom_query.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::Producer;

pub struct CustomQuery {
inner: Vec<Box<dyn Iterator<Item = usize>>>,
inner: Vec<(usize, Box<dyn Iterator<Item = usize>>)>,
size: usize,
prefix: String,
suffix: String,
min_digits: usize,
}

impl CustomQuery {
Expand All @@ -18,19 +17,19 @@ impl CustomQuery {
let (start, end) = Self::parse_range(&range);
size += end - start;
let iterator: Box<dyn Iterator<Item = usize>> = Box::new(start..end);
inner.push(iterator);
let min_digits = if add_preceding_zeros {
(end - 1).to_string().len()
} else {
0
};
inner.push((min_digits, iterator));
}

Self {
inner,
size,
prefix,
suffix,
min_digits: if add_preceding_zeros {
size.to_string().len()
} else {
0
},
}
}

Expand Down Expand Up @@ -61,10 +60,10 @@ impl CustomQuery {

impl Producer for CustomQuery {
fn next(&mut self) -> Result<Option<Vec<u8>>, String> {
for iter in &mut self.inner {
for (min_digits, iter) in &mut self.inner {
match iter.next() {
Some(value) => {
let full_number = format!("{:0>width$}", value, width = self.min_digits);
let full_number = format!("{:0>width$}", value, width = min_digits);
return Ok(Some(
format!("{}{}{}", self.prefix, full_number, self.suffix).into_bytes(),
));
Expand Down