Skip to content

Commit c00d2be

Browse files
committed
README WIP
1 parent dc0fcbc commit c00d2be

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

ctaffy/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# C API for Taffy (ctaffy)
2+
3+
Taffy is a flexible, high-performance, cross-platform UI layout library written in Rust.
4+
5+
This directory contains C bindings for Taffy. The API is a pure C API (no C++ features are used), and is designed to be easy to build other language bindings on top of. In addition to the documentation below, you may to read through the header file (`include/taffy.h`).
6+
7+
## Examples
8+
9+
There are readable examples in the examples directory.
10+
11+
Assuming you have Rust and Cargo installed (and a C compiler), then this should work to run the basic example:
12+
13+
```bash
14+
$ git clone https://github.com/DioxusLabs/taffy.git
15+
$ cd taffy/ctaffy/examples
16+
$ ./compile-basic.sh
17+
$ ./basic
18+
```
19+
20+
## Naming Conventions
21+
22+
- Everything in the Taffy C API is prefixed with `Taffy`, except enum variant names which are prefixed with `TAFFY_`
23+
- Structs and Enums are named in UpperCamelCase (e.g. `TaffyTree`, `TaffyStyle`)
24+
- Functions begin with the name of the struct they apply to, followed by an underscore, followed by the name of the function in UpperCamelCase (e.g. `TaffyTree_New`, `TaffyStyle_SetFlexBasis`)
25+
- Enum variants are SCREAMING_SNAKE_CASE
26+
27+
## Error Handling
28+
29+
Error handling is managed by the use of return codes and "result" structs. All functions in the API return either an `enum TaffyReturnCode` or one of the `struct Taffy*Result` structs (or `void` in the case of infallible operations that don't return anything).
30+
31+
### Return codes
32+
33+
Error handling is managed by the use of an enum `TaffyReturnCode`:
34+
35+
```c
36+
typedef enum TaffyReturnCode {
37+
TAFFY_RETURN_CODE_OK,
38+
TAFFY_RETURN_CODE_NULL_STYLE_POINTER,
39+
TAFFY_RETURN_CODE_NULL_TREE_POINTER,
40+
// ... (see header file for full definition)
41+
} TaffyReturnCode;
42+
```
43+
44+
`TAFFY_RETURN_CODE_OK` indicates that the operation succeeded. All other variant indicate
45+
46+
### Result structs
47+
48+
"Result structs" are used for functions that need to return another value in addition to a `TaffyReturnCode` indicating success/failure (such as style getters which need to return the relevant style value). As C doesn't support generic structs, there are several "Result structs": one for each type of value. But each struct follows the same structure as the following example (varying only in the name of the struct and the type of the `value` field):
49+
50+
<table>
51+
<tr><th>TaffyIntResult</th><th>TaffyDimensionResult</th></tr>
52+
<tr>
53+
<td>
54+
55+
```c
56+
typedef struct TaffyIntResult {
57+
enum TaffyReturnCode return_code;
58+
int32_t value;
59+
} TaffyIntResult;</code></pre></td>
60+
```
61+
62+
</td>
63+
<td>
64+
65+
```c
66+
typedef struct TaffyDimensionResult {
67+
enum TaffyReturnCode return_code;
68+
struct TaffyDimension value;
69+
} TaffyDimensionResult;
70+
```
71+
72+
</td>
73+
</tr>
74+
</table>
75+
76+
Functions that return Result structs will either return a `TAFFY_RETURN_CODE_OK` along with a meaningful value, or a error variant of `TaffyReturnCode` along with

0 commit comments

Comments
 (0)