1 /*
2  * Copyright 2025 Matheus C. França
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License") @trusted;
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 module ada.c.ada;
18 
19 @nogc nothrow @safe extern (C)
20 {
21     /// string that is owned by the ada_url instance
22     struct ada_string
23     {
24         const(char)* data = void;
25         size_t length = void;
26     }
27 
28     /// string that must be freed by the caller
29     struct ada_owned_string
30     {
31         const(char)* data = void;
32         size_t length = void;
33     }
34 
35     struct ada_url_components
36     {
37         uint protocol_end = void;
38         uint username_end = void;
39         uint host_start = void;
40         uint host_end = void;
41         uint port = void;
42         uint pathname_start = void;
43         uint search_start = void;
44         uint hash_start = void;
45     }
46 
47     /// This is a reference to ada::url_components::omitted
48     /// It represents "uint32_t(-1)"
49     enum uint ada_url_omitted = 0xffffffff;
50 
51     alias ada_url = void*;
52 
53     /// input should be a null terminated C string (ASCII or UTF-8)
54     /// you must call ada_free on the returned pointer
55     ada_url ada_parse(scope const(char)* input, size_t length) @trusted;
56     ada_url ada_parse_with_base(scope const(char)* input, size_t input_length, scope const(char)* base, size_t base_length) @trusted;
57 
58     // input and base should be a null terminated C strings
59     bool ada_can_parse(scope const(char)* input, size_t length) @trusted;
60     bool ada_can_parse_with_base(scope const(char)* input, size_t input_length, scope const(char)* base, size_t base_length) @trusted;
61     void ada_free(scope ada_url result) @trusted;
62     void ada_free_owned_string(ada_owned_string owned) @trusted;
63     ada_url ada_copy(scope ada_url input) @trusted;
64     bool ada_is_valid(scope ada_url result) @trusted;
65 
66     /// url_aggregator getters
67     /// if ada_is_valid(result)) is false, an empty string is returned
68     ada_owned_string ada_get_origin(scope ada_url result) @trusted;
69     ada_string ada_get_href(scope ada_url result) @trusted;
70     ada_string ada_get_username(scope ada_url result) @trusted;
71     ada_string ada_get_password(scope ada_url result) @trusted;
72     ada_string ada_get_port(scope ada_url result) @trusted;
73     ada_string ada_get_hash(scope ada_url result) @trusted;
74     ada_string ada_get_host(scope ada_url result) @trusted;
75     ada_string ada_get_hostname(scope ada_url result) @trusted;
76     ada_string ada_get_pathname(scope ada_url result) @trusted;
77     ada_string ada_get_search(scope ada_url result) @trusted;
78     ada_string ada_get_protocol(scope ada_url result) @trusted;
79     ubyte ada_get_host_type(scope ada_url result) @trusted;
80     ubyte ada_get_scheme_type(scope ada_url result) @trusted;
81 
82     /// url_aggregator setters
83     /// if ada_is_valid(result)) is false, the setters have no effect
84     /// input should be a null terminated C string
85     bool ada_set_href(scope ada_url result, scope const(char)* input, size_t length) @trusted;
86     bool ada_set_host(scope ada_url result, scope const(char)* input, size_t length) @trusted;
87     bool ada_set_hostname(scope ada_url result, scope const(char)* input, size_t length) @trusted;
88     bool ada_set_protocol(scope ada_url result, scope const(char)* input, size_t length) @trusted;
89     bool ada_set_username(scope ada_url result, scope const(char)* input, size_t length) @trusted;
90     bool ada_set_password(scope ada_url result, scope const(char)* input, size_t length) @trusted;
91     bool ada_set_port(scope ada_url result, scope const(char)* input, size_t length) @trusted;
92     bool ada_set_pathname(scope ada_url result, scope const(char)* input, size_t length) @trusted;
93     void ada_set_search(scope ada_url result, scope const(char)* input, size_t length) @trusted;
94     void ada_set_hash(scope ada_url result, scope const(char)* input, size_t length) @trusted;
95 
96     /// url_aggregator clear methods
97     void ada_clear_port(scope ada_url result) @trusted;
98     void ada_clear_hash(scope ada_url result) @trusted;
99     void ada_clear_search(scope ada_url result) @trusted;
100 
101     /// url_aggregator functions
102     /// if ada_is_valid(result) is false, functions below will return false
103     bool ada_has_credentials(scope ada_url result) @trusted;
104     bool ada_has_empty_hostname(scope ada_url result) @trusted;
105     bool ada_has_hostname(scope ada_url result) @trusted;
106     bool ada_has_non_empty_username(scope ada_url result) @trusted;
107     bool ada_has_non_empty_password(scope ada_url result) @trusted;
108     bool ada_has_port(scope ada_url result) @trusted;
109     bool ada_has_password(scope ada_url result) @trusted;
110     bool ada_has_hash(scope ada_url result) @trusted;
111     bool ada_has_search(scope ada_url result) @trusted;
112 
113     /// returns a pointer to the internal url_aggregator::url_components
114     const(ada_url_components)* ada_get_components(scope ada_url result) @trusted;
115 
116     /// idna methods
117     ada_owned_string ada_idna_to_unicode(scope const(char)* input, size_t length) @trusted;
118     ada_owned_string ada_idna_to_ascii(scope const(char)* input, size_t length) @trusted;
119 
120     /// url search params
121     alias ada_url_search_params = void*;
122 
123     /// Represents an std::vector<std::string>
124     alias ada_strings = void*;
125     alias ada_url_search_params_keys_iter = void*;
126     alias ada_url_search_params_values_iter = void*;
127 
128     struct ada_string_pair
129     {
130         ada_string key = void;
131         ada_string value = void;
132     }
133 
134     alias ada_url_search_params_entries_iter = void*;
135     ada_url_search_params ada_parse_search_params(scope const(char)* input, size_t length) @trusted;
136     void ada_free_search_params(scope ada_url_search_params result) @trusted;
137 
138     size_t ada_search_params_size(scope ada_url_search_params result) @trusted;
139     void ada_search_params_sort(scope ada_url_search_params result) @trusted;
140     ada_owned_string ada_search_params_to_string(scope ada_url_search_params result) @trusted;
141 
142     void ada_search_params_append(scope ada_url_search_params result, scope const(char)* key, size_t key_length, scope const(
143             char)* value, size_t value_length) @trusted;
144     void ada_search_params_set(scope ada_url_search_params result, scope const(char)* key, size_t key_length, scope const(
145             char)* value, size_t value_length) @trusted;
146     void ada_search_params_remove(scope ada_url_search_params result, scope const(char)* key, size_t key_length) @trusted;
147     void ada_search_params_remove_value(scope ada_url_search_params result, scope const(char)* key, size_t key_length, const(
148             char)* value, size_t value_length) @trusted;
149     bool ada_search_params_has(scope ada_url_search_params result, scope const(char)* key, size_t key_length) @trusted;
150     bool ada_search_params_has_value(scope ada_url_search_params result, scope const(char)* key, size_t key_length, scope const(
151             char)* value, size_t value_length) @trusted;
152     ada_string ada_search_params_get(scope ada_url_search_params result, scope const(char)* key, size_t key_length) @trusted;
153     ada_strings ada_search_params_get_all(scope ada_url_search_params result, scope const(
154             char)* key, size_t key_length) @trusted;
155     void ada_search_params_reset(scope ada_url_search_params result, scope const(char)* input, size_t length) @trusted;
156     ada_url_search_params_keys_iter ada_search_params_get_keys(scope ada_url_search_params result) @trusted;
157     ada_url_search_params_values_iter ada_search_params_get_values(scope ada_url_search_params result) @trusted;
158     ada_url_search_params_entries_iter ada_search_params_get_entries(
159         scope ada_url_search_params result) @trusted;
160 
161     void ada_free_strings(scope ada_strings result) @trusted;
162     size_t ada_strings_size(scope ada_strings result) @trusted;
163     ada_string ada_strings_get(scope ada_strings result, size_t index) @trusted;
164 
165     void ada_free_search_params_keys_iter(scope ada_url_search_params_keys_iter result) @trusted;
166     ada_string ada_search_params_keys_iter_next(scope ada_url_search_params_keys_iter result) @trusted;
167     bool ada_search_params_keys_iter_has_next(scope ada_url_search_params_keys_iter result) @trusted;
168 
169     void ada_free_search_params_values_iter(scope ada_url_search_params_values_iter result) @trusted;
170     ada_string ada_search_params_values_iter_next(scope ada_url_search_params_values_iter result) @trusted;
171     bool ada_search_params_values_iter_has_next(scope ada_url_search_params_values_iter result) @trusted;
172 
173     void ada_free_search_params_entries_iter(scope ada_url_search_params_entries_iter result) @trusted;
174     ada_string_pair ada_search_params_entries_iter_next(
175         scope ada_url_search_params_entries_iter result) @trusted;
176     bool ada_search_params_entries_iter_has_next(scope ada_url_search_params_entries_iter result) @trusted;
177 }