summaryrefslogtreecommitdiffstats
path: root/toolkit/components/bitsdownload/src/bits_interface/string.rs
blob: c3111b7cbafa1db8bc88eaa860a06d58961c42f7 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use super::{
    action::Action,
    error::{BitsTaskError, ErrorStage, ErrorType},
};

use bits_client::Guid;
use nsstring::nsCString;
use std::ffi::OsString;
use std::{str, str::FromStr};

/// This function is fallible, and the consumers would prefer a BitsTaskError
/// in the failure case. To facilitate that, this function takes some params
/// that give it the data necessary to construct the BitsTaskError if it fails.
/// If it succeeds, those values will be unused.
#[allow(non_snake_case)]
pub fn nsCString_to_String(
    value: &nsCString,
    error_action: Action,
    error_stage: ErrorStage,
) -> Result<String, BitsTaskError> {
    match String::from_utf8(value[..].to_vec()) {
        Ok(s) => Ok(s),
        Err(_) => Err(BitsTaskError::new(
            ErrorType::NoUtf8Conversion,
            error_action,
            error_stage,
        )),
    }
}

/// This function is fallible, and the consumers would prefer a BitsTaskError
/// in the failure case. To facilitate that, this function takes some params
/// that give it the data necessary to construct the BitsTaskError if it fails.
/// If it succeeds, those values will be unused.
#[allow(non_snake_case)]
pub fn nsCString_to_OsString(
    value: &nsCString,
    error_action: Action,
    error_stage: ErrorStage,
) -> Result<OsString, BitsTaskError> {
    Ok(OsString::from(nsCString_to_String(
        value,
        error_action,
        error_stage,
    )?))
}

/// This function is fallible, and the consumers would prefer a BitsTaskError
/// in the failure case. To facilitate that, this function takes some params
/// that give it the data necessary to construct the BitsTaskError if it fails.
/// If it succeeds, those values will be unused.
#[allow(non_snake_case)]
pub fn Guid_from_nsCString(
    value: &nsCString,
    error_action: Action,
    error_stage: ErrorStage,
) -> Result<Guid, BitsTaskError> {
    let vector = &value[..].to_vec();
    let string = match str::from_utf8(vector) {
        Ok(s) => s,
        Err(_) => {
            return Err(BitsTaskError::new(
                ErrorType::NoUtf8Conversion,
                error_action,
                error_stage,
            ));
        }
    };
    Guid::from_str(string).map_err(|comedy_error| {
        BitsTaskError::from_comedy(
            ErrorType::InvalidGuid,
            error_action,
            error_stage,
            comedy_error,
        )
    })
}