summaryrefslogtreecommitdiff
path: root/rust/libnewsboat-ffi/src/configpaths.rs
blob: e4f9080aa7a7a60023d328125ef9cd1f07cba533 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::abort_on_panic;
use libc::{c_char, c_void};
use libnewsboat::cliargsparser::CliArgsParser;
use libnewsboat::configpaths::ConfigPaths;
use std::ffi::{CStr, CString};
use std::mem;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::path::Path;
use std::ptr;

#[no_mangle]
pub extern "C" fn create_rs_configpaths() -> *mut c_void {
    abort_on_panic(|| Box::into_raw(Box::new(ConfigPaths::new())) as *mut c_void)
}

#[no_mangle]
pub unsafe extern "C" fn destroy_rs_configpaths(object: *mut c_void) {
    abort_on_panic(|| {
        if object.is_null() {
            return;
        };
        Box::from_raw(object as *mut ConfigPaths);
    })
}

unsafe fn with_configpaths<F, T>(object: *mut c_void, action: F, default: T) -> T
where
    F: RefUnwindSafe + Fn(&mut ConfigPaths) -> T,
    T: UnwindSafe,
{
    abort_on_panic(|| {
        if object.is_null() {
            return default;
        }

        let mut object = Box::from_raw(object as *mut ConfigPaths);
        let result = action(&mut object);

        // Don't destroy the object when the function finishes
        mem::forget(object);

        result
    })
}

unsafe fn with_configpaths_string<F>(object: *mut c_void, action: F) -> *mut c_char
where
    F: RefUnwindSafe + Fn(&mut ConfigPaths) -> String,
{
    with_configpaths(
        object,
        |o| CString::new(action(o)).unwrap().into_raw(),
        ptr::null_mut(),
    )
}

unsafe fn with_configpaths_path<F>(object: *mut c_void, action: F) -> *mut c_char
where
    F: RefUnwindSafe + Fn(&mut ConfigPaths) -> &Path,
{
    with_configpaths_string(object, |o| action(o).to_string_lossy().into_owned())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_initialized(object: *mut c_void) -> bool {
    with_configpaths(object, |o| o.initialized(), false)
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_error_message(object: *mut c_void) -> *mut c_char {
    with_configpaths_string(object, |o| o.error_message().to_owned())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_process_args(
    object: *mut c_void,
    rs_cliargsparser: *mut c_void,
) {
    abort_on_panic(|| {
        if rs_cliargsparser.is_null() {
            return;
        }

        let cliargsparser = Box::from_raw(rs_cliargsparser as *mut CliArgsParser);

        with_configpaths(object, |o| o.process_args(&cliargsparser), ());

        // Don't destroy the object when the function finishes
        mem::forget(cliargsparser);
    })
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_try_migrate_from_newsbeuter(object: *mut c_void) -> bool {
    with_configpaths(object, |o| o.try_migrate_from_newsbeuter(), false)
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_create_dirs(object: *mut c_void) -> bool {
    with_configpaths(object, |o| o.create_dirs(), false)
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_url_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.url_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_cache_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.cache_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_set_cache_file(
    object: *mut c_void,
    cstr_path: *const c_char,
) {
    abort_on_panic(|| {
        let path = {
            assert!(!cstr_path.is_null());
            CStr::from_ptr(cstr_path)
        }
        .to_string_lossy()
        .into_owned();
        with_configpaths(
            object,
            |o| o.set_cache_file(Path::new(&path).to_owned()),
            (),
        )
    })
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_config_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.config_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_lock_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.lock_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_queue_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.queue_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_search_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.search_file())
}

#[no_mangle]
pub unsafe extern "C" fn rs_configpaths_cmdline_file(object: *mut c_void) -> *mut c_char {
    with_configpaths_path(object, |o| o.cmdline_file())
}