From faccbdc6fd58762995ff07e1e5a5090bc4e3da69 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sat, 5 Feb 2022 14:21:05 +0100 Subject: [PATCH] commit --- Cargo.toml | 1 + src/main.rs | 47 +++++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a39768..f01be34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "rub-login" version = "0.1.0" authors = ["Valentin Brandl "] +edition = "2018" [dependencies] html5ever = "0.23.0" diff --git a/src/main.rs b/src/main.rs index d6a20a8..18f221b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ enum LocalError { #[fail(display = "Cannot parse value from HTML")] ParseError, #[fail(display = "Operation {} failed", _0)] - OperationFailed(String), + OperationFailed(&'static str), } #[derive(Debug, StructOpt)] @@ -54,17 +54,15 @@ enum Request { fn find_text(node: &Node, text: &str) -> bool { if let NodeData::Text { ref contents } = node.data { - if contents.borrow().to_string() == text { + if contents.borrow().as_ref() == text { return true; } } - for chld in node.children.borrow().iter() { - if find_text(chld, text) { - return true; - } - } - false + node.children + .borrow() + .iter() + .any(|child| find_text(child, text)) } fn find_value(node: &Node, tag: &str, attr_name: &str) -> Option @@ -77,10 +75,11 @@ where .. } = node.data { - if name.local == *tag && attrs - .borrow() - .iter() - .any(|attr| attr.name.local == *"name" && attr.value.to_string() == attr_name) + if name.local == *tag + && attrs + .borrow() + .iter() + .any(|attr| attr.name.local == *"name" && attr.value.to_string() == attr_name) { let t = attrs .borrow() @@ -92,13 +91,12 @@ where } } } - for chld in node.children.borrow().iter() { - let res = find_value(chld, tag, attr_name); - if res.is_some() { - return res; - } - } - None + node.children + .borrow() + .iter() + .map(|child| find_value(child, tag, attr_name)) + .flatten() + .next() } fn login(loginid: String, password: String) -> Result { @@ -142,14 +140,14 @@ fn logout() -> Result { }, ..Default::default() }; - let dom = parse_document(RcDom::default(), opts.clone()) + let dom = parse_document(RcDom::default(), opts) .from_utf8() .read_from(&mut res)?; Ok(find_text(&dom.document, LOGOUT_OK_MARKER)) } -fn convert_operation_result(res: bool, op_name: String) -> Result<(), Error> { +fn convert_operation_result(res: bool, op_name: &'static str) -> Result<(), Error> { if !res { Err(LocalError::OperationFailed(op_name).into()) } else { @@ -160,8 +158,9 @@ fn convert_operation_result(res: bool, op_name: String) -> Result<(), Error> { fn main() -> Result<(), Error> { let opt = Opt::from_args(); match opt { - Opt::Login { loginid, password } => login(loginid, password) - .and_then(|res| convert_operation_result(res, "Login".to_owned())), - Opt::Logout => logout().and_then(|res| convert_operation_result(res, "Logout".to_owned())), + Opt::Login { loginid, password } => { + login(loginid, password).and_then(|res| convert_operation_result(res, "Login")) + } + Opt::Logout => logout().and_then(|res| convert_operation_result(res, "Logout")), } }