1
1
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- use crate :: slice:: AsBytes ;
5
- use crate :: Error ;
6
- use hyper:: http:: uri:: { Authority , Parts } ;
4
+ use ddcommon_ffi:: slice:: AsBytes ;
5
+ use ddcommon_ffi:: { CharSlice , Error } ;
7
6
use std:: borrow:: Cow ;
8
7
use std:: str:: FromStr ;
9
8
10
- use ddcommon_net1 as net;
11
- use net:: parse_uri;
9
+ use ddcommon_net1 as net1;
10
+ use net1:: deps:: * ;
11
+ use net1:: parse_uri;
12
12
13
- /// Wrapper type to generate the correct FFI name.
14
- pub struct Endpoint ( net:: Endpoint ) ;
13
+ use http:: uri:: { Authority , Parts } ;
15
14
16
- impl From < net:: Endpoint > for Endpoint {
17
- fn from ( inner : net:: Endpoint ) -> Self {
18
- Self ( inner)
19
- }
20
- }
21
-
22
- impl From < Endpoint > for net:: Endpoint {
23
- fn from ( inner : Endpoint ) -> Self {
24
- inner. 0
25
- }
26
- }
15
+ // Transparent so they can be used interchangeably over FFI. However, the
16
+ // struct needs to be declared, or else it won't get emitted by bindgen.
17
+ #[ allow( dead_code) ]
18
+ #[ repr( transparent) ]
19
+ pub struct Endpoint ( net1:: Endpoint ) ;
27
20
28
21
#[ no_mangle]
29
22
#[ must_use]
30
- pub extern "C" fn ddog_endpoint_from_url ( url : crate :: CharSlice ) -> Option < Box < Endpoint > > {
23
+ pub extern "C" fn ddog_endpoint_from_url ( url : CharSlice ) -> Option < Box < net1 :: Endpoint > > {
31
24
parse_uri ( url. to_utf8_lossy ( ) . as_ref ( ) )
32
25
. ok ( )
33
- . map ( |url| Box :: new ( net :: Endpoint :: from_url ( url) . into ( ) ) )
26
+ . map ( |url| Box :: new ( net1 :: Endpoint :: from_url ( url) ) )
34
27
}
35
28
36
29
#[ no_mangle]
37
30
#[ must_use]
38
- pub extern "C" fn ddog_endpoint_from_filename ( filename : crate :: CharSlice ) -> Option < Box < Endpoint > > {
31
+ pub extern "C" fn ddog_endpoint_from_filename ( filename : CharSlice ) -> Option < Box < net1 :: Endpoint > > {
39
32
let url = format ! ( "file://{}" , filename. to_utf8_lossy( ) ) ;
40
- Some ( Box :: new ( net :: Endpoint :: from_slice ( & url) . into ( ) ) )
33
+ Some ( Box :: new ( net1 :: Endpoint :: from_slice ( & url) ) )
41
34
}
42
35
43
36
// We'll just specify the base site here. If api key provided, different intakes need to use their
44
37
// own subdomains.
45
38
#[ no_mangle]
46
39
#[ must_use]
47
- pub extern "C" fn ddog_endpoint_from_api_key ( api_key : crate :: CharSlice ) -> Box < Endpoint > {
40
+ pub extern "C" fn ddog_endpoint_from_api_key ( api_key : CharSlice ) -> Box < net1 :: Endpoint > {
48
41
let mut parts = Parts :: default ( ) ;
49
42
parts. authority = Some ( Authority :: from_static ( "datadoghq.com" ) ) ;
50
- Box :: new (
51
- net:: Endpoint {
52
- url : hyper:: Uri :: from_parts ( parts) . unwrap ( ) ,
53
- api_key : Some ( api_key. to_utf8_lossy ( ) . to_string ( ) . into ( ) ) ,
54
- ..Default :: default ( )
55
- }
56
- . into ( ) ,
57
- )
43
+ Box :: new ( net1:: Endpoint {
44
+ url : http:: Uri :: from_parts ( parts) . unwrap ( ) ,
45
+ api_key : Some ( api_key. to_utf8_lossy ( ) . to_string ( ) . into ( ) ) ,
46
+ ..Default :: default ( )
47
+ } )
58
48
}
59
49
60
50
// We'll just specify the base site here. If api key provided, different intakes need to use their
61
51
// own subdomains.
62
52
#[ no_mangle]
63
53
#[ must_use]
64
54
pub extern "C" fn ddog_endpoint_from_api_key_and_site (
65
- api_key : crate :: CharSlice ,
66
- site : crate :: CharSlice ,
67
- endpoint : & mut * mut Endpoint ,
55
+ api_key : CharSlice ,
56
+ site : CharSlice ,
57
+ endpoint : & mut * mut net1 :: Endpoint ,
68
58
) -> Option < Box < Error > > {
69
59
let mut parts = Parts :: default ( ) ;
70
60
parts. authority = Some ( match Authority :: from_str ( & site. to_utf8_lossy ( ) ) {
71
61
Ok ( s) => s,
72
62
Err ( e) => return Some ( Box :: new ( Error :: from ( e. to_string ( ) ) ) ) ,
73
63
} ) ;
74
- * endpoint = Box :: into_raw ( Box :: new (
75
- net:: Endpoint {
76
- url : hyper:: Uri :: from_parts ( parts) . unwrap ( ) ,
77
- api_key : Some ( api_key. to_utf8_lossy ( ) . to_string ( ) . into ( ) ) ,
78
- ..Default :: default ( )
79
- }
80
- . into ( ) ,
81
- ) ) ;
64
+ * endpoint = Box :: into_raw ( Box :: new ( net1:: Endpoint {
65
+ url : http:: Uri :: from_parts ( parts) . unwrap ( ) ,
66
+ api_key : Some ( api_key. to_utf8_lossy ( ) . to_string ( ) . into ( ) ) ,
67
+ ..Default :: default ( )
68
+ } ) ) ;
82
69
None
83
70
}
84
71
85
72
#[ no_mangle]
86
- extern "C" fn ddog_endpoint_set_timeout ( endpoint : & mut Endpoint , millis : u64 ) {
87
- endpoint. 0 . timeout_ms = millis;
73
+ extern "C" fn ddog_endpoint_set_timeout ( endpoint : & mut net1 :: Endpoint , millis : u64 ) {
74
+ endpoint. timeout_ms = millis;
88
75
}
89
76
90
77
#[ no_mangle]
91
- extern "C" fn ddog_endpoint_set_test_token ( endpoint : & mut Endpoint , token : crate :: CharSlice ) {
92
- endpoint. 0 . test_token = if token. is_empty ( ) {
78
+ extern "C" fn ddog_endpoint_set_test_token ( endpoint : & mut net1 :: Endpoint , token : CharSlice ) {
79
+ endpoint. test_token = if token. is_empty ( ) {
93
80
None
94
81
} else {
95
82
Some ( Cow :: Owned ( token. to_utf8_lossy ( ) . to_string ( ) ) )
96
83
} ;
97
84
}
98
85
99
86
#[ no_mangle]
100
- pub extern "C" fn ddog_endpoint_drop ( _: Box < Endpoint > ) { }
87
+ pub extern "C" fn ddog_endpoint_drop ( _: Box < net1 :: Endpoint > ) { }
101
88
102
89
#[ cfg( test) ]
103
90
mod tests {
104
91
use super :: * ;
105
- use crate :: CharSlice ;
106
- use net:: Endpoint ;
107
92
108
93
#[ test]
109
94
fn test_ddog_endpoint_from_url ( ) {
@@ -128,19 +113,19 @@ mod tests {
128
113
let url = CharSlice :: from ( "http://127.0.0.1" ) ;
129
114
130
115
let mut endpoint = ddog_endpoint_from_url ( url) ;
131
- let timeout_ms = endpoint. as_ref ( ) . unwrap ( ) . 0 . timeout_ms ;
132
- assert_eq ! ( timeout_ms, Endpoint :: DEFAULT_TIMEOUT ) ;
116
+ let timeout_ms = endpoint. as_ref ( ) . unwrap ( ) . timeout_ms ;
117
+ assert_eq ! ( timeout_ms, net1 :: Endpoint :: DEFAULT_TIMEOUT ) ;
133
118
134
119
ddog_endpoint_set_timeout ( endpoint. as_mut ( ) . unwrap ( ) , 2000 ) ;
135
- let timeout_ms = endpoint. as_ref ( ) . unwrap ( ) . 0 . timeout_ms ;
120
+ let timeout_ms = endpoint. as_ref ( ) . unwrap ( ) . timeout_ms ;
136
121
assert_eq ! ( timeout_ms, 2000 ) ;
137
122
138
123
let mut endpoint_api_key = ddog_endpoint_from_api_key ( CharSlice :: from ( "test-key" ) ) ;
139
- let timeout_ms = endpoint_api_key. 0 . timeout_ms ;
140
- assert_eq ! ( timeout_ms, Endpoint :: DEFAULT_TIMEOUT ) ;
124
+ let timeout_ms = endpoint_api_key. timeout_ms ;
125
+ assert_eq ! ( timeout_ms, net1 :: Endpoint :: DEFAULT_TIMEOUT ) ;
141
126
142
127
ddog_endpoint_set_timeout ( & mut endpoint_api_key, 2000 ) ;
143
- let timeout_ms = endpoint_api_key. 0 . timeout_ms ;
128
+ let timeout_ms = endpoint_api_key. timeout_ms ;
144
129
assert_eq ! ( timeout_ms, 2000 ) ;
145
130
}
146
131
}
0 commit comments