summaryrefslogtreecommitdiff
path: root/rust/libnewsboat-ffi/src/cliargsparser.rs
blob: 358db2d10b83d620a500e0c02187368ae510fb0d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use libnewsboat::cliargsparser;

// cxx doesn't allow to share types from other crates, so we have to wrap it
// cf. https://github.com/dtolnay/cxx/issues/496
struct CliArgsParser(cliargsparser::CliArgsParser);

#[cxx::bridge(namespace = "newsboat::cliargsparser::bridged")]
mod bridged {
    extern "Rust" {
        type CliArgsParser;

        fn create(argv: Vec<String>) -> Box<CliArgsParser>;

        fn do_import(cliargsparser: &CliArgsParser) -> bool;
        fn do_export(cliargsparser: &CliArgsParser) -> bool;
        fn do_vacuum(cliargsparser: &CliArgsParser) -> bool;
        fn do_cleanup(cliargsparser: &CliArgsParser) -> bool;
        fn do_show_version(cliargsparser: &CliArgsParser) -> u64;
        fn silent(cliargsparser: &CliArgsParser) -> bool;
        fn using_nonstandard_configs(cliargsparser: &CliArgsParser) -> bool;
        fn should_print_usage(cliargsparser: &CliArgsParser) -> bool;
        fn refresh_on_start(cliargsparser: &CliArgsParser) -> bool;

        fn importfile(cliargsparser: &CliArgsParser) -> String;
        fn program_name(cliargsparser: &CliArgsParser) -> String;
        fn display_msg(cliargsparser: &CliArgsParser) -> String;

        fn return_code(cliargsparser: &CliArgsParser, value: &mut isize) -> bool;

        fn readinfo_import_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn readinfo_export_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn url_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn lock_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn cache_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn config_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;
        fn log_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool;

        fn cmds_to_execute(cliargsparser: &CliArgsParser) -> Vec<String>;

        fn log_level(cliargsparser: &CliArgsParser, level: &mut i8) -> bool;
    }

    extern "C++" {
        // cxx uses `std::out_of_range`, but doesn't include the header that defines that
        // exception. So we do it for them.
        include!("stdexcept");
        // Also inject a header that defines ptrdiff_t. Note this is *not* a C++ header, because
        // cxx uses a non-C++ name of the type.
        include!("stddef.h");
    }
}

fn create(argv: Vec<String>) -> Box<CliArgsParser> {
    Box::new(CliArgsParser(cliargsparser::CliArgsParser::new(argv)))
}

fn do_import(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.importfile.is_some()
}

fn do_export(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.do_export
}

fn do_vacuum(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.do_vacuum
}

fn do_cleanup(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.do_cleanup
}

fn do_show_version(cliargsparser: &CliArgsParser) -> u64 {
    cliargsparser.0.show_version as u64
}

fn silent(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.silent
}

fn using_nonstandard_configs(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.using_nonstandard_configs()
}

fn should_print_usage(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.should_print_usage
}

fn refresh_on_start(cliargsparser: &CliArgsParser) -> bool {
    cliargsparser.0.refresh_on_start
}

fn importfile(cliargsparser: &CliArgsParser) -> String {
    match &cliargsparser.0.importfile {
        Some(path) => path.to_string_lossy().to_string(),
        None => String::new(),
    }
}

fn program_name(cliargsparser: &CliArgsParser) -> String {
    cliargsparser.0.program_name.to_string()
}

fn display_msg(cliargsparser: &CliArgsParser) -> String {
    cliargsparser.0.display_msg.to_string()
}

fn return_code(cliargsparser: &CliArgsParser, value: &mut isize) -> bool {
    match cliargsparser.0.return_code {
        Some(code) => {
            *value = code as isize;
            true
        }
        None => false,
    }
}

fn readinfo_import_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.readinfo_import_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn readinfo_export_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.readinfo_export_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn url_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.url_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn lock_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.lock_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn cache_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.cache_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn config_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.config_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn log_file(cliargsparser: &CliArgsParser, path: &mut String) -> bool {
    match &cliargsparser.0.log_file {
        Some(p) => {
            *path = p.to_string_lossy().to_string();
            true
        }
        None => false,
    }
}

fn cmds_to_execute(cliargsparser: &CliArgsParser) -> Vec<String> {
    cliargsparser.0.cmds_to_execute.to_owned()
}

fn log_level(cliargsparser: &CliArgsParser, level: &mut i8) -> bool {
    match cliargsparser.0.log_level {
        Some(l) => {
            *level = l as i8;
            true
        }
        None => false,
    }
}