commit
This commit is contained in:
parent
300e6115f1
commit
faccbdc6fd
@ -2,6 +2,7 @@
|
||||
name = "rub-login"
|
||||
version = "0.1.0"
|
||||
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
html5ever = "0.23.0"
|
||||
|
47
src/main.rs
47
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<T>(node: &Node, tag: &str, attr_name: &str) -> Option<T>
|
||||
@ -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<bool, Error> {
|
||||
@ -142,14 +140,14 @@ fn logout() -> Result<bool, Error> {
|
||||
},
|
||||
..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")),
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user