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
//! # One-line template
//! One-line template is library for serialization structures in one-line string. 
//! This library is helpful for path formatting.
//!
//! ## Template format
//! 
//! * Path navigation: `{self}`, `{self.field_1.field_2}`, `{self.0}`
//! * Value processing: `{self | uint:hex() | string:substr(0uint, 2uint)|string:unwrap_or('--')}`
//! 
//! ## Example
//!
//!```rust
//! use oneline_template::template::TemplateBuilder;
//! 
//! #[derive(serde_derive::Serialize)]
//! struct FileId {
//!   file_id: u32,
//! }
//!
//! fn main() {
//!   let template_builder = TemplateBuilder::new();
//!   let template = template_builder.build("directory/file_no_{ self.file_id }.json").expect("Unable to create template");
//!   
//!   let file_id = FileId {file_id: 1};
//!   let path = template.serialize(&file_id).expect("Unable to format path");
//!   assert_eq!("directory/file_no_1.json", &path);
//! }
//!```
//! ## Custom function
//!
//! You can implement your own processing function. Contribution in this project is welcomed!
//!
//! ```rust
//!
//! use oneline_template::function_executor::*;
//! use oneline_template::template::TemplateBuilder;
//!
//! /// Function: `uint::neg`
//! /// 
//! /// Input: `uint`
//! ///
//! /// Returns `uint`
//! pub struct Neg;
//!
//! impl FunctionExecutor for Neg {
//!    fn schema(&self) -> FunctionSchema {
//!        FunctionSchema::new("uint:neg")
//!    }
//!
//!    fn call(&self, value: Value, _arguments: &[Value]) -> Result<Value, FunctionError> {
//!        let value = value.as_uint()?;
//!        let value = !value;
//!        let value = Value::UInt(value);
//!        return Ok(value);
//!    }
//! }
//!
//! #[derive(serde_derive::Serialize)]
//! struct FileId {
//!   file_id: u32,
//! }
//!
//! fn main() {
//!   let template_builder = TemplateBuilder::new();
//!   let template_builder = template_builder.with_function(Neg);
//!   let template = template_builder.build("directory/file_no_{ self.file_id | uint:neg( ) | uint:hex( ) }.json").expect("Unable to create template");
//!   
//!   let file_id = FileId {file_id: 1};
//!   let path = template.serialize(&file_id).expect("Unable to format path");
//!   assert_eq!("directory/file_no_fffffffffffffffffffffffffffffffe.json", &path);
//! }
//! ```

/// Template engine.
pub mod template;
/// Traits and structures for function execution.
pub mod function_executor;
/// Library of functions.
pub mod functions;