From 9f000cbdcb5bcff1f01b2d53ebe509bf4910af49 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 18 Apr 2024 21:21:21 +0200 Subject: [PATCH] Use iterators --- src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8361d49..1a48c03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,59 @@ -use std::collections::VecDeque; - use crate::rentry::{build_client, delete_paste, CSRFData, DeleteError}; mod rentry; const PASTE_NAME: &str = "Hyprland-controversy"; +pub struct EditCodeGenerator { + codes: Vec, + index: usize, +} + +impl EditCodeGenerator { + pub fn retry_last(&mut self) -> Option<()> { + if self.index > 0 { + self.index -= 1; + Some(()) + } else { + None + } + } +} + +impl Default for EditCodeGenerator { + fn default() -> Self { + let codes = (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())) + .collect(); + + EditCodeGenerator { codes, index: 0 } + } +} + +impl Iterator for EditCodeGenerator { + type Item = String; + + fn next(&mut self) -> Option { + if self.index < self.codes.len() { + let code = self.codes[self.index].clone(); + self.index += 1; + Some(code) + } else { + None + } + } +} + fn main() { let client = build_client(); let mut csrf_data = CSRFData::get(&client, PASTE_NAME); - let mut edit_codes = VecDeque::from([ - "qwack", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", - "v", "w", "x", "y", "z", "a", "b", "c", - ]); + let mut edit_codes = EditCodeGenerator::default(); - while let Some(edit_code) = edit_codes.pop_front() { - match delete_paste(&client, &csrf_data, edit_code, PASTE_NAME) { + while let Some(edit_code) = edit_codes.next() { + match delete_paste(&client, &csrf_data, &edit_code, PASTE_NAME) { Ok(_) => { println!("Paste deleted successfully with edit code: {edit_code}"); return; @@ -35,7 +72,7 @@ fn main() { println!("Hit the limit, waiting..."); csrf_data = CSRFData::get(&client, PASTE_NAME); std::thread::sleep(std::time::Duration::from_secs(30)); - edit_codes.push_front(edit_code); + edit_codes.retry_last().unwrap(); } DeleteError::FormError(msg) => { eprintln!("Form error: {msg}");