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 544 545 546 547 548 549
//! # Uclicious [![Build Status](https://dev.azure.com/andoriyu/personal/_apis/build/status/andoriyu.uclicious?branchName=master)](https://dev.azure.com/andoriyu/personal/_build/latest?definitionId=7&branchName=master) [![codecov](https://codecov.io/gh/andoriyu/uclicious/branch/master/graph/badge.svg)](https://codecov.io/gh/andoriyu/uclicious) [![docs.rs](https://docs.rs/uclicious/badge.svg)](https://docs.rs/uclicious) [![Crates.io](https://img.shields.io/crates/v/uclicious.svg)](https://crates.io/crates/uclicious)
//!
//! * [What is Uclicious](#what-is-uclicious)
//! * [Usage](#usage)
//! + [Raw API](#raw-api)
//! + [Derive-driven](#derive-driven)
//! - [Validators](#validators)
//! - [Type Mapping](#type-mapping)
//! + [Supported attributes (`#[ucl(..)]`)](#supported-attributes-%23ucl)
//! - [Structure level](#structure-level)
//! - [Field level](#field-level)
//! + [Additional notes](#additional-notes)
//! * [Contributing](#contributing)
//! - [Particular Contributions of Interest](#particular-contributions-of-interest)
//! * [Goals](#goals)
//! + [Not Goals](#not-goals)
//! * [Special thanks](#special-thanks)
//! * [LICENSE](#license)
//! ## What is Uclicious
//!
//! Uclicious is a flexible reduced boilerplate configuration framework.
//!
//! Uclicious is built on top of [libucl](https://github.com/vstakhov/libucl). If you ever wrote an nginx configurtion and though "Damn, I wish all configuration files were like this" this is the library for you. Internal parser supports both: nginx-like and json-like formats. [JSON parser is a little bit more permissive than "proper" json parser](https://github.com/vstakhov/libucl#improvements-to-the-json-notation) - every json file is a valid UCL file, but not other way around.
//! It is much more complex than json or TOML, so I recommend reading documentaiton about it. Author of UCL did a great job documenting it. This library provides both: derive-driven and raw-api driven usage patterns.
//!
//! ## Usage
//! ### Raw API
//!
//! Raw API involves interacting with `libucl` parser via safe api:
//! ```rust
//! use uclicious::*;
//! let mut parser = Parser::default();
//! let input = r#"
//! test_string = "no scope"
//! a_float = 3.14
//! an_integer = 69420
//! is_it_good = yes
//! buffer_size = 1KB
//! interval = 1s
//! "#;
//! parser.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let result = parser.get_object().unwrap();
//!
//! let lookup_result = result.lookup("test_string").unwrap().as_string().unwrap();
//! assert_eq!(lookup_result.as_str(), "no scope");
//!
//! let lookup_result = result.lookup("a_float").unwrap().as_f64().unwrap();
//! assert_eq!(lookup_result, 3.14f64);
//!
//! let lookup_result = result.lookup("an_integer").unwrap().as_i64().unwrap();
//! assert_eq!(lookup_result, 69420i64);
//!
//! let lookup_result = result.lookup("is_it_good").unwrap().as_bool().unwrap();
//! assert_eq!(lookup_result, true);
//!
//! let lookup_result = result.lookup("buffer_size").unwrap().as_i64().unwrap();
//! assert_eq!(lookup_result, 1024);
//! let lookup_result = result.lookup("interval").unwrap().as_time().unwrap();
//! assert_eq!(lookup_result, 1.0f64);
//! ```
//!
//! In order to get around rust rules library implemets its own trait FromObject for some basic types:
//! ```rust
//! use uclicious::*;
//! let mut parser = Parser::default();
//! let input = r#"
//! test_string = "no scope"
//! a_float = 3.14
//! an_integer = 69420
//! is_it_good = yes
//! buffer_size = 1KB
//! "#;
//! parser.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let result = parser.get_object().unwrap();
//!
//! let lookup_result = result.lookup("is_it_good").unwrap();
//! let maybe: Option<bool> = FromObject::try_from(lookup_result).unwrap();
//! assert_eq!(Some(true), maybe);
//! ```
//! ### Derive-driven
//!
//! On top of "raw" interface to libUCL, Uclicious provides an easy way to derive constructor for strucs:
//! ```rust
//! use uclicious::*;
//! use std::path::PathBuf;
//! use std::net::SocketAddr;
//! use std::collections::HashMap;
//! use std::time::Duration;
//!
//! #[derive(Debug,Uclicious)]
//! #[ucl(var(name = "test", value = "works"))]
//! struct Connection {
//! #[ucl(default)]
//! enabled: bool,
//! host: String,
//! #[ucl(default = "420")]
//! port: i64,
//! buffer: u64,
//! #[ucl(path = "type")]
//! kind: String,
//! locations: Vec<PathBuf>,
//! addr: SocketAddr,
//! extra: Extra,
//! #[ucl(path = "subsection.host")]
//! hosts: Vec<String>,
//! #[ucl(default)]
//! option: Option<String>,
//! gates: HashMap<String, bool>,
//! interval: Duration,
//! }
//!
//! #[derive(Debug,Uclicious)]
//! #[ucl(skip_builder)]
//! struct Extra {
//! enabled: bool
//! }
//! let mut builder = Connection::builder().unwrap();
//!
//! let input = r#"
//! enabled = yes
//! host = "some.fake.url"
//! buffer = 1mb
//! type = $test
//! locations = "/etc/"
//! addr = "127.0.0.1:80"
//! extra = {
//! enabled = on
//! }
//! subsection {
//! host = [host1, host2]
//! }
//! interval = 10ms
//! gates {
//! feature_1 = on
//! feature_2 = off
//! feature_3 = on
//! }"#;
//!
//! builder.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let connection: Connection = builder.build().unwrap();
//! ```
//!
//! If you choose to derive builder then `::builder()` method will be added to target struct.
//!
//! #### Validators
//!
//! Library supports running optional validators on values before building the resulting struct:
//!
//! ```rust
//! use uclicious::*;
//! mod validators {
//! use uclicious::ObjectError;
//! pub fn is_positive(lookup_path: &str, value: &i64) -> Result<(), ObjectError> {
//! if *value > 0 {
//! Ok(())
//! } else {
//! Err(ObjectError::other(format!("{} is not a positive number", lookup_path)))
//! }
//! }
//! }
//! #[derive(Debug,Uclicious)]
//! struct Validated {
//! #[ucl(default, validate="validators::is_positive")]
//! number: i64
//! }
//! let mut builder = Validated::builder().unwrap();
//!
//! let input = "number = -1";
//! builder.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! assert!(builder.build().is_err())
//! ```
//! #### Type Mapping
//!
//! If your target structure has types that don't implement `FromObject` you can use `From` or `TryFrom`
//! via intermediate that does:
//!
//! ```rust
//! use uclicious::*;
//! use std::convert::{From,TryFrom};
//!
//! #[derive(Debug, Eq, PartialEq)]
//! enum Mode {
//! On,
//! Off,
//! }
//!
//! impl TryFrom<String> for Mode {
//! type Error = ObjectError;
//! fn try_from(src: String) -> Result<Mode, ObjectError> {
//! match src.to_lowercase().as_str() {
//! "on" => Ok(Mode::On),
//! "off" => Ok(Mode::Off),
//! _ => Err(ObjectError::other(format!("{} is not supported value", src)))
//! }
//! }
//! }
//!
//! #[derive(Debug, Eq, PartialEq)]
//! struct WrappedInt(i64);
//!
//! impl From<i64> for WrappedInt {
//! fn from(src: i64) -> WrappedInt {
//! WrappedInt(src)
//! }
//! }
//!
//! #[derive(Debug,Uclicious, Eq, PartialEq)]
//! struct Mapped {
//! #[ucl(from="i64")]
//! number: WrappedInt,
//! #[ucl(try_from="String")]
//! mode: Mode
//! }
//! let mut builder = Mapped::builder().unwrap();
//!
//! let input = r#"
//! number = -1,
//! mode = "on"
//! "#;
//! builder.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let actual = builder.build().unwrap();
//! let expected = Mapped {
//! number: WrappedInt(-1),
//! mode: Mode::On
//! };
//! assert_eq!(expected, actual);
//! ```
//!
//! Additionally you can provide mapping to your type from ObjectRef:
//! ```rust
//! use uclicious::*;
//!
//! #[derive(Debug, Eq, PartialEq)]
//! pub enum Mode {
//! On,
//! Off,
//! }
//!
//! pub fn map_bool(src: ObjectRef) -> Result<Mode, ObjectError> {
//! let bool: bool = src.try_into()?;
//! if bool {
//! Ok(Mode::On)
//! } else {
//! Ok(Mode::Off)
//! }
//! }
//! #[derive(Debug,Uclicious, Eq, PartialEq)]
//! struct Mapped {
//! #[ucl(map="map_bool")]
//! mode: Mode
//! }
//! let mut builder = Mapped::builder().unwrap();
//!
//! let input = r#"
//! mode = on
//! "#;
//! builder.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY).unwrap();
//! let actual = builder.build().unwrap();
//! let expected = Mapped {
//! mode: Mode::On
//! };
//! ```
//! ### Supported attributes (`#[ucl(..)]`)
//!
//! #### Structure level
//!
//! - `skip_builder`
//! - if set, then builder and builder methods won't be generated.
//! - `parser(..)`
//! - Optional attribute to configure inner parser.
//! - Has following nested attributes:
//! - `flags`
//! - a path to function that returns flags.
//! - `filevars(..)`
//! - call `set_filevars` on a parser.
//! - Has following nested attributes:
//! - `path`
//! - a string representation of filepath.
//! - `expand`
//! - (optional) if set, then variables would be expanded to absolute.
//! - `pre_source_hook(...)`
//! - Optional attribute to run a function before sources are added
//! - Can be used to register vars handler
//! - Must take `&mut Parser` as argument and return `Result<(), Into<UclError>>`
//! - `var(..)`
//! - Optional attribute to register string variables with the parser.
//! - Has following nested attributes:
//! - `name`
//! - A name of the variable without `$` part.
//! - `value`
//! - A string values for the variable.
//! - Onlt string variables are supported by libUCL.
//! - `include(..)`
//! - Used to add files into the parser.
//! - If file doesn't exist or failed to parse, then error will be returned in a constructor.
//! - Must specify exactly one of following sources: `path`, `chunk` or `chunk_static`
//! - Has following nested attirbutes:
//! - (semi-optional) `path = string`
//! - File path. Can be absolute or relative to CWD.
//! - (semi-optional) `chunk = string`
//! - A string that will be added to parser as a chunk.
//! - (semi-optional) `chunk_static = string`
//! - A path to a file that will be included into binary with [`include_str!()`](https://doc.rust-lang.org/std/macro.include_str.html)
//! - (optional) `priority = u32`
//! - 0-15 priority for the source. Consult the libUCL documentation for more information.
//! - (optional) `strategy = uclicious::DuplicateStrategy`
//! - Strategy to use for duplicate keys. Consult the libUCL documentation for more information.
//!
//! #### Field level
//! All field level options are optional.
//!
//! - `default`
//! - Use Default::default if key not found in object.
//! - `default = expression`
//! - Use this _expression_ as value if key not found.
//! - Could be a value or a function call.
//! - `path = string`
//! - By default field name is used as path.
//! - If set that would be used as a key.
//! - dot notation for key is supported.
//! - `validate = path::to_method`
//! - `Fn(key: &str, value: &T) -> Result<(), E>`
//! - Error needs to be convertable into `ObjectError`
//! - `from = Type`
//! - Try to convert `ObjectRef` to `Type` and then use `std::convert::From` to convert into target type
//! - `try_from = Type`
//! - Try to convert `ObjectRef` to `Type` and then use `std::convert::TryFrom` to convert into target type
//! - Error will be converted into `ObjectError::Other`
//! - `map = path::to_method`
//! - `Fn(src: ObjectRef) -> Result<T, E>`
//! - A way to map foreign objects that can't implement `From` or `TryFrom` or when error is not convertable into `ObjectError`
//!
//! ### Additional notes
//! - If target type is an array, but key is a single value — an implicit list is created.
//! - Automatic derive on enums is not supported, but you can implement it yourself.
//! - I have a few more features I want to implement before publishing this crate:
//! - Ability to add variables.
//! - Ability to add macross handlers.
//! - (maybe) configure parser that us used for derived builder with atrributes.
//! - (done) add sources to parser with attributes.
//!
//! ## Contributing
//!
//! PRs, feature requests, bug reports are welcome. I won't be adding CoC — be civilized.
//!
//! #### Particular Contributions of Interest
//!
//! - Optimize derive code.
//! - Improve documentation — I often write late and night and some it might look like a word soup.
//! - Better tests
//! - Glob support in derive parser section
//! - Variable handler
//!
//!
//! ## Goals
//! - Provider safe and convient configuration library
//! - Automatic derive, so you don't have to think about parser object
//!
//! ### Not Goals
//! - Providing UCL Object generation tools is not a goal for this project
//! - 1:1 interface to libUCL
//! - sugar inside `raw` module
//!
//! ## Special thanks
//! - [draft6](https://github.com/draft6) and [hauleth](https://github.com/hauleth)
//! - libucl-rs was a good starting point
//! - Type wrappers pretty much copied from there
//! - [colin-kiegel](https://github.com/colin-kiegel)
//! - Rust-derive-builder was used as a starting point for uclicious-derive
//! - Very well documented proc_macro crate, do recommend
//!
//! ## LICENSE
//!
//! [BSD-2-Clause](https://github.com/andoriyu/uclicious/blob/master/LICENSE).
pub mod error;
pub mod raw;
pub mod traits;
pub mod variable_handlers;
pub use error::{UclError, UclErrorType};
pub use raw::{
DuplicateStrategy, Object, ObjectError, ObjectRef, Parser, ParserFlags, Priority,
DEFAULT_DUPLICATE_STRATEGY, DEFAULT_PARSER_FLAG,
};
pub use traits::{FromObject, TryInto};
#[cfg(feature = "uclicious_derive")]
#[allow(unused_imports)]
#[macro_use]
extern crate uclicious_derive;
#[cfg(feature = "uclicious_derive")]
#[doc(hidden)]
pub use uclicious_derive::*;
#[cfg(test)]
mod test {
use super::*;
use std::collections::HashMap;
#[test]
fn primitives_from_object() {
let input = r#"
i64 = 1
i32 = 1
i16 = 1
i8 = 1
u64 = 1
u32 = 1
u16 = 1
u8 = 1
f64 = 3.14
bool = true
"#;
let mut parser = Parser::default();
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let boolean: bool = FromObject::try_from(root.lookup("bool").unwrap()).unwrap();
assert_eq!(true, boolean);
let float64: f64 = FromObject::try_from(root.lookup("f64").unwrap()).unwrap();
assert_eq!(3.14f64, float64);
let int64: i64 = FromObject::try_from(root.lookup("i64").unwrap()).unwrap();
assert_eq!(1, int64);
let int32: i32 = FromObject::try_from(root.lookup("i32").unwrap()).unwrap();
assert_eq!(1, int32);
let int16: i16 = FromObject::try_from(root.lookup("i16").unwrap()).unwrap();
assert_eq!(1, int16);
let int8: i8 = FromObject::try_from(root.lookup("i8").unwrap()).unwrap();
assert_eq!(1, int8);
let uint64: u64 = FromObject::try_from(root.lookup("u64").unwrap()).unwrap();
assert_eq!(1, uint64);
let uint32: u32 = FromObject::try_from(root.lookup("u32").unwrap()).unwrap();
assert_eq!(1, uint32);
let uint16: u16 = FromObject::try_from(root.lookup("u16").unwrap()).unwrap();
assert_eq!(1, uint16);
let uint8: u8 = FromObject::try_from(root.lookup("u8").unwrap()).unwrap();
assert_eq!(1, uint8);
}
#[test]
fn string_from_object() {
let input = r#"
test_string = "a string"
"#;
let mut parser = Parser::default();
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let expected = String::from("a string");
let actual: String = FromObject::try_from(root.lookup("test_string").unwrap()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn array_from_object() {
let input = r#"
list = [1,2,3,4]
implicit_list = 1
"#;
let mut parser = Parser::default();
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let expected = vec![1, 2, 3, 4];
let actual: Vec<i32> = FromObject::try_from(root.lookup("list").unwrap()).unwrap();
assert_eq!(expected, actual);
let expected = vec![1];
let actual: Vec<i32> = FromObject::try_from(root.lookup("implicit_list").unwrap()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn array_from_object_error() {
let input = r#"
list = [true,true,true,true]
"#;
let mut parser = Parser::default();
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let actual: Result<Vec<i32>, ObjectError> =
FromObject::try_from(root.lookup("list").unwrap());
assert!(actual.is_err());
}
#[test]
fn hashmap_from_object() {
let input = r#"
dict {
key = value
}
"#;
let mut parser = Parser::default();
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let mut expected = HashMap::new();
expected.insert(String::from("key"), String::from("value"));
let actual: HashMap<String, String> =
FromObject::try_from(root.lookup("dict").unwrap()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn register_var() {
let input = r#"
dst = $dst
"#;
let mut parser = Parser::default();
parser
.register_variable("dst", "/etc/")
.register_variable("int", "1");
parser
.add_chunk_full(input, Priority::default(), DEFAULT_DUPLICATE_STRATEGY)
.unwrap();
let root = parser.get_object().unwrap();
let expected = "/etc/".to_string();
let actual = root.lookup("dst").unwrap().as_string().unwrap();
assert_eq!(expected, actual);
}
}