From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../bitsdownload/src/bits_interface/string.rs | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 toolkit/components/bitsdownload/src/bits_interface/string.rs (limited to 'toolkit/components/bitsdownload/src/bits_interface/string.rs') diff --git a/toolkit/components/bitsdownload/src/bits_interface/string.rs b/toolkit/components/bitsdownload/src/bits_interface/string.rs new file mode 100644 index 0000000000..c3111b7cba --- /dev/null +++ b/toolkit/components/bitsdownload/src/bits_interface/string.rs @@ -0,0 +1,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 { + 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 { + 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 { + 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, + ) + }) +} -- cgit v1.2.3