base.rs (4257B)
1 /* -*- Mode: rust; rust-indent-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 use nsstring::nsCString; 7 use std::ffi::c_void; 8 use thin_vec::ThinVec; 9 10 // Note that we use MaybeString throughout the c++/rust ffi boundary functions 11 // since having a *nsCString to emulate Option<nsCString> (which has no representation on both rust and C++ 12 // operate on the string with common methods like slice() and to_utf8(). 13 // side) doesn't seem to survive crossing the ffi boundary, resulting in a crash when we attempt to 14 // We were getting errors like this: 15 // * Hit MOZ_CRASH(unsafe precondition(s) violated: slice::from_raw_parts requires the pointer 16 // * to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`) 17 // * at core/src/panicking.rs:223 18 #[derive(Debug, Clone)] 19 #[repr(C)] 20 pub struct MaybeString { 21 pub string: nsCString, 22 pub valid: bool, 23 } 24 25 impl MaybeString { 26 pub fn new(s: &nsCString) -> Self { 27 Self { 28 string: s.clone(), 29 valid: true, 30 } 31 } 32 pub fn none() -> Self { 33 Self { 34 string: nsCString::new(), 35 valid: false, 36 } 37 } 38 } 39 40 // this used to hide info of internal urlpattern::url from C++ compiler 41 // so cpp compilation doesn't fail since we don't expose url to gecko 42 #[repr(C)] 43 pub struct UrlpPattern(pub *mut c_void); // structs with unnamed fields 44 45 #[derive(Debug, Clone)] 46 #[repr(C)] 47 pub struct UrlpInit { 48 pub protocol: MaybeString, 49 pub username: MaybeString, 50 pub password: MaybeString, 51 pub hostname: MaybeString, 52 pub port: MaybeString, 53 pub pathname: MaybeString, 54 pub search: MaybeString, 55 pub hash: MaybeString, 56 pub base_url: MaybeString, 57 } 58 59 impl UrlpInit { 60 pub fn none() -> Self { 61 Self { 62 protocol: MaybeString::none(), 63 username: MaybeString::none(), 64 password: MaybeString::none(), 65 hostname: MaybeString::none(), 66 port: MaybeString::none(), 67 pathname: MaybeString::none(), 68 search: MaybeString::none(), 69 hash: MaybeString::none(), 70 base_url: MaybeString::none(), 71 } 72 } 73 } 74 75 #[derive(Debug)] 76 #[repr(C)] 77 pub struct UrlpMatchInput { 78 pub protocol: nsCString, 79 pub username: nsCString, 80 pub password: nsCString, 81 pub hostname: nsCString, 82 pub port: nsCString, 83 pub pathname: nsCString, 84 pub search: nsCString, 85 pub hash: nsCString, 86 } 87 88 #[derive(Debug)] 89 #[repr(C)] 90 pub enum UrlpStringOrInitType { 91 String, 92 Init, 93 } 94 95 // Note: rust's enum variant do not survive cbindgen's c++ generation because 96 // cbindgen creates a anonymous unions which creates fields that are inaccessible 97 // from caller code on the C++ side. We instead break the variants down into their 98 // own more easily digestable structs so that we can modify them on both sides 99 // of the ffi boundary 100 #[derive(Debug)] 101 #[repr(C)] 102 pub struct UrlpInput { 103 pub string_or_init_type: UrlpStringOrInitType, 104 pub str: nsCString, 105 pub init: UrlpInit, 106 pub base: MaybeString, 107 } 108 109 #[derive(Debug)] 110 #[repr(C)] 111 pub struct UrlpMatchInputAndInputs { 112 pub input: UrlpMatchInput, 113 pub inputs: UrlpInput, 114 } 115 116 #[derive(Debug)] 117 #[repr(C)] 118 pub struct UrlpOptions { 119 pub ignore_case: bool, 120 } 121 122 #[derive(Debug, Clone, PartialEq)] 123 #[repr(C)] 124 pub enum UrlpInnerMatcherType { 125 Literal, 126 SingleCapture, 127 RegExp, 128 } 129 130 #[derive(Debug, Clone, PartialEq)] 131 #[repr(C)] 132 pub struct UrlpInnerMatcher { 133 pub inner_type: UrlpInnerMatcherType, 134 pub literal: nsCString, // Literal 135 pub allow_empty: bool, // SingleCapture 136 pub filter_exists: bool, 137 pub filter: char, // SingleCapture 138 pub regexp: nsCString, // RegExp 139 } 140 141 #[derive(Debug, Clone, PartialEq)] 142 #[repr(C)] 143 pub struct UrlpMatcher { 144 pub prefix: nsCString, 145 pub suffix: nsCString, 146 pub inner: UrlpInnerMatcher, 147 } 148 149 #[derive(Debug, Clone, PartialEq)] 150 #[repr(C)] 151 pub struct UrlpComponent { 152 pub pattern_string: nsCString, 153 pub regexp_string: nsCString, 154 pub matcher: UrlpMatcher, 155 pub group_name_list: ThinVec<nsCString>, 156 }