yerba 0.2.0-arm-linux-gnu
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +528 -0
- data/exe/yerba +6 -0
- data/ext/yerba/extconf.rb +111 -0
- data/ext/yerba/yerba.c +752 -0
- data/lib/yerba/3.2/yerba.so +0 -0
- data/lib/yerba/3.3/yerba.so +0 -0
- data/lib/yerba/3.4/yerba.so +0 -0
- data/lib/yerba/4.0/yerba.so +0 -0
- data/lib/yerba/collection.rb +31 -0
- data/lib/yerba/document.rb +59 -0
- data/lib/yerba/formatting.rb +18 -0
- data/lib/yerba/location.rb +5 -0
- data/lib/yerba/map.rb +166 -0
- data/lib/yerba/scalar.rb +85 -0
- data/lib/yerba/sequence.rb +182 -0
- data/lib/yerba/version.rb +5 -0
- data/lib/yerba.rb +131 -0
- data/rust/Cargo.lock +805 -0
- data/rust/Cargo.toml +36 -0
- data/rust/build.rs +11 -0
- data/rust/cbindgen.toml +27 -0
- data/rust/rustfmt.toml +2 -0
- data/rust/src/commands/apply.rs +5 -0
- data/rust/src/commands/blank_lines.rs +58 -0
- data/rust/src/commands/check.rs +5 -0
- data/rust/src/commands/delete.rs +35 -0
- data/rust/src/commands/get.rs +194 -0
- data/rust/src/commands/init.rs +89 -0
- data/rust/src/commands/insert.rs +106 -0
- data/rust/src/commands/mate.rs +55 -0
- data/rust/src/commands/mod.rs +349 -0
- data/rust/src/commands/move_item.rs +54 -0
- data/rust/src/commands/move_key.rs +87 -0
- data/rust/src/commands/quote_style.rs +62 -0
- data/rust/src/commands/remove.rs +35 -0
- data/rust/src/commands/rename.rs +37 -0
- data/rust/src/commands/set.rs +59 -0
- data/rust/src/commands/sort.rs +52 -0
- data/rust/src/commands/sort_keys.rs +62 -0
- data/rust/src/commands/version.rs +8 -0
- data/rust/src/document.rs +2237 -0
- data/rust/src/error.rs +45 -0
- data/rust/src/ffi.rs +991 -0
- data/rust/src/json.rs +128 -0
- data/rust/src/lib.rs +29 -0
- data/rust/src/main.rs +72 -0
- data/rust/src/quote_style.rs +42 -0
- data/rust/src/selector.rs +241 -0
- data/rust/src/syntax.rs +262 -0
- data/rust/src/yaml_writer.rs +89 -0
- data/rust/src/yerbafile.rs +475 -0
- data/sig/yerba.rbs +3 -0
- data/yerba.gemspec +52 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 597f44a14c2f9daf9442e2806343277b45b49b5ff71ab8e92537cd8121991634
|
|
4
|
+
data.tar.gz: a380ffecf57a598dd827e7b1cc3ac9cba0f0c267142483f42e2c0a3ed6b57365
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1573d53df4d2c0795a13d947c70a2a2c2207b840395e03c4fa9255528e93c3ec03468d805a9753b641c0d2c288169d6bd06f68dc397e7fbd39dea2ba877ae48d
|
|
7
|
+
data.tar.gz: c0b433f08fac4920ebd2b9b387c2b32a651c0e1306d71bb2d18fc90d50a8e30115b487994943c825a72deebaa6369a8e8266f1500225e7a5a50537414e86683e
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marco Roth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="assets/logo.png" alt="Yerba Logo" width="150px">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h2 align="center">Yerba</h2>
|
|
6
|
+
|
|
7
|
+
<h4 align="center"><u>Y</u>AML <u>E</u>diting and <u>R</u>efactoring with <u>B</u>etter <u>A</u>ccuracy</h4>
|
|
8
|
+
|
|
9
|
+
<div align="center">A Rust CLI tool and Ruby library for editing YAML while preserving structure, comments, and format.</div><br/>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://rubygems.org/gems/yerba"><img alt="Gem Version" src="https://img.shields.io/gem/v/yerba"></a>
|
|
13
|
+
<a href="https://crates.io/crates/yerba"><img alt="Crates.io Version" src="https://img.shields.io/crates/v/yerba"></a>
|
|
14
|
+
<a href="https://github.com/marcoroth/yerba/blob/main/LICENSE.txt"><img alt="License" src="https://img.shields.io/github/license/marcoroth/yerba"></a>
|
|
15
|
+
<a href="https://github.com/marcoroth/yerba/issues"><img alt="Issues" src="https://img.shields.io/github/issues/marcoroth/yerba"></a>
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<br/>
|
|
19
|
+
|
|
20
|
+
## What is Yerba?
|
|
21
|
+
|
|
22
|
+
Yerba is a lossless YAML editing tool. It lets you programmatically read, modify, and enforce formatting in YAML files while preserving their original structure, including comments, blank lines, quote styles, and key ordering.
|
|
23
|
+
|
|
24
|
+
Most YAML libraries parse a file into a data structure and serialize it back, discarding all formatting in the process. Yerba operates on the concrete syntax tree (CST), so your edits are surgical: only the targeted values change, and everything else stays exactly as it was.
|
|
25
|
+
|
|
26
|
+
Yerba is available as:
|
|
27
|
+
|
|
28
|
+
- A **standalone CLI binary** with zero runtime dependencies
|
|
29
|
+
- A **Rust crate** for embedding in Rust applications
|
|
30
|
+
- A **Ruby gem** for programmatic YAML editing from Ruby
|
|
31
|
+
|
|
32
|
+
Yerba was born out of the need to manage, validate, and enforce consistent formatting for hundreds of YAML data files in the [RubyEvents.org](https://github.com/rubyevents/rubyevents) project.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
### CLI (standalone)
|
|
37
|
+
|
|
38
|
+
The `yerba` CLI is a standalone Rust binary with no Ruby dependency. Install it via Cargo:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
cargo install yerba
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Rust Crate
|
|
45
|
+
|
|
46
|
+
Use `yerba` as a library in your Rust project:
|
|
47
|
+
|
|
48
|
+
```toml
|
|
49
|
+
[dependencies]
|
|
50
|
+
yerba = "0.1"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```rust
|
|
54
|
+
let mut document = yerba::parse_file("config.yml")?;
|
|
55
|
+
document.set("database.host", "0.0.0.0")?;
|
|
56
|
+
document.save()?;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Ruby Gem
|
|
60
|
+
|
|
61
|
+
The Ruby gem bundles both the CLI binary and a native extension for programmatic access from Ruby:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
gem install yerba
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Or add it to your `Gemfile`:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
gem "yerba"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The gem ships with precompiled binaries for macOS and Linux.
|
|
74
|
+
|
|
75
|
+
If no precompiled binary is available for your platform, it will compile from source automatically, which requires a [Rust toolchain](https://rustup.rs).
|
|
76
|
+
|
|
77
|
+
## CLI Usage
|
|
78
|
+
|
|
79
|
+
The `yerba` CLI follows a consistent pattern:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
yerba <command> <file> <selector> [value] [options]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Selectors use dot-notation for nested keys, brackets for array access, and support glob patterns for operating on multiple files at once.
|
|
86
|
+
|
|
87
|
+
### Selectors
|
|
88
|
+
|
|
89
|
+
Selectors let you address any node in a YAML document:
|
|
90
|
+
|
|
91
|
+
| Pattern | Meaning | Example |
|
|
92
|
+
|---------|---------|---------|
|
|
93
|
+
| `key` | A single key | `"database.host"` |
|
|
94
|
+
| `key.nested` | Nested key path | `"database.settings.pool"` |
|
|
95
|
+
| `[]` | All items in array | `"[].title"` |
|
|
96
|
+
| `[N]` | Item at index | `"[0].title"` |
|
|
97
|
+
| `[].key[].nested` | Nested array access | `"[].speakers[].name"` |
|
|
98
|
+
|
|
99
|
+
### Conditions
|
|
100
|
+
|
|
101
|
+
Conditions filter which items a command operates on:
|
|
102
|
+
|
|
103
|
+
| Syntax | Meaning | Example |
|
|
104
|
+
|--------|---------|---------|
|
|
105
|
+
| `.key == value` | Equality | `".kind == keynote"` |
|
|
106
|
+
| `.key != value` | Inequality | `".status != draft"` |
|
|
107
|
+
| `.key contains val` | Substring or member | `".title contains Ruby"` |
|
|
108
|
+
| `.key not_contains val` | Negated contains | `".title not_contains test"` |
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### `get`
|
|
113
|
+
|
|
114
|
+
Retrieve values from YAML files. Supports single values, array traversal, glob patterns across multiple files, conditions for filtering, and field selection.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
yerba get config.yml "database.host"
|
|
118
|
+
yerba get videos.yml "[].title"
|
|
119
|
+
yerba get videos.yml "[0].title"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Use `--select` to pick specific fields from each item, and `--condition` to filter which items are returned:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
yerba get videos.yml "[]" --select ".title,.speakers"
|
|
126
|
+
yerba get videos.yml "[]" --condition ".kind == keynote"
|
|
127
|
+
yerba get videos.yml "[]" --select ".title" --condition ".kind == keynote"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Glob patterns let you query across many files at once:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
yerba get "data/**/videos.yml" "[].speakers[].name"
|
|
134
|
+
yerba get "data/**/videos.yml" "[]" --condition ".kind == keynote" --select ".id,.title"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Use `--raw` to output plain values (one per line) instead of JSON:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
yerba get videos.yml "[]" --condition ".speakers contains Matz" --raw
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### `set`
|
|
144
|
+
|
|
145
|
+
Update an existing value at a path. The original quote style is preserved automatically, if a value was double-quoted before, it stays double-quoted after the edit.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
yerba set config.yml "database.host" "0.0.0.0"
|
|
149
|
+
yerba set videos.yml "[0].title" "New Title"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Use `--if-exists` to only set the value when the path already exists, or `--if-missing` to only set it when the path does not exist:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
yerba set config.yml "database.host" "0.0.0.0" --if-exists
|
|
156
|
+
yerba set "data/**/event.yml" "website" "" --if-exists
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Use `--condition` to only apply the change when a sibling field matches:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
yerba set config.yml "database.host" "0.0.0.0" --condition ".port == 5432"
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### `insert`
|
|
166
|
+
|
|
167
|
+
Insert a new key into a map or a new item into a sequence. By default, new items are appended at the end.
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
yerba insert config.yml "database.ssl" true
|
|
171
|
+
yerba insert config.yml "tags" "yaml"
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Control placement with `--before`, `--after`, or `--at`:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
yerba insert config.yml "database.ssl" true --after "host"
|
|
178
|
+
yerba insert config.yml "database.ssl" true --before "port"
|
|
179
|
+
yerba insert config.yml "tags" "yaml" --at 0
|
|
180
|
+
yerba insert config.yml "tags" "yaml" --after "ruby"
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
For sequences of maps, use conditions to position relative to other items:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
yerba insert speakers.yml "" "name: Bob" --after ".name == Alice"
|
|
187
|
+
yerba insert videos.yml "[0].speakers" "Diana" --before ".name == Charlie"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Use `--from` to read the value from another file (or stdin with `-`):
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
yerba insert videos.yml "" --from "new_talk.yml" --after ".id == first-talk"
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### `delete`
|
|
197
|
+
|
|
198
|
+
Remove a key and its value from a map:
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
yerba delete config.yml "database.pool"
|
|
202
|
+
yerba delete videos.yml "[0].description"
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Use `--dry-run` to preview the result without writing to the file:
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
yerba delete config.yml "database.pool" --dry-run
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### `remove`
|
|
212
|
+
|
|
213
|
+
Remove a specific item from a sequence by its value:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
yerba remove config.yml "tags" "rust"
|
|
217
|
+
yerba remove videos.yml "[0].speakers" "Alice"
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### `rename`
|
|
221
|
+
|
|
222
|
+
Rename a key in a map while preserving its value and position:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
yerba rename config.yml "database.host" "database.hostname"
|
|
226
|
+
yerba rename config.yml "database.host" "hostname"
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### `move`
|
|
230
|
+
|
|
231
|
+
Move a sequence item to a new position. You can reference items by value, index, or condition:
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
yerba move config.yml "tags" "rust" --before "ruby"
|
|
235
|
+
yerba move config.yml "tags" "rust" --after "yaml"
|
|
236
|
+
yerba move config.yml "tags" 2 --to 0
|
|
237
|
+
yerba move videos.yml "" ".id == talk-2" --after ".id == talk-1"
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### `move-key`
|
|
241
|
+
|
|
242
|
+
Move a key to a new position within a map:
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
yerba move-key config.yml "database.name" --to 0
|
|
246
|
+
yerba move-key config.yml "database.pool" --before "database.host"
|
|
247
|
+
yerba move-key config.yml "database.pool" --after "database.name"
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### `sort`
|
|
251
|
+
|
|
252
|
+
Sort items in a sequence. For simple scalar sequences, no options are needed. For sequences of maps, use `--by` to specify sort fields. Append `:desc` for descending order:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
yerba sort config.yml "tags"
|
|
256
|
+
yerba sort videos.yml --by "title"
|
|
257
|
+
yerba sort videos.yml --by "date:desc,title"
|
|
258
|
+
yerba sort videos.yml "[].speakers" --by "name"
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### `sort-keys`
|
|
262
|
+
|
|
263
|
+
Reorder the keys in a map to match a predefined order. If any key in the document is not present in the order list, the command aborts with an error, this ensures you account for every field:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
yerba sort-keys config.yml "database" "host,port,name,pool"
|
|
267
|
+
yerba sort-keys "data/**/event.yml" "" "id,title,kind,location"
|
|
268
|
+
yerba sort-keys "data/**/videos.yml" "[]" "id,title,speakers"
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### `quote-style`
|
|
272
|
+
|
|
273
|
+
Enforce a consistent quote style across keys and/or values. Available styles are `plain`, `single`, and `double`:
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
yerba quote-style config.yml --values double
|
|
277
|
+
yerba quote-style config.yml --keys plain
|
|
278
|
+
yerba quote-style config.yml --keys plain --values double
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Scope the operation to a specific selector:
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
yerba quote-style config.yml "[].speakers" --values plain
|
|
285
|
+
yerba quote-style "data/**/*.yml" --keys plain --values double
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### `blank-lines`
|
|
289
|
+
|
|
290
|
+
Enforce a consistent number of blank lines between sequence entries:
|
|
291
|
+
|
|
292
|
+
```bash
|
|
293
|
+
yerba blank-lines videos.yml 1
|
|
294
|
+
yerba blank-lines videos.yml "[]" 1
|
|
295
|
+
yerba blank-lines config.yml "tags" 0
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## `Yerbafile`
|
|
299
|
+
|
|
300
|
+
A `Yerbafile` is a YAML configuration file that defines formatting and editing rules as pipelines of operations that are applied to your files across your project.
|
|
301
|
+
|
|
302
|
+
Use `yerba init` to create one, then `yerba apply` to apply all rules, or `yerba check` to verify compliance (exits with code `1` if files would change):
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
yerba init
|
|
306
|
+
yerba apply
|
|
307
|
+
yerba check
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Each rule specifies a file glob and a list of steps to run in order:
|
|
311
|
+
|
|
312
|
+
```yaml
|
|
313
|
+
rules:
|
|
314
|
+
- files: "config/**/*.yml"
|
|
315
|
+
pipeline:
|
|
316
|
+
- quote_style:
|
|
317
|
+
key_style: plain
|
|
318
|
+
value_style: double
|
|
319
|
+
|
|
320
|
+
- sort_keys:
|
|
321
|
+
path: ""
|
|
322
|
+
order:
|
|
323
|
+
- id
|
|
324
|
+
- title
|
|
325
|
+
- description
|
|
326
|
+
|
|
327
|
+
- blank_lines:
|
|
328
|
+
count: 1
|
|
329
|
+
|
|
330
|
+
- files: "data/speakers.yml"
|
|
331
|
+
pipeline:
|
|
332
|
+
- quote_style:
|
|
333
|
+
key_style: plain
|
|
334
|
+
value_style: double
|
|
335
|
+
|
|
336
|
+
- sort_keys:
|
|
337
|
+
path: ""
|
|
338
|
+
order:
|
|
339
|
+
- name
|
|
340
|
+
- slug
|
|
341
|
+
- github
|
|
342
|
+
- twitter
|
|
343
|
+
- website
|
|
344
|
+
|
|
345
|
+
- sort:
|
|
346
|
+
path: ""
|
|
347
|
+
by: name
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Available pipeline steps:
|
|
351
|
+
|
|
352
|
+
- `quote_style` Enforce quote style on keys and/or values, optionally scoped by path
|
|
353
|
+
- `sort_keys` Reorder keys to match a predefined list
|
|
354
|
+
- `sort` Sort sequence items by field(s)
|
|
355
|
+
- `blank_lines` Enforce blank lines between sequence entries
|
|
356
|
+
- `set` Set a value (supports conditions)
|
|
357
|
+
- `insert` Insert a new key or sequence item
|
|
358
|
+
- `delete` Remove a key (supports conditions)
|
|
359
|
+
- `rename` Rename a key
|
|
360
|
+
- `remove` Remove an item from a sequence
|
|
361
|
+
- `get` Read a value and store it as a variable for subsequent steps
|
|
362
|
+
|
|
363
|
+
This makes it easy to enforce project-wide YAML conventions in CI:
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
yerba check
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Ruby API
|
|
370
|
+
|
|
371
|
+
Yerba includes a native C extension (backed by the same Rust core) that provides a full Ruby API for YAML editing.
|
|
372
|
+
|
|
373
|
+
### Parsing
|
|
374
|
+
|
|
375
|
+
Create a document from a file path or from a string:
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
require "yerba"
|
|
379
|
+
|
|
380
|
+
document = Yerba.parse_file("config.yml")
|
|
381
|
+
document = Yerba.parse("database:\n host: localhost\n port: 5432\n")
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### Reading Values
|
|
385
|
+
|
|
386
|
+
Use `get` to retrieve the raw value at a path. Values are returned with their YAML types, strings, integers, booleans, and nil are all mapped to their Ruby equivalents:
|
|
387
|
+
|
|
388
|
+
```ruby
|
|
389
|
+
document.get("database.host") # => "localhost"
|
|
390
|
+
document.get("database.port") # => 5432
|
|
391
|
+
document.get("database.ssl") # => false
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Structured Navigation
|
|
395
|
+
|
|
396
|
+
Use bracket notation to get typed wrapper objects (`Scalar`, `Map`, or `Sequence`) representing nodes in the document. These are live references, mutations flow back to the document.
|
|
397
|
+
|
|
398
|
+
You can use a full dot-path in a single bracket call, or chain brackets to navigate one level at a time:
|
|
399
|
+
|
|
400
|
+
```ruby
|
|
401
|
+
document["database.host"].value # => "localhost"
|
|
402
|
+
document["database"]["host"].value # => "localhost"
|
|
403
|
+
document["database"]["port"].value # => 5432
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
The returned object type depends on what's at the path:
|
|
407
|
+
|
|
408
|
+
```ruby
|
|
409
|
+
document["database"] # => Yerba::Map
|
|
410
|
+
document["database.host"] # => Yerba::Scalar
|
|
411
|
+
document["tags"] # => Yerba::Sequence
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
Scalars expose their value and quote style:
|
|
415
|
+
|
|
416
|
+
```ruby
|
|
417
|
+
scalar = document["database.host"]
|
|
418
|
+
scalar.value # => "localhost"
|
|
419
|
+
scalar.quote_style # => :double
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### Mutations
|
|
423
|
+
|
|
424
|
+
Modify values in place. The original formatting is preserved:
|
|
425
|
+
|
|
426
|
+
```ruby
|
|
427
|
+
document["database"]["host"].value = "0.0.0.0"
|
|
428
|
+
document.set("database.port", 3306)
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
Insert new keys with positional control:
|
|
432
|
+
|
|
433
|
+
```ruby
|
|
434
|
+
document["database"].insert("ssl", true, after: "host")
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Work with sequences using familiar Ruby patterns:
|
|
438
|
+
|
|
439
|
+
```ruby
|
|
440
|
+
tags = document["tags"]
|
|
441
|
+
tags << "yaml"
|
|
442
|
+
tags << { name: "Rust", version: "1.80" }
|
|
443
|
+
tags.remove("obsolete")
|
|
444
|
+
tags.sort(by: "name")
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Quote Style Control
|
|
448
|
+
|
|
449
|
+
Read and set the quote style on individual scalars:
|
|
450
|
+
|
|
451
|
+
```ruby
|
|
452
|
+
scalar = document["database.host"]
|
|
453
|
+
scalar.quote_style # => :double
|
|
454
|
+
scalar.quote_style = :single
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Wildcard Access
|
|
458
|
+
|
|
459
|
+
Use `at_path` to access all items matching a wildcard selector:
|
|
460
|
+
|
|
461
|
+
```ruby
|
|
462
|
+
titles = document.at_path("[].title")
|
|
463
|
+
titles.each { |scalar| puts scalar.value }
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
### Collections
|
|
467
|
+
|
|
468
|
+
Operate on multiple files matching a glob pattern:
|
|
469
|
+
|
|
470
|
+
```ruby
|
|
471
|
+
collection = Yerba.files("data/**/videos.yml")
|
|
472
|
+
|
|
473
|
+
collection.each do |document|
|
|
474
|
+
puts document.get("[0].title")
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
collection.apply! do |document|
|
|
478
|
+
document.set("status", "published")
|
|
479
|
+
end
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Saving
|
|
483
|
+
|
|
484
|
+
Write changes back to the original file:
|
|
485
|
+
|
|
486
|
+
```ruby
|
|
487
|
+
document.save!
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
Or render the document as a string without writing to disk:
|
|
491
|
+
|
|
492
|
+
```ruby
|
|
493
|
+
document.to_s
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
## Development
|
|
497
|
+
|
|
498
|
+
After checking out the repo, run `bundle install` to install Ruby dependencies, then `bundle exec rake test` to run the test suite.
|
|
499
|
+
|
|
500
|
+
### Building from source
|
|
501
|
+
|
|
502
|
+
The Rust core is in the `rust/` directory:
|
|
503
|
+
|
|
504
|
+
```bash
|
|
505
|
+
cd rust
|
|
506
|
+
cargo build
|
|
507
|
+
cargo test
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
The C extension (for the Ruby API) is compiled via `ext/yerba/extconf.rb` which invokes `cargo build` and links against the resulting static library. Running `bundle exec rake compile` will build both the Rust library and the C extension.
|
|
511
|
+
|
|
512
|
+
### Running the CLI locally
|
|
513
|
+
|
|
514
|
+
```bash
|
|
515
|
+
cargo run -- get config.yml "database.host"
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
Or build a release binary:
|
|
519
|
+
|
|
520
|
+
```bash
|
|
521
|
+
cd rust
|
|
522
|
+
cargo build --release
|
|
523
|
+
./target/release/yerba --help
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
## License
|
|
527
|
+
|
|
528
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/marcoroth/yerba/blob/main/LICENSE.txt).
|
data/exe/yerba
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
rust_dir = File.expand_path("../../rust", __dir__)
|
|
7
|
+
root_dir = File.expand_path("../..", __dir__)
|
|
8
|
+
|
|
9
|
+
unless system("cargo --version > /dev/null 2>&1")
|
|
10
|
+
abort <<~MESSAGE
|
|
11
|
+
|
|
12
|
+
ERROR: Rust toolchain not found.
|
|
13
|
+
|
|
14
|
+
yerba requires the Rust toolchain to compile from source.
|
|
15
|
+
|
|
16
|
+
Install Rust: https://rustup.rs
|
|
17
|
+
|
|
18
|
+
MESSAGE
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
RUST_TARGETS = {
|
|
22
|
+
"aarch64-linux-gnu" => "aarch64-unknown-linux-gnu",
|
|
23
|
+
"aarch64-linux-musl" => "aarch64-unknown-linux-musl",
|
|
24
|
+
"arm-linux-gnu" => "armv7-unknown-linux-gnueabihf",
|
|
25
|
+
"arm-linux-musl" => "armv7-unknown-linux-musleabihf",
|
|
26
|
+
"arm64-darwin" => "aarch64-apple-darwin",
|
|
27
|
+
"x86_64-darwin" => "x86_64-apple-darwin",
|
|
28
|
+
"x86_64-linux-gnu" => "x86_64-unknown-linux-gnu",
|
|
29
|
+
"x86_64-linux-musl" => "x86_64-unknown-linux-musl",
|
|
30
|
+
"x86-linux-gnu" => "i686-unknown-linux-gnu",
|
|
31
|
+
"x86-linux-musl" => "i686-unknown-linux-musl",
|
|
32
|
+
}.freeze
|
|
33
|
+
|
|
34
|
+
cross_compiling = ENV.key?("RUBY_CC_VERSION")
|
|
35
|
+
target_platform = ENV.fetch("CARGO_BUILD_TARGET", nil)
|
|
36
|
+
|
|
37
|
+
if cross_compiling && target_platform.nil?
|
|
38
|
+
rcd_platform = ENV.fetch("RCD_PLATFORM", "")
|
|
39
|
+
target_platform = RUST_TARGETS[rcd_platform]
|
|
40
|
+
|
|
41
|
+
if target_platform.nil?
|
|
42
|
+
ruby_platform = RbConfig::CONFIG["arch"]
|
|
43
|
+
target_platform = RUST_TARGETS.values.find { |t| ruby_platform.include?(t.split("-").first) }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if target_platform
|
|
48
|
+
puts "yerba: Cross-compiling Rust for target: #{target_platform}"
|
|
49
|
+
system("rustup target add #{target_platform}") || warn("yerba: Failed to add Rust target #{target_platform}")
|
|
50
|
+
|
|
51
|
+
cargo_args = "--release --target #{target_platform}"
|
|
52
|
+
lib_dir = File.join(rust_dir, "target", target_platform, "release")
|
|
53
|
+
else
|
|
54
|
+
puts "yerba: Compiling Rust library for native platform..."
|
|
55
|
+
cargo_args = "--release"
|
|
56
|
+
lib_dir = File.join(rust_dir, "target", "release")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
unless system("cd #{rust_dir} && cargo build #{cargo_args}")
|
|
60
|
+
abort "ERROR: Failed to compile yerba from Rust source."
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
if target_platform
|
|
64
|
+
platform_key = ENV.fetch("RCD_PLATFORM", "")
|
|
65
|
+
else
|
|
66
|
+
platform = Gem::Platform.local
|
|
67
|
+
platform_key = "#{platform.cpu}-#{platform.os}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
exe_directory = File.join(root_dir, "exe", platform_key)
|
|
71
|
+
exe_file = File.join(exe_directory, "yerba")
|
|
72
|
+
source_binary = File.join(lib_dir, "yerba")
|
|
73
|
+
|
|
74
|
+
if File.exist?(source_binary)
|
|
75
|
+
FileUtils.mkdir_p(exe_directory)
|
|
76
|
+
FileUtils.cp(source_binary, exe_file)
|
|
77
|
+
FileUtils.chmod(0o755, exe_file)
|
|
78
|
+
|
|
79
|
+
puts "yerba: CLI binary installed to #{exe_file}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
lib_name = case RbConfig::CONFIG["host_os"]
|
|
83
|
+
when /darwin/ then "libyerba.dylib"
|
|
84
|
+
when /mingw|mswin/ then "yerba.dll"
|
|
85
|
+
else "libyerba.so"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
if target_platform
|
|
89
|
+
lib_name = case target_platform
|
|
90
|
+
when /darwin/ then "libyerba.dylib"
|
|
91
|
+
when /windows|mingw/ then "yerba.dll"
|
|
92
|
+
else "libyerba.so"
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
lib_path = File.join(lib_dir, lib_name)
|
|
97
|
+
|
|
98
|
+
unless File.exist?(lib_path)
|
|
99
|
+
abort "ERROR: Shared library not found at #{lib_path}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
puts "yerba: Shared library found at #{lib_path}"
|
|
103
|
+
|
|
104
|
+
$LDFLAGS << " -L#{lib_dir} -lyerba"
|
|
105
|
+
$CFLAGS << " -I#{File.join(__dir__, "include")}"
|
|
106
|
+
|
|
107
|
+
if RbConfig::CONFIG["host_os"].match?(/darwin|linux/)
|
|
108
|
+
$LDFLAGS << " -Wl,-rpath,#{lib_dir}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
create_makefile("yerba/yerba")
|