Make Init return bool result, clean up a bit

This commit is contained in:
Tyler Hallada 2020-10-17 19:31:30 -04:00
parent 3f6ce19e61
commit ae20df0a69
2 changed files with 104 additions and 100 deletions

View File

@ -145,7 +145,7 @@ FFIResult<MerchRecordVec> get_merchandise_list(const char *api_url,
const char *api_key, const char *api_key,
int32_t merchandise_list_id); int32_t merchandise_list_id);
void init(); bool init();
bool status_check(const char *api_url); bool status_check(const char *api_url);

View File

@ -1,6 +1,5 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![feature(vec_into_raw_parts)] #![feature(vec_into_raw_parts)]
#![feature(unwind_attributes)]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -247,13 +246,19 @@ unsafe impl Sync for MerchRecord {}
#[no_mangle] #[no_mangle]
pub extern "C" fn init() { pub extern "C" fn init() -> bool {
info!("init called"); match dirs::document_dir() {
let mut log_dir = dirs::document_dir().expect("could not get Documents directory"); Some(mut log_dir) => {
log_dir.push(Path::new( log_dir.push(Path::new(
r#"My Games\Skyrim Special Edition\SKSE\BazaarRealmClient.log"#, r#"My Games\Skyrim Special Edition\SKSE\BazaarRealmClient.log"#,
)); ));
simple_logging::log_to_file(log_dir, LevelFilter::Info).unwrap(); match simple_logging::log_to_file(log_dir, LevelFilter::Info) {
Ok(_) => true,
Err(_) => false
}
},
None => false
}
} }
#[no_mangle] #[no_mangle]
@ -317,23 +322,8 @@ pub extern "C" fn create_owner(
info!("api_key: {:?}", api_key); info!("api_key: {:?}", api_key);
info!("name: {:?}", name); info!("name: {:?}", name);
info!("mod_version: {:?}", mod_version); info!("mod_version: {:?}", mod_version);
match create_owner_inner(&api_url, &api_key, &name, mod_version) {
Ok(owner) => {
info!("create_owner successful");
if let Some(id) = owner.id {
id
} else {
-1
}
}
Err(err) => {
error!("create_owner failed. {}", err);
-1
}
}
}
fn create_owner_inner(api_url: &str, api_key: &str, name: &str, mod_version: u32) -> Result<Owner> { fn inner(api_url: &str, api_key: &str, name: &str, mod_version: u32) -> Result<Owner> {
#[cfg(not(test))] #[cfg(not(test))]
let url = Url::parse(api_url)?.join("v1/owners")?; let url = Url::parse(api_url)?.join("v1/owners")?;
#[cfg(test)] #[cfg(test)]
@ -359,6 +349,22 @@ fn create_owner_inner(api_url: &str, api_key: &str, name: &str, mod_version: u32
} else { } else {
Err(anyhow!("api-key not defined")) Err(anyhow!("api-key not defined"))
} }
}
match inner(&api_url, &api_key, &name, mod_version) {
Ok(owner) => {
info!("create_owner successful");
if let Some(id) = owner.id {
id
} else {
-1
}
}
Err(err) => {
error!("create_owner failed. {}", err);
-1
}
}
} }
// Because C++ does not have Result, -1 means that the request was unsuccessful // Because C++ does not have Result, -1 means that the request was unsuccessful
@ -382,23 +388,8 @@ pub extern "C" fn create_shop(
info!("api_key: {:?}", api_key); info!("api_key: {:?}", api_key);
info!("name: {:?}", name); info!("name: {:?}", name);
info!("description: {:?}", description); info!("description: {:?}", description);
match create_shop_inner(&api_url, &api_key, &name, &description) {
Ok(shop) => {
info!("create_shop successful");
if let Some(id) = shop.id {
id
} else {
-1
}
}
Err(err) => {
error!("create_shop failed. {}", err);
-1
}
}
}
fn create_shop_inner(api_url: &str, api_key: &str, name: &str, description: &str) -> Result<Shop> { fn inner(api_url: &str, api_key: &str, name: &str, description: &str) -> Result<Shop> {
#[cfg(not(test))] #[cfg(not(test))]
let url = Url::parse(api_url)?.join("v1/shops")?; let url = Url::parse(api_url)?.join("v1/shops")?;
#[cfg(test)] #[cfg(test)]
@ -420,6 +411,22 @@ fn create_shop_inner(api_url: &str, api_key: &str, name: &str, description: &str
file.write_all(&bytes.as_ref())?; file.write_all(&bytes.as_ref())?;
} }
Ok(json) Ok(json)
}
match inner(&api_url, &api_key, &name, &description) {
Ok(shop) => {
info!("create_shop successful");
if let Some(id) = shop.id {
id
} else {
-1
}
}
Err(err) => {
error!("create_shop failed. {}", err);
-1
}
}
} }
// Because C++ does not have Result, -1 means that the request was unsuccessful // Because C++ does not have Result, -1 means that the request was unsuccessful
@ -442,27 +449,13 @@ pub extern "C" fn create_interior_ref_list(
assert!(!ref_records.is_null()); assert!(!ref_records.is_null());
slice::from_raw_parts(ref_records, ref_records_len) slice::from_raw_parts(ref_records, ref_records_len)
}; };
match create_interior_ref_list_inner(&api_url, &api_key, shop_id, ref_records_slice) {
Ok(interior_ref_list) => {
if let Some(id) = interior_ref_list.id {
id
} else {
-1
}
}
Err(err) => {
error!("interior_ref_list failed. {}", err);
-1
}
}
}
fn create_interior_ref_list_inner( fn inner(
api_url: &str, api_url: &str,
api_key: &str, api_key: &str,
shop_id: i32, shop_id: i32,
ref_records: &[RefRecord], ref_records: &[RefRecord],
) -> Result<InteriorRefList> { ) -> Result<InteriorRefList> {
#[cfg(not(test))] #[cfg(not(test))]
let url = Url::parse(api_url)?.join("v1/interior_ref_lists")?; let url = Url::parse(api_url)?.join("v1/interior_ref_lists")?;
#[cfg(test)] #[cfg(test)]
@ -487,6 +480,21 @@ fn create_interior_ref_list_inner(
file.write_all(&bytes.as_ref())?; file.write_all(&bytes.as_ref())?;
} }
Ok(json) Ok(json)
}
match inner(&api_url, &api_key, shop_id, ref_records_slice) {
Ok(interior_ref_list) => {
if let Some(id) = interior_ref_list.id {
id
} else {
-1
}
}
Err(err) => {
error!("interior_ref_list failed. {}", err);
-1
}
}
} }
lazy_static! { lazy_static! {
@ -498,7 +506,6 @@ lazy_static! {
// TODO: fetch by shop_id // TODO: fetch by shop_id
#[no_mangle] #[no_mangle]
#[unwind(allowed)]
pub extern "C" fn get_interior_ref_list( pub extern "C" fn get_interior_ref_list(
api_url: *const c_char, api_url: *const c_char,
api_key: *const c_char, api_key: *const c_char,
@ -512,7 +519,6 @@ pub extern "C" fn get_interior_ref_list(
info!("api_url: {:?}", api_url); info!("api_url: {:?}", api_url);
info!("api_key: {:?}", api_key); info!("api_key: {:?}", api_key);
#[unwind(allowed)]
fn inner( fn inner(
api_url: &str, api_url: &str,
api_key: &str, api_key: &str,
@ -667,7 +673,6 @@ pub extern "C" fn create_merchandise_list(
// TODO: fetch by shop_id // TODO: fetch by shop_id
#[no_mangle] #[no_mangle]
#[unwind(allowed)]
pub extern "C" fn get_merchandise_list( pub extern "C" fn get_merchandise_list(
api_url: *const c_char, api_url: *const c_char,
api_key: *const c_char, api_key: *const c_char,
@ -681,7 +686,6 @@ pub extern "C" fn get_merchandise_list(
info!("api_url: {:?}", api_url); info!("api_url: {:?}", api_url);
info!("api_key: {:?}", api_key); info!("api_key: {:?}", api_key);
#[unwind(allowed)]
fn inner( fn inner(
api_url: &str, api_url: &str,
api_key: &str, api_key: &str,