Use iterators

This commit is contained in:
ItsDrike 2024-04-18 21:21:21 +02:00
parent 9cc9f5dd02
commit 9f000cbdcb
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

@ -1,22 +1,59 @@
use std::collections::VecDeque;
use crate::rentry::{build_client, delete_paste, CSRFData, DeleteError}; use crate::rentry::{build_client, delete_paste, CSRFData, DeleteError};
mod rentry; mod rentry;
const PASTE_NAME: &str = "Hyprland-controversy"; const PASTE_NAME: &str = "Hyprland-controversy";
pub struct EditCodeGenerator {
codes: Vec<String>,
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<Self::Item> {
if self.index < self.codes.len() {
let code = self.codes[self.index].clone();
self.index += 1;
Some(code)
} else {
None
}
}
}
fn main() { fn main() {
let client = build_client(); let client = build_client();
let mut csrf_data = CSRFData::get(&client, PASTE_NAME); let mut csrf_data = CSRFData::get(&client, PASTE_NAME);
let mut edit_codes = VecDeque::from([ let mut edit_codes = EditCodeGenerator::default();
"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",
]);
while let Some(edit_code) = edit_codes.pop_front() { while let Some(edit_code) = edit_codes.next() {
match delete_paste(&client, &csrf_data, edit_code, PASTE_NAME) { match delete_paste(&client, &csrf_data, &edit_code, PASTE_NAME) {
Ok(_) => { Ok(_) => {
println!("Paste deleted successfully with edit code: {edit_code}"); println!("Paste deleted successfully with edit code: {edit_code}");
return; return;
@ -35,7 +72,7 @@ fn main() {
println!("Hit the limit, waiting..."); println!("Hit the limit, waiting...");
csrf_data = CSRFData::get(&client, PASTE_NAME); csrf_data = CSRFData::get(&client, PASTE_NAME);
std::thread::sleep(std::time::Duration::from_secs(30)); std::thread::sleep(std::time::Duration::from_secs(30));
edit_codes.push_front(edit_code); edit_codes.retry_last().unwrap();
} }
DeleteError::FormError(msg) => { DeleteError::FormError(msg) => {
eprintln!("Form error: {msg}"); eprintln!("Form error: {msg}");