configer 0.6.0

A simple configuration management implemented in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# `configer`


A simple configuration management implemented in Rust.

[APIs Documents](https://docs.rs/configer)

[changelog](./CHANGELOG.md)

## 1.`Usage`


Add this to your `Cargo.toml`:

```toml
[dependencies]
configer = "0.6"

# Or

# If necessary

configer = { version = "0.6", features = ["usetoml"] }
```



## 2.`APIs`


**Notes: It's not stable yet and like a toy. Please be careful when using it in a production environment.**

### 2.1.`new`


```rust
let mut configer = ConfigerEnvironment::new();
```



### 2.2.`set`


- Support nested key settings

#### 2.2.1.`Err`


```rust
let mut configer = ConfigerEnvironment::new();

let empty_rvt = configer.set("", "Rust".into());
assert_eq!(empty_rvt, Err(ConfigerError::EmptyKey));
```

#### 2.2.2.`Ok`


```rust
let mut configer = ConfigerEnvironment::new();

configer.set("io.github.photowey.string", String::from("Hello, Configer!").into()).unwrap();
configer.set("io.github.photowey.str", "Rust".into()).unwrap();

let rvt = snowflake_dynamic!().unwrap() as i64;
configer.set("io.github.photowey.i32", 123_i32.into()).unwrap();
configer.set("io.github.photowey.i64", rvt.into()).unwrap();

let pi = PI as f64;
configer.set("io.github.photowey.configer.f32", 9527.8848_f32.into()).unwrap();
configer.set("io.github.photowey.configer.f64", pi.into()).unwrap();

let now = NaiveDateTime::parse_from_str("2024-03-11 22:50:00", DateTimePattern::YYYY_MM_DD_HH_MM_SS).unwrap();
configer.set("io.github.photowey.configer.Time", now.into()).unwrap();
```



#### 2.2.3.`set_t`


- Update signature  `@since 0.4.3`

```rust
let mut configer = ConfigerEnvironment::new();
// set
// configer.set("io.github.photowey.string", String::from("Hello, Configer!").into()).unwrap();
// set_t
configer.set_t("io.github.photowey.string", String::from("Hello, Configer!")).unwrap();

// set
// configer.set("io.github.photowey.str", "Rust".into()).unwrap();
// set_t
configer.set_t("io.github.photowey.str", "Rust").unwrap();

let rvt = snowflake_dynamic!().unwrap() as i64;
configer.set_t("io.github.photowey.i32", 123_i32).unwrap();
configer.set_t("io.github.photowey.i64", rvt).unwrap();

let pi = PI as f64;
configer.set_t("io.github.photowey.configer.f32", 9527.8848_f32).unwrap();
configer.set_t("io.github.photowey.configer.f64", pi).unwrap();

let now = NaiveDateTime::parse_from_str("2024-03-11 22:50:00", DateTimePattern::YYYY_MM_DD_HH_MM_SS).unwrap();
configer.set_t("io.github.photowey.configer.Time", now).unwrap();
```



### 2.3.`get`


#### 2.3.1.`Err`


```rust
let mut configer = ConfigerEnvironment::new();
configer.set("io.github.photowey.configer.ok", "Rust".into()).unwrap();

assert_eq!(configer.get("io.github.photowey.configer.not.found"), Err(ConfigerError::NotFound));
```

#### 2.3.2.`Ok`


```rust
let mut configer = ConfigerEnvironment::new();

configer.set("io.github.photowey.string", String::from("Hello, Configer!").into()).unwrap();
configer.set("io.github.photowey.str", "Rust".into()).unwrap();

let rvt = snowflake_dynamic!().unwrap() as i64;
configer.set("io.github.photowey.i32", 123_i32.into()).unwrap();
configer.set("io.github.photowey.i64", rvt.into()).unwrap();

let pi = PI as f64;
configer.set("io.github.photowey.configer.f32", 9527.8848_f32.into()).unwrap();
configer.set("io.github.photowey.configer.f64", pi.into()).unwrap();

let now = NaiveDateTime::parse_from_str("2024-03-11 22:50:00", DateTimePattern::YYYY_MM_DD_HH_MM_SS).unwrap();
configer.set("io.github.photowey.configer.Time", now.into()).unwrap();

assert_eq!(configer.get("io.github.photowey.string"), Ok(&Node::String(String::from("Hello, Configer!").into())));
assert_eq!(configer.get("io.github.photowey.str"), Ok(&Node::String(String::from("Rust").into())));
assert_eq!(configer.get("io.github.photowey.i32"), Ok(&Node::Int32(123_i32)));
assert_eq!(configer.get("io.github.photowey.i64"), Ok(&Node::Int64(rvt)));
assert_eq!(configer.get("io.github.photowey.configer.f32"), Ok(&Node::Float32(9527.8848_f32)));
assert_eq!(configer.get("io.github.photowey.configer.f64"), Ok(&Node::Float64(pi)));
assert_eq!(configer.get("io.github.photowey.configer.Time"), Ok(&Node::DateTime(now)));
```

#### 2.3.3.`Convert`


- Convert `&Node`

##### 2.3.3.1.`Nested`


```rust
let mut configer = ConfigerEnvironment::new();

let mut nested = Table::new();
nested.insert("Hello".to_string(), Node::String("Rust".to_string()));

configer
.set("io.github.photowey.nested", Node::Nested(nested))
.unwrap();

let rvt_nested = configer.get("io.github.photowey.nested");

if let Some(into_value) = NodeConverter::try_nested(rvt_nested) {
	match into_value.get("Hello") {
        Some(node) => {
            assert_eq ! ( * node, Node::String("Rust".to_string()));
        },
        _ => {}
    }
} else {
    panic ! ("failed to convert the value to Table")
}
```

##### 2.3.3.2.`Array`


```rust
let mut configer = ConfigerEnvironment::new();
let now = 1710265983u32;
let mut array = domain::Array::new();
array.push(Node::String("Rust".to_string()));
array.push(Node::IntU32(now));

configer
.set("io.github.photowey.array", Node::Array(array))
.unwrap();

let rvt_array = configer.get("io.github.photowey.array");

let mut image = domain::Array::new();
image.push(Node::String("Rust".to_string()));
image.push(Node::IntU32(now));

if let Some(into_value) = NodeConverter::try_array(rvt_array) {
    assert!(assert_array_equals(into_value, &image));
} else {
    panic!("failed to convert the value to Table")
}
```

##### 2.3.3.3.`DateTime`


```rust
let mut configer = ConfigerEnvironment::new();

let now = NaiveDateTime::parse_from_str("2024-03-11 22:50:00", DateTimePattern::YYYY_MM_DD_HH_MM_SS).unwrap();
configer.set("io.github.photowey.configer.Time", now.into()).unwrap();

let rvt_time = configer.get("io.github.photowey.configer.Time");

// match
match rvt_time {
    Ok(node) => {
        match node {
            Node::DateTime(ref time) => {
                assert_eq!(*time, now);
            }
            _ => {}
        }
    }
    _ => {}
}

// converter
if let Some(into_value) = NodeConverter::try_datetime(rvt_time) {
    assert_eq!(*into_value, now);
} else {
    panic!("failed to convert the value to NaiveDateTime")
}
```

##### 2.3.4.4.`...`




## 3.`Reader`


- `@since 0.3.0`

### 3.1.`toml`


#### 3.1.1.`new`


```rust
let toml_reader = TomlConfigReader::default();
```



#### 3.1.2.`Read`


##### 3.1.2.1.`path`


```rust
let path = "resources/testdata/configer-dev.toml";
let toml_reader = TomlConfigReader::default();
let toml_rvt = toml_reader.read_from_path(path);
```



##### 3.1.2.2.`file content`


```rust
let toml_reader = TomlConfigReader::default();

let path = "resources/testdata/configer-dev.toml";
let content = fs::read_to_string(path).expect("Failed to read config file");
let toml_from_content_rvt = toml_reader.read_from_str(&content);
```



## 4.`ConfigerEnvironmentBuilder`


- `@since 0.4.0`

### 4.1.`With table`


```rust
let path = "resources/testdata/configer-dev.toml";

let toml_reader = TomlConfigReader::default();
let toml_rvt = toml_reader.read_from_path(path);

if let Ok(table) = toml_rvt {
    // With table
    let builder_rvt = ConfigerEnvironment::builder()
    .with_table(table)
    .build();

    if let Ok(configer) = builder_rvt {
        let rvt_database_servers = configer.get("database.servers");

        return assert_configer_array(rvt_database_servers, "database.servers");
    }

    panic!("Failed to build ConfigerEnvironment")
}

panic!("Failed to read configer-dev.toml file")
```

### 4.2.`With registry and path`


- Only support `toml` file now.

```rust
let path = "resources/testdata/configer-dev.toml";

let toml_reader = TomlConfigReader::default();
let mut registry = ConfigReaderRegistry::default();
registry.register(Box::new(toml_reader));

let builder_rvt = ConfigerEnvironment::builder()
.with_registry(Box::new(registry))
.with_path(path.to_string())
.build();

if let Ok(configer) = builder_rvt {
    let rvt_database_servers = configer.get("database.servers");
    return assert_configer_array(rvt_database_servers, "database.servers");
}

panic!("Failed to read configer-dev.toml file")
```



### 4.3.`With table,registry and path`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let path = "resources/testdata/configer-dev.toml";

let toml_reader = TomlConfigReader::default();
let mut registry = ConfigReaderRegistry::default();
registry.register(Box::new(toml_reader));

let table = crate::env::try_load_env_variables();

let builder_rvt = ConfigerEnvironment::builder()
.with_table(table)
.with_registry(Box::new(registry))
.with_path(path.to_string())
.build();

if let Ok(configer) = builder_rvt {
    let rvt_database_servers = configer.get("database.servers");
    assert_configer_array(rvt_database_servers, "database.servers");

    let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
    assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));

    return ();
}

panic!("Failed to read configer-dev.toml file")
```



### 4.4.`With table,registry,path and profiles`


- `@since 0.6.0`

```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let path = "resources/testdata/config.toml";

let toml_reader = TomlConfigReader::default();
let mut registry = ConfigReaderRegistry::default();
registry.register(Box::new(toml_reader));

let builder_rvt = ConfigerEnvironment::builder()
.with_registry(Box::new(registry))
.with_path(path.to_string())
// with profiles
.with_profiles(vec![String::from("dev"), String::from("shared")])
.build();

if let Ok(configer) = builder_rvt {
    // config-dev.toml
    let rvt_database_servers = configer.get("database.servers");
    assert_configer_array(rvt_database_servers, "database.servers");

    let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
    assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));

    // config-shared.toml
    let config_shared_rvt = configer.get("strings");
    assert_configer_array_strings(config_shared_rvt, "strings");

    // config.toml
    let config_bool_rvt = configer.get("boolean_value");
    assert_eq!(config_bool_rvt, Ok(&Node::Boolean(true)));

    return ();
}

panic!("Failed to read configer-[dev, shared].toml file")
```





## 5.`Load Environment variables`


### 5.1.`default`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let configer = ConfigerEnvironment::default();
let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));
```

### 5.2.`new`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let configer = ConfigerEnvironment::new();
let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));
```

### 5.3.`mixed_with_env_variables`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let configer = ConfigerEnvironment::mixed_with_env_variables(None, None);
let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));
```

### 5.4.`table`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let path = "resources/testdata/configer-dev.toml";

let toml_reader = TomlConfigReader::default();
let toml_rvt = toml_reader.read_from_path(path);

if let Ok(table) = toml_rvt {
    let configer = ConfigerEnvironment::table(table);

    let rvt_database_servers = configer.get("database.servers");
    assert_configer_array(rvt_database_servers, "database.servers");

    let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
    assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));

    return ();
}

panic!("Failed to read configer-dev.toml file")
```

### 5.5.`builder`


```rust
env::set_var("CONFIGER_TEST_VAR", "rust.configer");

let path = "resources/testdata/configer-dev.toml";

let toml_reader = TomlConfigReader::default();
let mut registry = ConfigReaderRegistry::default();
registry.register(Box::new(toml_reader));

let builder_rvt = ConfigerEnvironment::builder()
.with_registry(Box::new(registry))
.with_path(path.to_string())
.build(); // load environment variables auto.

if let Ok(configer) = builder_rvt {
    let rvt_database_servers = configer.get("database.servers");
    assert_configer_array(rvt_database_servers, "database.servers");

    let env_var_rvt = configer.get("CONFIGER_TEST_VAR");
    assert_eq!(env_var_rvt, Ok(&Node::String(String::from("rust.configer"))));

    return ();
}

panic!("Failed to read configer-dev.toml file")
```





## 6.`Next`


- Support load `config` files (P 0).
    - [x] `configer.toml`
      - [x] `@since 0.3.0`
    - [x] `configer-${profile}.toml`
      - [x] `@since 0.6.0`
      - [x] `ConfigerEnvironmentBuilder`
        - [x] `with_profiles`
    -    - [ ] `yaml` | `yml` ?
    - [ ] `properties` ?
    - [ ] `ini`?
    - [ ] `.env`?
    - [ ] `json`?
    -- [x] Auto. load environment variables (P 1)
- [x] Support merge exists `HashMap<String,Node>/Table`
- Support bind `struct`
-


## 7.`Documents`


**Please wait a moment.**



## 8.`Test`


### 8.1.`cargo test`


```shell
$ cargo test --features "usetoml" -- --show-output
$ cargo test --features "usetoml"
```



## 9.`Docs`


### 9.1.`features`


- `usetoml`

```shell
$ cargo doc --open --features usetoml
```