| 1 |
# Generating the Rust API |
| 2 |
In the previous step we successfully created the unsafe bindings of the -sys crate. |
| 3 |
We are now in the directory of the safe wrapper crate (`gir-tutorial/pango`). |
| 4 |
|
| 5 |
## The Cargo.toml file |
| 6 |
The Cargo.toml file will not be replaced when you run gir. |
| 7 |
So it is our responsibility to make sure the information in it is correct. |
| 8 |
Open the Cargo.toml file and have a look at it. |
| 9 |
Make sure everything under `[package]` is to your liking. |
| 10 |
|
| 11 |
Add the following lines to the file: |
| 12 |
```toml |
| 13 |
[package.metadata.docs.rs] |
| 14 |
all-features = true |
| 15 |
``` |
| 16 |
|
| 17 |
We also need to add `libc`, `bitflags`, `glib` and `glib-sys` and all other dependencies we used in the sys crate as dependencies. |
| 18 |
Because we are creating a wrapper for the sys crate, which we generated in the previous chapter, we also need to add the sys crate to the list of dependencies. |
| 19 |
In the automatically generated code, the sys crate is always called `ffi`, so we need to rename the sys crate in our `Cargo.toml`. |
| 20 |
For our example, this results in the following dependencies: |
| 21 |
```toml |
| 22 |
[dependencies] |
| 23 |
libc = "0.2" |
| 24 |
bitflags = "2.2" |
| 25 |
|
| 26 |
[dependencies.ffi] |
| 27 |
package = "pango-sys" |
| 28 |
path = "./pango-sys" |
| 29 |
|
| 30 |
[dependencies.glib] |
| 31 |
package = "glib-sys" |
| 32 |
git = "https://github.com/gtk-rs/gtk-rs-core" |
| 33 |
|
| 34 |
[dependencies.gobject] |
| 35 |
package = "gobject-sys" |
| 36 |
git = "https://github.com/gtk-rs/gtk-rs-core" |
| 37 |
``` |
| 38 |
|
| 39 |
In order to make the features of the sys crate available for users of your safe wrapper, you need to add features. |
| 40 |
Copy the `[features]` part of the Cargo.toml of your sys crate and paste it into the Cargo.toml of the normal crate. |
| 41 |
The features are supposed to activate the corresponding features of the sys crate, so you need to make some changes. |
| 42 |
If for example you have the following sys features: |
| 43 |
|
| 44 |
```toml |
| 45 |
[features] |
| 46 |
v1_2 = [] |
| 47 |
v1_4 = ["v1_2"] |
| 48 |
v1_6 = ["v1_4"] |
| 49 |
v1_8 = ["v1_6"] |
| 50 |
v1_10 = ["v1_8"] |
| 51 |
v1_12 = ["v1_10"] |
| 52 |
v1_14 = ["v1_12"] |
| 53 |
v1_16 = ["v1_14"] |
| 54 |
v1_18 = ["v1_16"] |
| 55 |
v1_20 = ["v1_18"] |
| 56 |
v1_22 = ["v1_20"] |
| 57 |
v1_24 = ["v1_22"] |
| 58 |
v1_26 = ["v1_24"] |
| 59 |
v1_30 = ["v1_26"] |
| 60 |
v1_31 = ["v1_30"] |
| 61 |
v1_32 = ["v1_31"] |
| 62 |
v1_32_4 = ["v1_32"] |
| 63 |
v1_34 = ["v1_32_4"] |
| 64 |
v1_36_7 = ["v1_34"] |
| 65 |
v1_38 = ["v1_36_7"] |
| 66 |
v1_42 = ["v1_38"] |
| 67 |
v1_44 = ["v1_42"] |
| 68 |
v1_46 = ["v1_44"] |
| 69 |
v1_48 = ["v1_46"] |
| 70 |
v1_50 = ["v1_48"] |
| 71 |
v1_52 = ["v1_50"] |
| 72 |
``` |
| 73 |
|
| 74 |
You need to change the features in the Cargo.toml of your normal crate to |
| 75 |
|
| 76 |
```toml |
| 77 |
[features] |
| 78 |
v1_2 = ["ffi/v1_2"] |
| 79 |
v1_4 = ["ffi/v1_4", "v1_2"] |
| 80 |
v1_6 = ["ffi/v1_6", "v1_4"] |
| 81 |
v1_8 = ["ffi/v1_8", "v1_6"] |
| 82 |
v1_10 = ["ffi/v1_10", "v1_8"] |
| 83 |
v1_12 = ["ffi/v1_12", "v1_10"] |
| 84 |
v1_14 = ["ffi/v1_14", "v1_12"] |
| 85 |
v1_16 = ["ffi/v1_16", "v1_14"] |
| 86 |
v1_18 = ["ffi/v1_18", "v1_16"] |
| 87 |
v1_20 = ["ffi/v1_20", "v1_18"] |
| 88 |
v1_22 = ["ffi/v1_22", "v1_20"] |
| 89 |
v1_24 = ["ffi/v1_24", "v1_22"] |
| 90 |
v1_26 = ["ffi/v1_26", "v1_24"] |
| 91 |
v1_30 = ["ffi/v1_30", "v1_26"] |
| 92 |
v1_31 = ["ffi/v1_31", "v1_30"] |
| 93 |
v1_32 = ["ffi/v1_32", "v1_31"] |
| 94 |
v1_32_4 = ["ffi/v1_32_4", "v1_32"] |
| 95 |
v1_34 = ["ffi/v1_34", "v1_32_4"] |
| 96 |
v1_36_7 = ["ffi/v1_36_7", "v1_34"] |
| 97 |
v1_38 = ["ffi/v1_38", "v1_36_7"] |
| 98 |
v1_42 = ["ffi/v1_42", "v1_38"] |
| 99 |
v1_44 = ["ffi/v1_44", "v1_42"] |
| 100 |
v1_46 = ["ffi/v1_46", "v1_44"] |
| 101 |
v1_48 = ["ffi/v1_48", "v1_46"] |
| 102 |
v1_50 = ["ffi/v1_50", "v1_48"] |
| 103 |
v1_52 = ["ffi/v1_52", "v1_50"] |
| 104 |
``` |
| 105 |
|
| 106 |
## The lib.rs file |
| 107 |
The lib.rs file will not be replaced when you run gir. |
| 108 |
All the code that gir will generate for us is going to be in src/auto. |
| 109 |
We need to include all `auto` files in our library. |
| 110 |
Also it needs to include sys create so auto files can use it. |
| 111 |
To do so, let's update the `src/lib.rs` file as follows: |
| 112 |
|
| 113 |
```rust |
| 114 |
#![cfg_attr(docsrs, feature(doc_cfg))] |
| 115 |
|
| 116 |
use ffi; |
| 117 |
pub use auto::*; |
| 118 |
mod auto; |
| 119 |
``` |
| 120 |
|
| 121 |
|
| 122 |
## The Gir.toml file |
| 123 |
As you certainly guessed, we have to fill our `Gir.toml` file for the normal crate as well. |
| 124 |
Let's write it: |
| 125 |
|
| 126 |
```toml |
| 127 |
[options] |
| 128 |
library = "Pango" |
| 129 |
version = "1.0" |
| 130 |
min_cfg_version = "1.0" |
| 131 |
target_path = "." |
| 132 |
girs_directories = ["../gir-files"] |
| 133 |
work_mode = "normal" |
| 134 |
single_version_file = true |
| 135 |
generate_safety_asserts = true |
| 136 |
deprecate_by_min_version = true |
| 137 |
|
| 138 |
generate = [] |
| 139 |
|
| 140 |
manual = [] |
| 141 |
``` |
| 142 |
|
| 143 |
Many of these options look familiar from the last chapter but there are also a few new things in here. |
| 144 |
Let's take a look at them: |
| 145 |
|
| 146 |
* `work_mode` value is now set to `normal`, it means it'll generate the high-level Rust api instead of the sys-level. |
| 147 |
* `generate_safety_asserts` is used to generates checks to ensure that, or any other kind of initialization needed before being able to use the library. |
| 148 |
* `deprecate_by_min_version` is used to generate a [Rust "#[deprecated]"](https://doc.rust-lang.org/edition-guide/rust-2018/the-compiler/an-attribute-for-deprecation.html) attribute based on the deprecation information provided by the `.gir` file. |
| 149 |
* `generate = []`: this line currently does nothing. |
| 150 |
We say to [gir] to generate nothing. |
| 151 |
We'll fill it later on. |
| 152 |
* `manual = []`: this line currently does nothing. |
| 153 |
We can let [gir] know about objects which it does not have to generate code for. |
| 154 |
|
| 155 |
Let's make a first generation of our high-level Rust API! |
| 156 |
|
| 157 |
```sh |
| 158 |
gir -o . |
| 159 |
``` |
| 160 |
|
| 161 |
If you take a look at which files and folders were created, you'll see a new "auto" folder inside "src". |
| 162 |
This folder contains all the generated code. |
| 163 |
It doesn't contain anything though. |
| 164 |
Which makes sense since we're generating nothing. |
| 165 |
|
| 166 |
Now it's time to introduce you to a whole new [gir] mode: `not_bound`. |
| 167 |
Let's give it a try: |
| 168 |
|
| 169 |
```console |
| 170 |
> gir -o . -m not_bound |
| 171 |
[NOT GENERATED] Pango.Glyph |
| 172 |
[NOT GENERATED] Pango.GlyphUnit |
| 173 |
[NOT GENERATED] Pango.GlyphItem |
| 174 |
[NOT GENERATED] Pango.LayoutRun |
| 175 |
[NOT GENERATED] Pango.Alignment |
| 176 |
[NOT GENERATED] Pango.Font |
| 177 |
[NOT GENERATED] Pango.Language |
| 178 |
[NOT GENERATED] Pango.Analysis |
| 179 |
[NOT GENERATED] Pango.AttrType |
| 180 |
[NOT GENERATED] Pango.Attribute |
| 181 |
[NOT GENERATED] Pango.Color |
| 182 |
[NOT GENERATED] Pango.AttrColor |
| 183 |
[NOT GENERATED] Pango.AttrFloat |
| 184 |
[NOT GENERATED] Pango.FontDescription |
| 185 |
[NOT GENERATED] Pango.AttrFontDesc |
| 186 |
[NOT GENERATED] Pango.AttrFontFeatures |
| 187 |
[NOT GENERATED] Pango.AttrInt |
| 188 |
[NOT GENERATED] Pango.AttrIterator |
| 189 |
[NOT GENERATED] Pango.AttrLanguage |
| 190 |
[NOT GENERATED] Pango.AttrList |
| 191 |
[NOT GENERATED] Pango.Rectangle |
| 192 |
[NOT GENERATED] Pango.AttrShape |
| 193 |
[NOT GENERATED] Pango.AttrSize |
| 194 |
[NOT GENERATED] Pango.AttrString |
| 195 |
[NOT GENERATED] Pango.BaselineShift |
| 196 |
[NOT GENERATED] Pango.BidiType (deprecated in 1.44) |
| 197 |
[NOT GENERATED] Pango.Context |
| 198 |
[NOT GENERATED] Pango.Direction |
| 199 |
[NOT GENERATED] Pango.Gravity |
| 200 |
[NOT GENERATED] Pango.FontMap |
| 201 |
[NOT GENERATED] Pango.GravityHint |
| 202 |
[NOT GENERATED] Pango.Matrix |
| 203 |
[NOT GENERATED] Pango.FontMetrics |
| 204 |
[NOT GENERATED] Pango.FontFamily |
| 205 |
[NOT GENERATED] Pango.Fontset |
| 206 |
[NOT GENERATED] Pango.Coverage |
| 207 |
[NOT GENERATED] Pango.CoverageLevel |
| 208 |
[NOT GENERATED] Pango.EllipsizeMode |
| 209 |
[NOT GENERATED] Pango.FontFace |
| 210 |
[NOT GENERATED] Pango.FontMask |
| 211 |
[NOT GENERATED] Pango.Stretch |
| 212 |
[NOT GENERATED] Pango.Style |
| 213 |
[NOT GENERATED] Pango.Variant |
| 214 |
[NOT GENERATED] Pango.Weight |
| 215 |
[NOT GENERATED] Pango.FontScale |
| 216 |
[NOT GENERATED] Pango.FontsetSimple |
| 217 |
[NOT GENERATED PARENT] Pango.Fontset |
| 218 |
[NOT GENERATED] Pango.GlyphGeometry |
| 219 |
[NOT GENERATED] Pango.GlyphVisAttr |
| 220 |
[NOT GENERATED] Pango.GlyphInfo |
| 221 |
[NOT GENERATED] Pango.Item |
| 222 |
[NOT GENERATED] Pango.GlyphString |
| 223 |
[NOT GENERATED] Pango.LogAttr |
| 224 |
[NOT GENERATED] Pango.GlyphItemIter |
| 225 |
[NOT GENERATED] Pango.Script |
| 226 |
[NOT GENERATED] Pango.Layout |
| 227 |
[NOT GENERATED] Pango.LayoutDeserializeFlags |
| 228 |
[NOT GENERATED] Pango.LayoutIter |
| 229 |
[NOT GENERATED] Pango.LayoutLine |
| 230 |
[NOT GENERATED] Pango.TabArray |
| 231 |
[NOT GENERATED] Pango.WrapMode |
| 232 |
[NOT GENERATED] Pango.LayoutSerializeFlags |
| 233 |
[NOT GENERATED] Pango.LayoutDeserializeError |
| 234 |
[NOT GENERATED] Pango.Overline |
| 235 |
[NOT GENERATED] Pango.RenderPart |
| 236 |
[NOT GENERATED] Pango.Renderer |
| 237 |
[NOT GENERATED] Pango.Underline |
| 238 |
[NOT GENERATED] Pango.ScriptIter |
| 239 |
[NOT GENERATED] Pango.ShapeFlags |
| 240 |
[NOT GENERATED] Pango.ShowFlags |
| 241 |
[NOT GENERATED] Pango.TabAlign |
| 242 |
[NOT GENERATED] Pango.TextTransform |
| 243 |
[NOT GENERATED FUNCTION] Pango.attr_allow_breaks_new because of Pango.Attribute |
| 244 |
[NOT GENERATED FUNCTION] Pango.attr_background_alpha_new because of Pango.Attribute |
| 245 |
[NOT GENERATED FUNCTION] Pango.attr_background_new because of Pango.Attribute |
| 246 |
[NOT GENERATED FUNCTION] Pango.attr_baseline_shift_new because of Pango.Attribute |
| 247 |
[NOT GENERATED FUNCTION] Pango.attr_break because of Pango.AttrList and Pango.LogAttr |
| 248 |
[NOT GENERATED FUNCTION] Pango.attr_fallback_new because of Pango.Attribute |
| 249 |
[NOT GENERATED FUNCTION] Pango.attr_family_new because of Pango.Attribute |
| 250 |
[NOT GENERATED FUNCTION] Pango.attr_font_scale_new because of Pango.FontScale and Pango.Attribute |
| 251 |
[NOT GENERATED FUNCTION] Pango.attr_foreground_alpha_new because of Pango.Attribute |
| 252 |
[NOT GENERATED FUNCTION] Pango.attr_foreground_new because of Pango.Attribute |
| 253 |
[NOT GENERATED FUNCTION] Pango.attr_gravity_hint_new because of Pango.GravityHint and Pango.Attribute |
| 254 |
[NOT GENERATED FUNCTION] Pango.attr_gravity_new because of Pango.Gravity and Pango.Attribute |
| 255 |
[NOT GENERATED FUNCTION] Pango.attr_insert_hyphens_new because of Pango.Attribute |
| 256 |
[NOT GENERATED FUNCTION] Pango.attr_letter_spacing_new because of Pango.Attribute |
| 257 |
[NOT GENERATED FUNCTION] Pango.attr_line_height_new because of Pango.Attribute |
| 258 |
[NOT GENERATED FUNCTION] Pango.attr_line_height_new_absolute because of Pango.Attribute |
| 259 |
[NOT GENERATED FUNCTION] Pango.attr_overline_color_new because of Pango.Attribute |
| 260 |
[NOT GENERATED FUNCTION] Pango.attr_overline_new because of Pango.Overline and Pango.Attribute |
| 261 |
[NOT GENERATED FUNCTION] Pango.attr_rise_new because of Pango.Attribute |
| 262 |
[NOT GENERATED FUNCTION] Pango.attr_scale_new because of Pango.Attribute |
| 263 |
[NOT GENERATED FUNCTION] Pango.attr_sentence_new because of Pango.Attribute |
| 264 |
[NOT GENERATED FUNCTION] Pango.attr_show_new because of Pango.ShowFlags and Pango.Attribute |
| 265 |
[NOT GENERATED FUNCTION] Pango.attr_stretch_new because of Pango.Stretch and Pango.Attribute |
| 266 |
[NOT GENERATED FUNCTION] Pango.attr_strikethrough_color_new because of Pango.Attribute |
| 267 |
[NOT GENERATED FUNCTION] Pango.attr_strikethrough_new because of Pango.Attribute |
| 268 |
[NOT GENERATED FUNCTION] Pango.attr_style_new because of Pango.Style and Pango.Attribute |
| 269 |
[NOT GENERATED FUNCTION] Pango.attr_text_transform_new because of Pango.TextTransform and Pango.Attribute |
| 270 |
[NOT GENERATED FUNCTION] Pango.attr_underline_color_new because of Pango.Attribute |
| 271 |
[NOT GENERATED FUNCTION] Pango.attr_underline_new because of Pango.Underline and Pango.Attribute |
| 272 |
[NOT GENERATED FUNCTION] Pango.attr_variant_new because of Pango.Variant and Pango.Attribute |
| 273 |
[NOT GENERATED FUNCTION] Pango.attr_weight_new because of Pango.Weight and Pango.Attribute |
| 274 |
[NOT GENERATED FUNCTION] Pango.attr_word_new because of Pango.Attribute |
| 275 |
[NOT GENERATED FUNCTION] Pango.break (deprecated in 1.44) because of Pango.Analysis and Pango.LogAttr |
| 276 |
[NOT GENERATED FUNCTION] Pango.default_break because of Pango.Analysis and Pango.LogAttr |
| 277 |
[NOT GENERATED FUNCTION] Pango.extents_to_pixels because of Pango.Rectangle and Pango.Rectangle |
| 278 |
[NOT GENERATED FUNCTION] Pango.find_base_dir because of Pango.Direction |
| 279 |
[NOT GENERATED FUNCTION] Pango.get_log_attrs because of Pango.Language and Pango.LogAttr |
| 280 |
[NOT GENERATED FUNCTION] Pango.itemize because of Pango.Context, Pango.AttrList, Pango.AttrIterator and Pango.Item |
| 281 |
[NOT GENERATED FUNCTION] Pango.itemize_with_base_dir because of Pango.Context, Pango.Direction, Pango.AttrList, Pango.AttrIterator and Pango.Item |
| 282 |
[NOT GENERATED FUNCTION] Pango.log2vis_get_embedding_levels because of Pango.Direction |
| 283 |
[NOT GENERATED FUNCTION] Pango.markup_parser_finish because of GLib.MarkupParseContext, Pango.AttrList and GLib.Error |
| 284 |
[NOT GENERATED FUNCTION] Pango.markup_parser_new because of GLib.MarkupParseContext |
| 285 |
[NOT GENERATED FUNCTION] Pango.parse_markup because of Pango.AttrList and GLib.Error |
| 286 |
[NOT GENERATED FUNCTION] Pango.parse_stretch because of Pango.Stretch |
| 287 |
[NOT GENERATED FUNCTION] Pango.parse_style because of Pango.Style |
| 288 |
[NOT GENERATED FUNCTION] Pango.parse_variant because of Pango.Variant |
| 289 |
[NOT GENERATED FUNCTION] Pango.parse_weight because of Pango.Weight |
| 290 |
[NOT GENERATED FUNCTION] Pango.read_line (deprecated in 1.38) because of GLib.String |
| 291 |
[NOT GENERATED FUNCTION] Pango.reorder_items because of Pango.Item and Pango.Item |
| 292 |
[NOT GENERATED FUNCTION] Pango.scan_string (deprecated in 1.38) because of GLib.String |
| 293 |
[NOT GENERATED FUNCTION] Pango.scan_word (deprecated in 1.38) because of GLib.String |
| 294 |
[NOT GENERATED FUNCTION] Pango.shape because of Pango.Analysis and Pango.GlyphString |
| 295 |
[NOT GENERATED FUNCTION] Pango.shape_full because of Pango.Analysis and Pango.GlyphString |
| 296 |
[NOT GENERATED FUNCTION] Pango.shape_item because of Pango.Item, Pango.LogAttr, Pango.GlyphString and Pango.ShapeFlags |
| 297 |
[NOT GENERATED FUNCTION] Pango.shape_with_flags because of Pango.Analysis, Pango.GlyphString and Pango.ShapeFlags |
| 298 |
[NOT GENERATED FUNCTION] Pango.tailor_break because of Pango.Analysis and Pango.LogAttr |
| 299 |
[NOT GENERATED FUNCTION] Pango.unichar_direction because of Pango.Direction |
| 300 |
``` |
| 301 |
|
| 302 |
We now have the list of all the not-yet generated items. |
| 303 |
Quite convenient. |
| 304 |
There can be different kinds of not generated items: |
| 305 |
|
| 306 |
* `[NOT GENERATED]`: |
| 307 |
Objects marked with `[NOT GENERATED]` are objects that we can generate, but we did not (yet) add to the `generate` array. |
| 308 |
* `[NOT GENERATED PARENT]`: |
| 309 |
These objects live in a dependency of the current library. |
| 310 |
These are the objects we will add to the `manual` array in the following steps. |
| 311 |
* `[NOT GENERATED FUNCTION]`: |
| 312 |
These are global functions that were not generated. |
| 313 |
To fix it, we just add `"NameOfYourLibrary.*"` to the `generate` array in the Git.toml and add the following line to your src/lib.rs file: |
| 314 |
```rust |
| 315 |
pub mod functions { |
| 316 |
pub use super::auto::functions::*; |
| 317 |
} |
| 318 |
``` |
| 319 |
|
| 320 |
## Generating the code |
| 321 |
In order to generate the code for the safe wrapper, we follow these steps until all objects have been generated: |
| 322 |
|
| 323 |
- Run `gir -o . -m not_bound` to see which objects have not been generated yet |
| 324 |
- Pick one of the types marked with `[NOT GENERATED]` |
| 325 |
- Add it to the `generate` array in the Gir.toml file |
| 326 |
- Run `gir -o .` to generate the code |
| 327 |
- Open the generated files under src/auto and have a look at them |
| 328 |
- Search for `/*Ignored*/`. |
| 329 |
If the type name following `/*Ignored*/` is prepended by `[crate_name]::` (e.g `Ignored*/&glib::MarkupParseContext`), |
| 330 |
- then we add it to the `manual` array. |
| 331 |
By doing so we tell [gir] that those types have been generated somewhere else and that they can be used just like the other types. |
| 332 |
- Otherwise, the type comes from the current crate and we just put it into the `generate` list of the `Gir.toml` file. |
| 333 |
- Start with the first step again |
| 334 |
|
| 335 |
The names of the objects are not the same as the crates names. |
| 336 |
You have to use the names of the corresponding gir files. |
| 337 |
|
| 338 |
Okay, let's go through that process for a few objects of our example. |
| 339 |
|
| 340 |
🚧 TODO: Add remaining steps of the pango example 🚧 |
| 341 |
|
| 342 |
Again, if you do it on another library and it fails and you can't figure out why, don't hesitate to [contact us](https://gtk-rs.org/contact)! |
| 343 |
|
| 344 |
At this point, you should have almost everything you need. |
| 345 |
Let's have a look at errors that can happen in this process. |
| 346 |
|
| 347 |
|
| 348 |
[gir]: https://github.com/gtk-rs/gir |