• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ortham / libloadorder / 9669712859

25 Jun 2024 09:15PM UTC coverage: 91.599% (+4.7%) from 86.942%
9669712859

push

github

Ortham
Use Starfield plugins in Starfield tests

65 of 65 new or added lines in 5 files covered. (100.0%)

163 existing lines in 11 files now uncovered.

7218 of 7880 relevant lines covered (91.6%)

66616.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.82
/ffi/src/helpers.rs
1
/*
2
 * This file is part of libloadorder
3
 *
4
 * Copyright (C) 2017 Oliver Hamlet
5
 *
6
 * libloadorder is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * libloadorder is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with libloadorder. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
use std::ffi::{CStr, CString};
20
use std::io;
21
use std::mem;
22
use std::path::PathBuf;
23
use std::slice;
24

25
use libc::{c_char, c_uint, size_t};
26
use loadorder::Error;
27

28
use super::ERROR_MESSAGE;
29
use crate::constants::*;
30

31
pub fn error(code: c_uint, message: &str) -> c_uint {
2✔
32
    ERROR_MESSAGE.with(|f| {
2✔
33
        *f.borrow_mut() = unsafe { CString::from_vec_unchecked(message.as_bytes().to_vec()) }
2✔
34
    });
2✔
35
    code
2✔
36
}
2✔
37

38
pub fn handle_error(err: Error) -> c_uint {
1✔
39
    let code = map_error(&err);
1✔
40
    error(code, &format!("{}", err))
1✔
41
}
1✔
42

43
fn map_io_error(err: &io::Error) -> c_uint {
×
44
    use io::ErrorKind::*;
×
45
    match err.kind() {
×
46
        NotFound => LIBLO_ERROR_FILE_NOT_FOUND,
×
47
        AlreadyExists => LIBLO_ERROR_FILE_RENAME_FAIL,
×
48
        PermissionDenied => LIBLO_ERROR_IO_PERMISSION_DENIED,
×
49
        _ => LIBLO_ERROR_IO_ERROR,
×
50
    }
51
}
×
52

53
fn map_error(err: &Error) -> c_uint {
1✔
54
    use Error::*;
1✔
55
    match *err {
1✔
56
        InvalidPath(_) => LIBLO_ERROR_FILE_NOT_FOUND,
×
57
        IoError(_, ref x) => map_io_error(x),
×
58
        NoFilename(_) => LIBLO_ERROR_FILE_PARSE_FAIL,
×
59
        SystemTimeError(_) => LIBLO_ERROR_TIMESTAMP_WRITE_FAIL,
×
60
        NotUtf8(_) => LIBLO_ERROR_FILE_NOT_UTF8,
×
61
        DecodeError(_) => LIBLO_ERROR_TEXT_DECODE_FAIL,
×
62
        EncodeError(_) => LIBLO_ERROR_TEXT_ENCODE_FAIL,
×
63
        PluginParsingError(_, _) => LIBLO_ERROR_FILE_PARSE_FAIL,
×
64
        PluginNotFound(_) => LIBLO_ERROR_INVALID_ARGS,
×
65
        TooManyActivePlugins { .. } => LIBLO_ERROR_INVALID_ARGS,
×
66
        DuplicatePlugin(_) => LIBLO_ERROR_INVALID_ARGS,
×
67
        NonMasterBeforeMaster { .. } => LIBLO_ERROR_INVALID_ARGS,
×
68
        GameMasterMustLoadFirst(_) => LIBLO_ERROR_INVALID_ARGS,
×
69
        InvalidEarlyLoadingPluginPosition { .. } => LIBLO_ERROR_INVALID_ARGS,
×
70
        ImplicitlyActivePlugin(_) => LIBLO_ERROR_INVALID_ARGS,
×
71
        NoLocalAppData => LIBLO_ERROR_INVALID_ARGS,
1✔
72
        NoDocumentsPath => LIBLO_ERROR_INVALID_ARGS,
×
73
        UnrepresentedHoist { .. } => LIBLO_ERROR_INVALID_ARGS,
×
74
        InstalledPlugin(_) => LIBLO_ERROR_INVALID_ARGS,
×
UNCOV
75
        IniParsingError { .. } => LIBLO_ERROR_FILE_PARSE_FAIL,
×
76
        VdfParsingError(_, _) => LIBLO_ERROR_FILE_PARSE_FAIL,
×
UNCOV
77
        SystemError(_, _) => LIBLO_ERROR_SYSTEM_ERROR,
×
78
    }
79
}
1✔
80

81
pub unsafe fn to_str<'a>(c_string: *const c_char) -> Result<&'a str, u32> {
10✔
82
    if c_string.is_null() {
10✔
83
        Err(error(LIBLO_ERROR_INVALID_ARGS, "Null pointer passed"))
×
84
    } else {
85
        CStr::from_ptr(c_string)
10✔
86
            .to_str()
10✔
87
            .map_err(|_| error(LIBLO_ERROR_INVALID_ARGS, "Non-UTF-8 string passed"))
10✔
88
    }
89
}
10✔
90

91
pub fn to_c_string<S: AsRef<str>>(string: S) -> Result<*mut c_char, u32> {
1✔
92
    CString::new(string.as_ref())
1✔
93
        .map(CString::into_raw)
1✔
94
        .map_err(|_| LIBLO_ERROR_TEXT_ENCODE_FAIL)
1✔
95
}
1✔
96

97
pub fn to_c_string_array<S: AsRef<str>>(strings: &[S]) -> Result<(*mut *mut c_char, size_t), u32> {
1✔
98
    let mut c_strings = strings
1✔
99
        .iter()
1✔
100
        .map(to_c_string)
1✔
101
        .collect::<Result<Vec<*mut c_char>, u32>>()?;
1✔
102

103
    c_strings.shrink_to_fit();
1✔
104

1✔
105
    let pointer = c_strings.as_mut_ptr();
1✔
106
    let size = c_strings.len();
1✔
107
    mem::forget(c_strings);
1✔
108

1✔
109
    Ok((pointer, size))
1✔
110
}
1✔
111

112
pub unsafe fn to_str_vec<'a>(
×
113
    array: *const *const c_char,
×
114
    array_size: usize,
×
115
) -> Result<Vec<&'a str>, u32> {
×
116
    slice::from_raw_parts(array, array_size)
×
117
        .iter()
×
UNCOV
118
        .map(|c| to_str(*c))
×
119
        .collect()
×
120
}
×
121

122
pub unsafe fn to_path_buf_vec(
×
123
    array: *const *const c_char,
×
124
    array_size: usize,
×
UNCOV
125
) -> Result<Vec<PathBuf>, u32> {
×
126
    if array_size == 0 {
×
127
        Ok(Vec::new())
×
128
    } else {
129
        slice::from_raw_parts(array, array_size)
×
UNCOV
130
            .iter()
×
131
            .map(|c| to_str(*c).map(PathBuf::from))
×
UNCOV
132
            .collect()
×
133
    }
UNCOV
134
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc