Backed out 7 changesets (bug 1942424) for causing frequent crashes. a=backout
[gecko.git] / toolkit / components / bitsdownload / src / bits_interface / string.rs
blobc3111b7cbafa1db8bc88eaa860a06d58961c42f7
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 use super::{
5     action::Action,
6     error::{BitsTaskError, ErrorStage, ErrorType},
7 };
9 use bits_client::Guid;
10 use nsstring::nsCString;
11 use std::ffi::OsString;
12 use std::{str, str::FromStr};
14 /// This function is fallible, and the consumers would prefer a BitsTaskError
15 /// in the failure case. To facilitate that, this function takes some params
16 /// that give it the data necessary to construct the BitsTaskError if it fails.
17 /// If it succeeds, those values will be unused.
18 #[allow(non_snake_case)]
19 pub fn nsCString_to_String(
20     value: &nsCString,
21     error_action: Action,
22     error_stage: ErrorStage,
23 ) -> Result<String, BitsTaskError> {
24     match String::from_utf8(value[..].to_vec()) {
25         Ok(s) => Ok(s),
26         Err(_) => Err(BitsTaskError::new(
27             ErrorType::NoUtf8Conversion,
28             error_action,
29             error_stage,
30         )),
31     }
34 /// This function is fallible, and the consumers would prefer a BitsTaskError
35 /// in the failure case. To facilitate that, this function takes some params
36 /// that give it the data necessary to construct the BitsTaskError if it fails.
37 /// If it succeeds, those values will be unused.
38 #[allow(non_snake_case)]
39 pub fn nsCString_to_OsString(
40     value: &nsCString,
41     error_action: Action,
42     error_stage: ErrorStage,
43 ) -> Result<OsString, BitsTaskError> {
44     Ok(OsString::from(nsCString_to_String(
45         value,
46         error_action,
47         error_stage,
48     )?))
51 /// This function is fallible, and the consumers would prefer a BitsTaskError
52 /// in the failure case. To facilitate that, this function takes some params
53 /// that give it the data necessary to construct the BitsTaskError if it fails.
54 /// If it succeeds, those values will be unused.
55 #[allow(non_snake_case)]
56 pub fn Guid_from_nsCString(
57     value: &nsCString,
58     error_action: Action,
59     error_stage: ErrorStage,
60 ) -> Result<Guid, BitsTaskError> {
61     let vector = &value[..].to_vec();
62     let string = match str::from_utf8(vector) {
63         Ok(s) => s,
64         Err(_) => {
65             return Err(BitsTaskError::new(
66                 ErrorType::NoUtf8Conversion,
67                 error_action,
68                 error_stage,
69             ));
70         }
71     };
72     Guid::from_str(string).map_err(|comedy_error| {
73         BitsTaskError::from_comedy(
74             ErrorType::InvalidGuid,
75             error_action,
76             error_stage,
77             comedy_error,
78         )
79     })