Use proper iterators
This commit is contained in:
parent
e1d70f424e
commit
567da290dd
65
src/main.rs
65
src/main.rs
|
@ -4,15 +4,30 @@ mod rentry;
|
||||||
|
|
||||||
const PASTE_NAME: &str = "Hyprland-controversy";
|
const PASTE_NAME: &str = "Hyprland-controversy";
|
||||||
|
|
||||||
pub struct EditCodeGenerator {
|
pub struct EditCodeGenerator<I>
|
||||||
codes: Vec<String>,
|
where
|
||||||
index: usize,
|
I: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
inner: I,
|
||||||
|
last_item: Option<String>,
|
||||||
|
retry: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditCodeGenerator {
|
impl<I> EditCodeGenerator<I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
pub fn new(inner: I) -> Self {
|
||||||
|
EditCodeGenerator {
|
||||||
|
inner,
|
||||||
|
last_item: None,
|
||||||
|
retry: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn retry_last(&mut self) -> Option<()> {
|
pub fn retry_last(&mut self) -> Option<()> {
|
||||||
if self.index > 0 {
|
if let Some(ref _item) = self.last_item {
|
||||||
self.index -= 1;
|
self.retry = true;
|
||||||
Some(())
|
Some(())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -20,29 +35,33 @@ impl EditCodeGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for EditCodeGenerator {
|
impl<I> Iterator for EditCodeGenerator<I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
type Item = String;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.retry {
|
||||||
|
self.retry = false;
|
||||||
|
return self.last_item.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let next_item = self.inner.next();
|
||||||
|
self.last_item = next_item.clone();
|
||||||
|
next_item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EditCodeGenerator<std::vec::IntoIter<String>> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let codes = (b'a'..=b'z')
|
let codes = (b'a'..=b'z')
|
||||||
.map(|c| (c as char).to_string())
|
.map(|c| (c as char).to_string())
|
||||||
.chain((b'A'..=b'Z').map(|c| (c as char).to_string()))
|
.chain((b'A'..=b'Z').map(|c| (c as char).to_string()))
|
||||||
.chain((b'0'..=b'9').map(|c| (c as char).to_string()))
|
.chain((b'0'..=b'9').map(|c| (c as char).to_string()))
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
EditCodeGenerator { codes, index: 0 }
|
EditCodeGenerator::new(codes.into_iter())
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for EditCodeGenerator {
|
|
||||||
type Item = String;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
if self.index < self.codes.len() {
|
|
||||||
let code = self.codes[self.index].clone();
|
|
||||||
self.index += 1;
|
|
||||||
Some(code)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue