mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-17 03:31:25 +00:00
Add ResultExt
This commit is contained in:
parent
c0d1bf63bc
commit
c0c9204848
@ -1,4 +1,4 @@
|
|||||||
use std::fmt::Arguments;
|
use std::fmt::{Arguments, Display};
|
||||||
use std::io::{stderr, stdout, Write};
|
use std::io::{stderr, stdout, Write};
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
@ -155,3 +155,34 @@ macro_rules! debug {
|
|||||||
macro_rules! debug {
|
macro_rules! debug {
|
||||||
($($args:tt)+) => {};
|
($($args:tt)+) => {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ResultExt {
|
||||||
|
fn ok_or_log(&self);
|
||||||
|
fn ok_or_msg(&self, args: Arguments);
|
||||||
|
fn log_on_error(&self) -> &Self;
|
||||||
|
fn msg_on_error(&self, args: Arguments) -> &Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E: Display> ResultExt for Result<R, E> {
|
||||||
|
fn ok_or_log(&self) {
|
||||||
|
if let Err(e) = self {
|
||||||
|
error!("{}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ok_or_msg(&self, args: Arguments) {
|
||||||
|
if let Err(e) = self {
|
||||||
|
error!("{}: {}", args, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log_on_error(&self) -> &Self {
|
||||||
|
self.ok_or_log();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn msg_on_error(&self, args: Arguments) -> &Self {
|
||||||
|
self.ok_or_msg(args);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -7,8 +7,8 @@ use byteorder::{BigEndian, ReadBytesExt};
|
|||||||
use protobuf::{EnumFull, Message};
|
use protobuf::{EnumFull, Message};
|
||||||
|
|
||||||
use base::libc::c_char;
|
use base::libc::c_char;
|
||||||
use base::WriteExt;
|
use base::ptr_to_str;
|
||||||
use base::{error, ptr_to_str};
|
use base::{ResultExt, WriteExt};
|
||||||
|
|
||||||
use crate::ffi;
|
use crate::ffi;
|
||||||
use crate::update_metadata::install_operation::Type;
|
use crate::update_metadata::install_operation::Type;
|
||||||
@ -148,11 +148,7 @@ fn do_extract_boot_from_payload(in_path: &str, out_path: &str) -> io::Result<()>
|
|||||||
pub fn extract_boot_from_payload(in_path: *const c_char, out_path: *const c_char) -> bool {
|
pub fn extract_boot_from_payload(in_path: *const c_char, out_path: *const c_char) -> bool {
|
||||||
let in_path = ptr_to_str(in_path);
|
let in_path = ptr_to_str(in_path);
|
||||||
let out_path = ptr_to_str(out_path);
|
let out_path = ptr_to_str(out_path);
|
||||||
match do_extract_boot_from_payload(in_path, out_path) {
|
do_extract_boot_from_payload(in_path, out_path)
|
||||||
Ok(_) => true,
|
.msg_on_error(format_args!("Failed to extract boot from payload"))
|
||||||
Err(err) => {
|
.is_ok()
|
||||||
error!("Failed to extract boot from payload: {}", err);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user