alpm_ll/
util.rs

1use std::ffi::{CStr, CString};
2use std::fmt;
3
4use alpm_sys_ll::*;
5
6use crate::{LIBRARY, Library};
7
8#[derive(Debug, Eq, PartialEq, Copy, Clone, Ord, PartialOrd, Hash)]
9pub struct ChecksumError;
10
11impl fmt::Display for ChecksumError {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        f.write_str("failed to compute checksum")
14    }
15}
16
17impl std::error::Error for ChecksumError {}
18
19pub fn compute_md5sum<S: Into<Vec<u8>>>(s: S) -> Result<String, ChecksumError> {
20    let s = CString::new(s).unwrap();
21    let ret = unsafe { LIBRARY.force_load().alpm_compute_md5sum(s.as_ptr()) };
22    if ret.is_null() {
23        return Err(ChecksumError);
24    }
25
26    let s = unsafe { CStr::from_ptr(ret).to_str().unwrap() };
27    Ok(s.into())
28}
29
30pub fn compute_sha256sum<S: Into<Vec<u8>>>(s: S) -> Result<String, ChecksumError> {
31    let s = CString::new(s).unwrap();
32    let ret = unsafe { LIBRARY.force_load().alpm_compute_sha256sum(s.as_ptr()) };
33    if ret.is_null() {
34        return Err(ChecksumError);
35    }
36
37    let s = unsafe { CStr::from_ptr(ret).to_str().unwrap() };
38    Ok(s.into())
39}