Skip to main content

alpm_rs/
question.rs

1use std::os::raw::{c_char, c_void};
2
3use crate::db::alpm_db_t;
4use crate::dependency::{alpm_conflict_t, alpm_depend_t, Conflict, Depend};
5use crate::enums;
6use crate::list::alpm_list_t;
7use crate::package::{alpm_pkg_t, Package, PackageList};
8
9#[allow(non_camel_case_types)]
10type alpm_time_t = i64;
11
12#[repr(C)]
13#[derive(Copy, Clone)]
14struct alpm_pgpkey_t {
15    data: *mut c_void,
16    fingerprint: *const c_char,
17    uid: *const c_char,
18    name: *const c_char,
19    email: *const c_char,
20    created: alpm_time_t,
21    expires: alpm_time_t,
22    length: u32,
23    revoked: u32,
24    pubkey_algo: u8,
25}
26
27#[repr(C)]
28#[derive(Copy, Clone)]
29struct alpm_question_any_t {
30    question_type: i32,
31    answer: i32,
32}
33
34#[repr(C)]
35#[derive(Copy, Clone)]
36struct alpm_question_install_ignorepkg_t {
37    question_type: i32,
38    answer: i32,
39    pkg: *mut alpm_pkg_t,
40}
41
42#[repr(C)]
43#[derive(Copy, Clone)]
44struct alpm_question_replace_t {
45    question_type: i32,
46    answer: i32,
47    oldpkg: *mut alpm_pkg_t,
48    newpkg: *mut alpm_pkg_t,
49    newdb: *mut alpm_db_t,
50}
51
52#[repr(C)]
53#[derive(Copy, Clone)]
54struct alpm_question_conflict_t {
55    question_type: i32,
56    answer: i32,
57    conflict: *mut alpm_conflict_t,
58}
59
60#[repr(C)]
61#[derive(Copy, Clone)]
62struct alpm_question_corrupted_t {
63    question_type: i32,
64    answer: i32,
65    filepath: *const c_char,
66    reason: enums::ErrorNo,
67}
68
69#[repr(C)]
70#[derive(Copy, Clone)]
71struct alpm_question_remove_pkgs_t {
72    question_type: i32,
73    answer: i32,
74    packages: *mut alpm_list_t,
75}
76
77#[repr(C)]
78#[derive(Copy, Clone)]
79struct alpm_question_select_provider_t {
80    question_type: i32,
81    answer: i32,
82    providers: *mut alpm_list_t, //List of alpm_pkg_t
83    dependant: *mut alpm_depend_t,
84}
85
86#[repr(C)]
87#[derive(Copy, Clone)]
88struct alpm_question_import_key_t {
89    question_type: i32,
90    answer: i32,
91    key: *mut alpm_pgpkey_t,
92}
93
94#[repr(C)]
95pub union alpm_question_t {
96    question_type: i32,
97    any: alpm_question_any_t,
98    install_ignorepkg: alpm_question_install_ignorepkg_t,
99    replace: alpm_question_replace_t,
100    conflict: alpm_question_conflict_t,
101    corrupted: alpm_question_corrupted_t,
102    remove_pkgs: alpm_question_remove_pkgs_t,
103    select_provider: alpm_question_select_provider_t,
104    import_key: alpm_question_import_key_t,
105}
106
107pub struct PgpKey {
108    pub data: *mut c_void,
109    pub fingerprint: String,
110    pub uid: String,
111    pub name: String,
112    pub email: String,
113    pub created: i64,
114    pub expires: i64,
115    pub length: u32,
116    pub revoked: u32,
117    pub pubkey_algo: u8,
118}
119
120impl From<*mut alpm_pgpkey_t> for PgpKey {
121    fn from(pgpk: *mut alpm_pgpkey_t) -> Self {
122        let k = unsafe { *pgpk };
123        PgpKey {
124            data: k.data,
125            fingerprint: cstring!(k.fingerprint),
126            uid: cstring!(k.uid),
127            name: cstring!(k.name),
128            email: cstring!(k.email),
129            created: k.created,
130            expires: k.expires,
131            length: k.length,
132            revoked: k.revoked,
133            pubkey_algo: k.pubkey_algo,
134        }
135    }
136}
137
138pub struct QuestionAny {
139    pub question_type: i32,
140    pub answer: i32,
141}
142
143impl From<*mut alpm_question_t> for QuestionAny {
144    fn from(q_raw: *mut alpm_question_t) -> Self {
145        let q = unsafe { *(q_raw as *mut alpm_question_any_t) };
146        QuestionAny {
147            question_type: q.question_type,
148            answer: q.answer,
149        }
150    }
151}
152
153pub struct QuestionInstallIgnorePkg {
154    pub question_type: i32,
155    pub answer: i32,
156    pub pkg: Package,
157}
158
159impl From<*mut alpm_question_t> for QuestionInstallIgnorePkg {
160    fn from(q_raw: *mut alpm_question_t) -> Self {
161        let q = unsafe { *(q_raw as *mut alpm_question_install_ignorepkg_t) };
162        QuestionInstallIgnorePkg {
163            question_type: q.question_type,
164            answer: q.answer,
165            pkg: q.pkg.into(),
166        }
167    }
168}
169
170pub struct QuestionReplacePkg {
171    pub question_type: i32,
172    pub answer: i32,
173    pub conflict: Conflict,
174}
175
176impl From<*mut alpm_question_t> for QuestionReplacePkg {
177    fn from(q_raw: *mut alpm_question_t) -> Self {
178        let q = unsafe { *(q_raw as *mut alpm_question_conflict_t) };
179        QuestionReplacePkg {
180            question_type: q.question_type,
181            answer: q.answer,
182            conflict: q.conflict.into(),
183        }
184    }
185}
186
187pub struct QuestionConflict {
188    pub question_type: i32,
189    pub answer: i32,
190    pub conflict: Conflict,
191}
192
193impl From<*mut alpm_question_t> for QuestionConflict {
194    fn from(q_raw: *mut alpm_question_t) -> Self {
195        let q = unsafe { *(q_raw as *mut alpm_question_conflict_t) };
196        QuestionConflict {
197            question_type: q.question_type,
198            answer: q.answer,
199            conflict: q.conflict.into(),
200        }
201    }
202}
203
204pub struct QuestionCorrupted {
205    pub question_type: i32,
206    pub answer: i32,
207    pub filepath: String,
208    pub reason: enums::ErrorNo,
209}
210
211impl From<*mut alpm_question_t> for QuestionCorrupted {
212    fn from(q_raw: *mut alpm_question_t) -> Self {
213        let q = unsafe { *(q_raw as *mut alpm_question_corrupted_t) };
214        QuestionCorrupted {
215            question_type: q.question_type,
216            answer: q.answer,
217            filepath: cstring!(q.filepath),
218            reason: q.reason,
219        }
220    }
221}
222
223pub struct QuestionRemovePackages {
224    pub question_type: i32,
225    pub answer: i32,
226    pub packages: PackageList,
227}
228
229impl From<*mut alpm_question_t> for QuestionRemovePackages {
230    fn from(q_raw: *mut alpm_question_t) -> Self {
231        let q = unsafe { *(q_raw as *mut alpm_question_remove_pkgs_t) };
232        QuestionRemovePackages {
233            question_type: q.question_type,
234            answer: q.answer,
235            packages: q.packages.into(),
236        }
237    }
238}
239
240pub struct QuestionSelectProvider {
241    pub question_type: i32,
242    pub answer: i32,
243    pub providers: PackageList,
244    pub dependant: Depend,
245}
246
247impl From<*mut alpm_question_t> for QuestionSelectProvider {
248    fn from(q_raw: *mut alpm_question_t) -> Self {
249        let q = unsafe { *(q_raw as *mut alpm_question_select_provider_t) };
250        QuestionSelectProvider {
251            question_type: q.question_type,
252            answer: q.answer,
253            providers: q.providers.into(),
254            dependant: q.dependant.into(),
255        }
256    }
257}
258
259pub struct QuestionImportKey {
260    pub question_type: i32,
261    pub answer: i32,
262    pub key: PgpKey,
263}
264
265impl From<*mut alpm_question_t> for QuestionImportKey {
266    fn from(q_raw: *mut alpm_question_t) -> Self {
267        let q = unsafe { *(q_raw as *mut alpm_question_import_key_t) };
268        QuestionImportKey {
269            question_type: q.question_type,
270            answer: q.answer,
271            key: q.key.into(),
272        }
273    }
274}
275
276pub enum Question {
277    Any(QuestionAny),
278    InstallIgnorePkg(QuestionInstallIgnorePkg),
279    Replace(QuestionReplacePkg),
280    Conflict(QuestionConflict),
281    Corrupted(QuestionCorrupted),
282    RemovePkgs(QuestionRemovePackages),
283    SelectProvider(QuestionSelectProvider),
284    ImportKey(QuestionImportKey),
285}
286
287pub struct QuestionArgs {
288    question_raw: *mut alpm_question_any_t,
289    pub question: Question,
290}
291
292impl QuestionArgs {
293    pub fn set_answer(&self, ans: i32) {
294        unsafe {
295            (*self.question_raw).answer = ans;
296        }
297    }
298
299    pub fn answer(&self) -> i32 {
300        unsafe { (*self.question_raw).answer }
301    }
302
303    pub fn to_any(&self) -> QuestionAny {
304        (self.question_raw as *mut alpm_question_t).into()
305    }
306}
307
308impl From<*mut alpm_question_t> for QuestionArgs {
309    fn from(q: *mut alpm_question_t) -> Self {
310        QuestionArgs {
311            question_raw: q as *mut alpm_question_any_t,
312            question: unsafe {
313                match (*q).question_type {
314                    enums::ALPM_QUESTION_INSTALL_IGNOREPKG => Question::InstallIgnorePkg(q.into()),
315                    enums::ALPM_QUESTION_REPLACE_PKG => Question::Replace(q.into()),
316                    enums::ALPM_QUESTION_CONFLICT_PKG => Question::Conflict(q.into()),
317                    enums::ALPM_QUESTION_CORRUPTED_PKG => Question::Corrupted(q.into()),
318                    enums::ALPM_QUESTION_REMOVE_PKGS => Question::RemovePkgs(q.into()),
319                    enums::ALPM_QUESTION_SELECT_PROVIDER => Question::SelectProvider(q.into()),
320                    enums::ALPM_QUESTION_IMPORT_KEY => Question::ImportKey(q.into()),
321                    _ => Question::Any(q.into()),
322                }
323            },
324        }
325    }
326}