@arrirpc/codegen-rust 0.52.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2024 Joshua Sosso
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Arri Rust Codegen
2
+
3
+ ## Setup
4
+
5
+ ### 1) Add the generator to your arri config
6
+
7
+ ```ts
8
+ // arri.config.ts
9
+ import { defineConfig, generators } from "arri";
10
+
11
+ export default defineConfig({
12
+ generators: [
13
+ generators.rustClient({
14
+ clientName: "MyClient",
15
+ outputFile: "./some-project/my_client.g.rs",
16
+ }),
17
+ ],
18
+ });
19
+ ```
20
+
21
+ **Options**
22
+
23
+ | Name | Descriptions |
24
+ | --------------------- | -------------------------------------------------------------------------- |
25
+ | clientName | The named of the generated client struct (Defaults to "Client") |
26
+ | outputFile (required) | Path to the file that will be created by the generator |
27
+ | typePrefix | Add a prefix to the generated struct names |
28
+ | format | Whether to run `rustfmt` on the generated file or not (Defaults to "true") |
29
+
30
+ ### 2) Install the Rust client library
31
+
32
+ The generated code relies on the [arri_client](/languages/rust/rust-client/README.md) library. So make sure it's installed wherever the generated code is being used. The version number should match your arri cli version. (Run `arri version` to check)
33
+
34
+ ```bash
35
+ cargo add arri_client
36
+ ```
37
+
38
+ ## Using the generated code
39
+
40
+ ### Initialize the client
41
+
42
+ ```rust
43
+ let config = ArriClientConfig {
44
+ http_client: reqwest::Client::new(),
45
+ base_url: "https://example.com".to_string(),
46
+ // this function will run before every request
47
+ headers: || {
48
+ let mut header_map = Hashmap::<&'static str, &'static str>::new();
49
+ header_map.insert("some-header", "some-header-value");
50
+ header_map
51
+ }
52
+ }
53
+ let client = MyClient::create(&config);
54
+
55
+ client.my_procedure().await;
56
+ ```
57
+
58
+ The root client will be a struct containing all of the services and procedures. If you only need a particular service you can initialize just that service.
59
+
60
+ ```rust
61
+ let users_service = MyClientUsersService(&config);
62
+ users_service.some_procedure().await;
63
+ ```
64
+
65
+ ### Using the generated types
66
+
67
+ All the generated types will have the following methods implemented
68
+
69
+ - `from_json_string(String input) -> Self`
70
+ - `from_json(serde_json::Value input) -> Self`
71
+ - `to_json(&Self) -> serde_json::Value`
72
+ - `to_json_string(&Self) -> String`
73
+ - `to_query_params_string(&Self) -> String`
74
+
75
+ `serde_json` is used for parsing JSON. However we do not rely on `serde` itself for serializing and deserializing.
76
+
77
+ The generated types also derive the following traits
78
+
79
+ - Clone
80
+ - Debug
81
+ - PartialEq
82
+
83
+ # Development
84
+
85
+ ```bash
86
+ # run unit tests
87
+ nx test rust-codegen
88
+ ```