Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

减少 unwrap #31

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cli/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ pub fn parse_location_sub_command(db: &DataBase, sub_command: LocationSubCommand
inquire::Confirm::new(msg)
.with_default(false)
.prompt()
.unwrap()
.unwrap_or_else(|e| {
warn!("无法识别输入:{e}.");
false
})
}
match sub_command {
LocationSubCommand::Add {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn do_sign(
active_id: Option<i64>,
accounts_str: Option<String>,
cli_args: CliArgs,
) -> Result<(), Box<cxsign::Error>> {
) -> Result<(), cxsign::Error> {
let account_table = AccountTable::from_ref(&db);
let (sessions, has_accounts_arg) = if let Some(accounts_str) = &accounts_str {
(
Expand All @@ -132,7 +132,7 @@ pub fn do_sign(
};
let (valid_signs, other_signs, _) =
Activity::get_all_activities(ExcludeTable::from_ref(&db), sessions.values(), false)
.unwrap();
.map_err(cxsign::Error::from)?;
let signs = if let Some(active_id) = active_id {
let (sign, sessions) = {
if let Some(s1) = valid_signs
Expand Down
31 changes: 25 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use cxsign::{
utils::DIR,
Activity, Course, SignTrait,
};
use log::{info, warn};
use log::{error, info, warn};
const NOTICE: &str = r#"

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -85,7 +85,10 @@ fn main() {
let ans = inquire::Confirm::new("是否删除?")
.with_default(false)
.prompt()
.unwrap();
.unwrap_or_else(|e| {
warn!("无法识别输入:{e}.");
false
});
if !ans {
return;
}
Expand Down Expand Up @@ -118,7 +121,13 @@ fn main() {
let sessions = account_table.get_sessions();
CourseTable::delete(&db);
for (_, session) in sessions {
let courses = Course::get_courses(&session).unwrap();
let courses = Course::get_courses(&session).unwrap_or_else(|e| {
warn!(
"未能获取到用户[{}]的课程,错误信息:{e}.",
session.get_stu_name()
);
Vec::default()
});
for c in courses {
table.add_course_or(&c, |_, _| {});
}
Expand Down Expand Up @@ -198,7 +207,10 @@ fn main() {
sessions.values(),
all,
)
.unwrap();
.unwrap_or_else(|e| {
warn!("未能获取签到列表,错误信息:{e}.",);
Default::default()
});
// 列出所有有效签到。
for a in available_sign_activities {
info!("{}", a.0.as_inner());
Expand All @@ -214,7 +226,13 @@ fn main() {
}
}
MainCommand::WhereIsConfig => {
info!("{}", &DIR.get_config_dir().to_str().unwrap());
info!(
"{}",
&DIR.get_config_dir()
.into_os_string()
.to_string_lossy()
.to_string()
);
}
}
} else {
Expand All @@ -225,6 +243,7 @@ fn main() {
precisely,
};
warn!("{NOTICE}");
cli::do_sign(db, active_id, accounts, cli_args).unwrap();
cli::do_sign(db, active_id, accounts, cli_args)
.unwrap_or_else(|e| error!("签到失败!错误信息:{e}."));
}
}
43 changes: 37 additions & 6 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,65 @@ use cxsign::{
utils::DIR,
Course, Session,
};
use log::warn;

// 添加账号。
pub fn inquire_pwd_and_add_account(db: &DataBase, uname: String, pwd: Option<String>) {
let pwd = if let Some(pwd) = pwd {
pwd
} else {
inquire::Password::new("密码:")
match inquire::Password::new("密码:")
.without_confirmation()
.prompt()
.unwrap()
{
Ok(pwd) => pwd,
Err(e) => {
warn!("输入的密码无法解析:{e}.");
return;
}
}
};
let enc_pwd = cxsign::utils::des_enc(&pwd);
let session = Session::login(&DIR, &uname, &enc_pwd).unwrap();
let session = match Session::login(&DIR, &uname, &enc_pwd) {
Ok(s) => s,
Err(e) => {
warn!("账号[{uname}]登录失败:{e}.");
return;
}
};
let table = AccountTable::from_ref(db);
let name = session.get_stu_name();
table.add_account_or(&uname, &enc_pwd, name, AccountTable::update_account);
let courses = Course::get_courses(&session).unwrap();
let courses = Course::get_courses(&session).unwrap_or_else(|e| {
warn!(
"用户[{}]获取课程列表失败,错误信息:{e}.",
session.get_stu_name()
);
Default::default()
});
let table = CourseTable::from_ref(db);
for c in courses {
table.add_course_or(&c, |_, _| {});
}
}
pub fn add_account_by_enc_pwd_when_fresh(db: &DataBase, uname: String, enc_pwd: &str) {
let session = Session::login(&DIR, &uname, enc_pwd).unwrap();
let session = match Session::login(&DIR, &uname, enc_pwd) {
Ok(s) => s,
Err(e) => {
warn!("账号[{uname}]登录失败:{e}.");
return;
}
};
let name = session.get_stu_name();
let table = AccountTable::from_ref(db);
table.add_account_or(&uname, enc_pwd, name, AccountTable::update_account);
let courses = Course::get_courses(&session).unwrap();
let courses = Course::get_courses(&session).unwrap_or_else(|e| {
warn!(
"用户[{}]获取课程列表失败,错误信息:{e}.",
session.get_stu_name()
);
Default::default()
});
for c in courses {
let table = CourseTable::from_ref(db);
table.add_course_or(&c, |_, _| {});
Expand Down
Loading