This commit is contained in:
Valentin Brandl 2022-02-05 14:21:05 +01:00
parent 300e6115f1
commit faccbdc6fd
2 changed files with 24 additions and 24 deletions

View File

@ -2,6 +2,7 @@
name = "rub-login" name = "rub-login"
version = "0.1.0" version = "0.1.0"
authors = ["Valentin Brandl <vbrandl@riseup.net>"] authors = ["Valentin Brandl <vbrandl@riseup.net>"]
edition = "2018"
[dependencies] [dependencies]
html5ever = "0.23.0" html5ever = "0.23.0"

View File

@ -28,7 +28,7 @@ enum LocalError {
#[fail(display = "Cannot parse value from HTML")] #[fail(display = "Cannot parse value from HTML")]
ParseError, ParseError,
#[fail(display = "Operation {} failed", _0)] #[fail(display = "Operation {} failed", _0)]
OperationFailed(String), OperationFailed(&'static str),
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -54,17 +54,15 @@ enum Request {
fn find_text(node: &Node, text: &str) -> bool { fn find_text(node: &Node, text: &str) -> bool {
if let NodeData::Text { ref contents } = node.data { if let NodeData::Text { ref contents } = node.data {
if contents.borrow().to_string() == text { if contents.borrow().as_ref() == text {
return true; return true;
} }
} }
for chld in node.children.borrow().iter() { node.children
if find_text(chld, text) { .borrow()
return true; .iter()
} .any(|child| find_text(child, text))
}
false
} }
fn find_value<T>(node: &Node, tag: &str, attr_name: &str) -> Option<T> fn find_value<T>(node: &Node, tag: &str, attr_name: &str) -> Option<T>
@ -77,10 +75,11 @@ where
.. ..
} = node.data } = node.data
{ {
if name.local == *tag && attrs if name.local == *tag
.borrow() && attrs
.iter() .borrow()
.any(|attr| attr.name.local == *"name" && attr.value.to_string() == attr_name) .iter()
.any(|attr| attr.name.local == *"name" && attr.value.to_string() == attr_name)
{ {
let t = attrs let t = attrs
.borrow() .borrow()
@ -92,13 +91,12 @@ where
} }
} }
} }
for chld in node.children.borrow().iter() { node.children
let res = find_value(chld, tag, attr_name); .borrow()
if res.is_some() { .iter()
return res; .map(|child| find_value(child, tag, attr_name))
} .flatten()
} .next()
None
} }
fn login(loginid: String, password: String) -> Result<bool, Error> { fn login(loginid: String, password: String) -> Result<bool, Error> {
@ -142,14 +140,14 @@ fn logout() -> Result<bool, Error> {
}, },
..Default::default() ..Default::default()
}; };
let dom = parse_document(RcDom::default(), opts.clone()) let dom = parse_document(RcDom::default(), opts)
.from_utf8() .from_utf8()
.read_from(&mut res)?; .read_from(&mut res)?;
Ok(find_text(&dom.document, LOGOUT_OK_MARKER)) 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 { if !res {
Err(LocalError::OperationFailed(op_name).into()) Err(LocalError::OperationFailed(op_name).into())
} else { } else {
@ -160,8 +158,9 @@ fn convert_operation_result(res: bool, op_name: String) -> Result<(), Error> {
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let opt = Opt::from_args(); let opt = Opt::from_args();
match opt { match opt {
Opt::Login { loginid, password } => login(loginid, password) Opt::Login { loginid, password } => {
.and_then(|res| convert_operation_result(res, "Login".to_owned())), login(loginid, password).and_then(|res| convert_operation_result(res, "Login"))
Opt::Logout => logout().and_then(|res| convert_operation_result(res, "Logout".to_owned())), }
Opt::Logout => logout().and_then(|res| convert_operation_result(res, "Logout")),
} }
} }