1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![feature(try_trait)]
extern crate reqwest;

use std::ffi::CStr;
use std::os::raw::{c_char, c_int};
use std::path::Path;

use crate::account_api::AccountApiImpl;
use crate::account_repo::AccountRepoImpl;
use crate::account_service::{AccountService, AccountServiceImpl};
use crate::crypto::RsaCryptoService;
use crate::db_provider::DiskBackedDB;
use crate::schema::SchemaCreatorImpl;
use crate::state::Config;

pub mod account;
pub mod account_api;
pub mod account_repo;
pub mod account_service;
pub mod crypto;
pub mod db_provider;
pub mod error_enum;
pub mod lockbook_api;
pub mod schema;
pub mod state;

static API_LOC: &str = "http://lockbook.app:8000";
static DB_NAME: &str = "lockbook.db3";

type DefaultCrypto = RsaCryptoService;
type DefaultSchema = SchemaCreatorImpl;
type DefaultDbProvider = DiskBackedDB<DefaultSchema>;
type DefaultAcountRepo = AccountRepoImpl;
type DefaultAccountApi = AccountApiImpl;
type DefaultAcountService =
    AccountServiceImpl<DefaultDbProvider, DefaultCrypto, DefaultAcountRepo, DefaultAccountApi>;

#[no_mangle]
pub unsafe extern "C" fn is_db_present(path_c: *const c_char) -> c_int {
    let path = CStr::from_ptr(path_c)
        .to_str()
        .expect("Could not C String -> Rust String")
        .to_string();

    let db_path = path + "/" + DB_NAME;

    if Path::new(db_path.as_str()).exists() {
        1
    } else {
        0
    }
}

#[no_mangle]
pub unsafe extern "C" fn create_account(c_username: *const c_char) -> c_int {
    let username = CStr::from_ptr(c_username)
        .to_str()
        .expect("Could not C String -> Rust String");

    let config = Config {
        writeable_path: "".to_string(),
    };

    match DefaultAcountService::create_account(config, username.to_string()) {
        Ok(_) => 0,
        Err(err) => {
            println!("Account creation failed with error: {:?}", err);
            1
        }
    }
}