gearbox
Gearbox is a versatile library that encompasses a wide array of functionalities, including networking, logging, railway-oriented programming extensions, and time management. Initially designed as a collection of utilities, the ultimate vision for Gearbox is to evolve into a highly optimized, standalone toolkit. The goal is to minimize external dependencies progressively, leading to a library that is lightweight and efficient. By doing so, Gearbox aims to be universally compatible, from embedded systems to WebAssembly (WASM) environments, all while maintaining simplicity and minimizing the need for boilerplate code. This development strategy positions Gearbox as a comprehensive solution for developers seeking to build efficient, scalable applications across a broad spectrum of platforms.
Features
| Category | Feature | use | Description | Status |
|---|---|---|---|---|
| Common | TryDefault | gearbox::common::TryDefault | This is a trait used internally in Gearbox defining a TryDefault trait that returns a Result<T,Self::Error>. It can also be used in other systems. |
✅ |
| BoxedFuture | gearbox::common::BoxedFuture | Type alias for a pinned boxed future. Used for returning dynamically dispatched futures. | ✅ | |
| Error | ErrorTracer | gearbox::error::tracer::* | An error structure that builds a traceable stack of errors. It allows breaking down the error into a TypeId to define the encapsulated error for further operations. Contains information about file, line, module path, and optional error_code with display and debug. This also comes with the macro Error!() which sets up the ErrorTracerExtInfo with all the needed information (file, line, module). |
⚠️ |
| Rail ErrorTracer | gearbox::rails::ext::map_err_tracer | Simplification for map_err for operating with the ErrorTracer, allowing for passing an Error!() or an ErrorTracerExtInfo for collecting all the information. |
✅ | |
| Logging | Tracing Log Formatter | gearbox::log::fmt::* | Custom subscriber for formatting logs when using the rust Tracing library. | ⚠️ |
| Networking | hostname | gearbox::net::hostname | Get the hostname of the local machine. | ✅ |
| HTTP Request | gearbox::net::http::request | Send an HTTP request. This is an extension on top of Reqwest that simplifies the implementation of mTLS and payload signing. |
⚠️ | |
| HTTP Request Chaining | gearbox::net::http::request_chain | Chaining system for HTTP requests, allowing for chaining requests and responses for more advanced request/response handling. | ❌ | |
| Paths | Common Paths | gearbox::path::* | Common paths under Windows, Linux, and more. For example, under Linux, the config path is usually ~/.config/. |
✅ |
| Rails | Common Extensions | gearbox::rails::ext::* | Various extension traits for operating on Result, Option, and other standard types, providing additional methods for error handling, merging results, and tapping into values. |
⚠️ |
| Future Extensions | gearbox::rails::ext::future::* | Extensions for working with Future types, providing methods for mapping, chaining, and merging futures. |
✅ | |
| serde | Dynamic Serialization | gearbox::serde::dynamic::* | Dynamic serialization system that allows for encoding and decoding of multiple formats. This is a simplified version of the serde library. | ⚠️ |
| Wasm Bindgen Ser/de | gearbox::serde::wasm_bindgen::* | Implementation for WASM Bind Generator that allows for serialization/deserialization of JsValue. | ✅ | |
| Storage | Web Storage | gearbox::storage::web::local_storage::* | Interface for interacting with local storage in a web environment, including features for setting, getting, and deleting data with JSON serialization/deserialization support. | 🚧 |
| File Storage | gearbox::storage::io::file::* | Interface for interacting with file storage, including features for setting, getting, and deleting data with JSON and YAML serialization/deserialization support. | 🧪 | |
| Selective Storage | gearbox::storage::selective_storage | Trait for selective storage operations, providing methods for creating, setting, getting, and deleting storage entries. | 🚧 | |
| Time | Time Stamps and more | gearbox::time::* | Timestamp system similar to Chrono, handling times and time calculations. Used throughout Gearbox instead of Chrono. | ⚠️ |
| Template | Template Engine | gearbox::template::* | Template engine responsible for rendering templates using context data and applying pipelines for data transformations. It supports pipelines for operations like date formatting and string prefixing. | ⚠️ |
Status Icons Explanation
- ✅ Completed: The feature is fully implemented and tested.
- ❌ Not Completed: The feature is not implemented.
- ⚠️ Partially: The feature is partially implemented.
- 🚧 In Development: The feature is currently being developed.
- 🧪 Missing Testing: The feature is implemented but lacks testing.
Test Status
| File | Coverage Bar | Line Coverage | Lines Covered | Lines Total |
|---|---|---|---|---|
| src/collections/const_hash_map | 0.0% | 0 | 240 | |
| src/collections/hash_map | 25.76% | 51 | 198 | |
| src/collections/simple_linked_list | 100.0% | 138 | 138 | |
| src/collections/vec_deque | 95.79% | 182 | 190 | |
| src/common | 42.22% | 19 | 45 | |
| src/error | 33.33% | 12 | 36 | |
| src/error/tracer | 30.55% | 150 | 491 | |
| src/log/tracing | 37.69% | 228 | 605 | |
| src/log/tracing/entity/deeplog | 0.0% | 0 | 537 | |
| src/log/tracing/entity/syslog | 40.86% | 190 | 465 | |
| src/log/tracing/formatter/bunyan | 0.0% | 0 | 178 | |
| src/log/tracing/formatter/deeplog | 0.0% | 0 | 148 | |
| src/log/tracing/formatter/syslog | 0.0% | 0 | 129 | |
| src/log/tracing/layer | 0.0% | 0 | 188 | |
| src/log/tracing/macros | 38.46% | 5 | 13 | |
| src/net | 65.96% | 31 | 47 | |
| src/net/http/request | 43.78% | 401 | 916 | |
| src/net/http/request/header | 45.59% | 119 | 261 | |
| src/net/http/request_chaining | 88.85% | 526 | 592 | |
| src/net/http/test | 77.59% | 45 | 58 | |
| src/net/signature | 84.78% | 323 | 381 | |
| src/rails/ext/blocking | 29.98% | 155 | 517 | |
| src/rails/ext/future | 98.48% | 649 | 659 | |
| src/rails/ext/future/ext/option | 86.65% | 305 | 352 | |
| src/rails/ext/future/ext/result | 82.57% | 270 | 327 | |
| src/rails/tracing | 100.0% | 113 | 113 | |
| src/serde/dynamic | 75.0% | 246 | 328 | |
| src/serde/dynamic/test | 14.71% | 5 | 34 | |
| src/service/discovery/entity | 0.0% | 0 | 34 | |
| src/service/discovery/services | 0.0% | 0 | 160 | |
| src/service/framework/axum | 0.0% | 0 | 197 | |
| src/storage | 0.0% | 0 | 39 | |
| src/storage/io/file | 57.39% | 163 | 284 | |
| src/sync | 100.0% | 6 | 6 | |
| src/sync/rw_arc | 74.3% | 506 | 681 | |
| src/task | 96.74% | 297 | 307 | |
| src/task/multicommand | 62.05% | 103 | 166 | |
| src/template | 83.04% | 240 | 289 | |
| src/template/pipelines | 80.3% | 53 | 66 | |
| src/time | 47.51% | 1029 | 2166 |
Http Request (gearbox::net::http::request)
Complete architectural overview:
classDiagram
%% Package: request
namespace request {
class Client {
+client: reqwest::Client
+new()
+with_client(reqwest::Client)
+set_global_signing(Signature)
}
class Error {
+UrlParser(ParseError)
+Request(reqwest::Error)
+NoUrl
+HeaderValue(reqwest::header::InvalidHeaderValue)
+DeserializeContentType(String)
+DeserializeJson(serde_json::Error)
+BodyError(TracerError)
}
class Method {
<<enumeration>>
Get
Post
Put
Delete
Patch
Head
Options
Connect
Trace
None
}
class RequestBuilder {
+client: Option<Arc<Client>>
+method: Method
+uri: Option<Url>
+headers: HeaderMap
+body: Body
+content_type: String
+signature: Option<Signature>
+new_with_client(client: Option<Client>, method: Method, uri: Url)
+method(T: Into<Method>)
+uri(uri: &str)
+header(H: Into<Header>)
+headers(H: Into<HeaderMap>)
+body(B: Into<Body>)
+content_type(content_type: &str)
+with_signing_default()
+with_signing(signature: Signature)
+send()
}
class Response {
+status: StatusCode
+headers: HeaderMap
+content_length: Option<u64>
+url: Url
+body: BodyOwned
+status()
+to(T: DeserializeOwned)
}
class StatusCode {
+code: u16
+reason: &'static str
+as_u16()
+as_str()
}
class Url {
+Simple(url: String)
}
class Body {
+Empty
}
class BodyOwned {
+from(box_raw: Box<reqwest::Response>)
}
}
%% Relationships
Client --> RequestBuilder
RequestBuilder --> Response
RequestBuilder --> Error
Response --> StatusCode
Response --> HeaderMap
Response --> Url
Response --> BodyOwned
HeaderMap --> Header
Header --> Name
Header --> Values
Values --> Value
Http Request Chaining (gearbox::net::http::request_chaining)
Complete architectural overview:
classDiagram
%% Package: request
namespace request {
class Client {
+client: reqwest::Client
+new()
+with_client(reqwest::Client)
+set_global_signing(Signature)
}
class Error {
+UrlParser(ParseError)
+Request(reqwest::Error)
+NoUrl
+HeaderValue(reqwest::header::InvalidHeaderValue)
+DeserializeContentType(String)
+DeserializeJson(serde_json::Error)
+BodyError(TracerError)
}
class Method {
<<enumeration>>
Get
Post
Put
Delete
Patch
Head
Options
Connect
Trace
None
}
class RequestBuilder {
+client: Option<Arc<Client>>
+method: Method
+uri: Option<Url>
+headers: HeaderMap
+body: Body
+content_type: String
+signature: Option<Signature>
+new_with_client(client: Option<Client>, method: Method, uri: Url)
+method(T: Into<Method>)
+uri(uri: &str)
+header(H: Into<Header>)
+headers(H: Into<HeaderMap>)
+body(B: Into<Body>)
+content_type(content_type: &str)
+with_signing_default()
+with_signing(signature: Signature)
+send()
}
class Response {
+status: StatusCode
+headers: HeaderMap
+content_length: Option<u64>
+url: Url
+body: BodyOwned
+status()
+to(T: DeserializeOwned)
}
class StatusCode {
+code: u16
+reason: &'static str
+as_u16()
+as_str()
}
class Url {
+Simple(url: String)
}
class Body {
+Empty
}
class BodyOwned {
+from(box_raw: Box<reqwest::Response>)
}
}
%% Package: request_chaining
namespace request_chaining {
class Header {
+name: Name
+values: Values
}
class HeaderMap {
+inner: HashMap<Name, Values>
+get(K: Into<Name>)
+insert(header: Header)
+extend(headers: HeaderMap)
}
class Name {
+String name
}
class Values {
+Vec<Value> values
+iter()
}
class Value {
+Vec<u8> value
+as_bytes()
+to_vec()
}
class TracerError
class Signature
class ParseError
}
%% Relationships
Client --> RequestBuilder
RequestBuilder --> Response
RequestBuilder --> Error
Response --> StatusCode
Response --> HeaderMap
Response --> Url
Response --> BodyOwned
HeaderMap --> Header
Header --> Name
Header --> Values
Values --> Value
Signature (gearbox::net::signature)
Payload Signature Config/Generator This object is for creating a API key signature.
This this example a static nonce is used to generate a API signature. This is to confirm the signature is as expected. The example is also using the default signature configuration.
extern crate alloc;
use Arc;
use Signature;
use base64;
let mut signing = default;
let nonce = 1616492376594usize;
let validated_sign = decode.unwrap;
let cal_sign = signing
.var
.var
.var
.nonce
.sign;
assert_eq!
At the time of signing is might be usefull to locking the nonce. By locking the nonce you will prevent change in the next signing. This is usefull in the default signing configuration, and if the nonce is not predictable.
In this example the signature will only generate a base64 encoded value.
extern crate alloc;
use Arc;
use *;
use base64;
let mut signing = default;
let cal_sign = signing
.config.nonce_default;
let nonce = cal_sign.nonce_lock;
let b64_nonce = encode.into_bytes;
assert_eq!;
Note: Using nonce_lock will lock the nonce until the next signing, as soon as a signing has happened the lock will be removed! Also running the lock multiple times will force the signature generator to create new nonce values.
Railway Future extension (gearbox::rails::ext::future)
FutureOptional and FutureResult Documentation
FutureOptional
An extension trait for Futures that yield Option<T> that provides a variety of convenient adapters.
map
Map this future's optional output to a different type, returning a new future of the resulting type.
This function is similar to the Option::map where it will change the type of the underlying future. This is useful to chain along a computation once a future has been resolved and if it is Some.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.map;
let final_res = res.await;
assert_eq!;
and_then
Chains this future with another future if the output is Some, returning a new future of the resulting type.
This function is similar to the Option::and_then where it will chain another computation if the future resolves to Some.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.and_then;
let final_res = res.await;
assert_eq!;
filter
Filters the output of this future, returning None if the predicate returns false.
This function is similar to the Option::filter where it will return None if the predicate returns false.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.filter;
let final_res = res.await;
assert_eq!;
or
Returns this future's output if it is Some, otherwise returns the provided fallback.
This function is similar to the Option::or where it will return the provided fallback if the future resolves to None.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.or;
let final_res = res.await;
assert_eq!;
let future_opt = async ;
let res = future_opt.or;
let final_res = res.await;
assert_eq!;
or_else
Returns this future's output if it is Some, otherwise calls the provided fallback function.
This function is similar to the Option::or_else where it will call the provided fallback function if the future resolves to None.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.or_else;
let final_res = res.await;
assert_eq!;
let future_opt = async ;
let res = future_opt.or_else;
let final_res = res.await;
assert_eq!;
unwrap_or
Returns this future's output if it is Some, otherwise returns the provided default.
This function is similar to the Option::unwrap_or where it will return the provided default if the future resolves to None.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.unwrap_or;
let final_res = res.await;
assert_eq!;
let future_opt = async ;
let res = future_opt.unwrap_or;
let final_res = res.await;
assert_eq!;
unwrap_or_else
Returns this future's output if it is Some, otherwise calls the provided fallback function.
This function is similar to the Option::unwrap_or_else where it will call the provided fallback function if the future resolves to None.
Example
use FutureOptional;
let future_opt = async ;
let res = future_opt.unwrap_or_else;
let final_res = res.await;
assert_eq!;
let future_opt = async ;
let res = future_opt.unwrap_or_else;
let final_res = res.await;
assert_eq!;
merge
Merges this future with an optional value, producing a new future.
This function takes an additional option and a function to combine the resolved value of the future and the option into a new future.
Example
use FutureOptional;
async
let x = async ;
let y = Some;
let res = x.merge;
assert_eq!;
merge2
Merges this future with two optional values, producing a new future.
This function takes two additional options and a function to combine the resolved value of the future and the options into a new future.
Example
use FutureOptional;
async
let x = async ;
let y = Some;
let z = Some;
let res = x.merge2;
assert_eq!;
merge3
Merges this future with three optional values, producing a new future.
This function takes three additional options and a function to combine the resolved value of the future and the options into a new future.
Example
use FutureOptional;
async
let x = async ;
let y = Some;
let z = Some;
let a = Some;
let res = x.merge3;
assert_eq!;
merge4
Merges this future with four optional values, producing a new future.
This function takes four additional options and a function to combine the resolved value of the future and the options into a new future.
Example
use FutureOptional;
async
let x = async ;
let y = Some;
let z = Some;
let a = Some;
let b = Some;
let res = x.merge4;
assert_eq!;
FutureResult
An extension trait for Futures that yield Result<T, E> that provides a variety of convenient adapters.
map
Map this future's result output to a different type, returning a new future of the resulting type.
This function is similar to the Result::map where it will change the type of the underlying future. This is useful to chain along a computation once a future has been resolved and if it is Ok.
Example
use FutureResult;
let future_res = async ;
let res = future_res.map;
let final_res = res.await;
assert_eq!;
map_or
Maps a Result by applying a function to the contained Ok value, or a default value if it is Err.
This function is similar to the Result::map_or.
Example
map_err
Maps a Result by applying a function to the contained Err value.
This function is similar to the Result::map_err.
Example
use FutureResult;
let future_res = async ;
let res = future_res.map_err;
let final_res = res.await;
assert_eq!;
and_then
Chains this future with another future if the output is Ok, returning a new future of the resulting type.
This function is similar to the Result::and_then where it will chain another computation if the future resolves to Ok.
Example
use FutureResult;
let future_res = async ;
let res = future_res.and_then;
let final_res = res.await;
assert_eq!;
or_else
Returns this future's result if it is Ok, otherwise calls the provided fallback function.
This function is similar to the Result::or_else where it will call the provided fallback function if the future resolves to Err.
Example
use FutureResult;
let future_res = async ;
let res = future_res.or_else;
let final_res = res.await;
assert_eq!;
let future_res = async ;
let res = future_res.or_else;
let final_res = res.await;
assert_eq!;
unwrap_or_else
Returns this future's result if it is Ok, otherwise calls the provided fallback function.
This function is similar to the Result::unwrap_or_else where it will call the provided fallback function if the future resolves to Err.
Example
use FutureResult;
let future_res = async ;
let res = future_res.unwrap_or_else;
let final_res = res.await;
assert_eq!;
let future_res = async ;
let res = future_res.unwrap_or_else;
let final_res = res.await;
assert_eq!;
merge
Merges this future with a result value, producing a new future.
This function takes an additional result and a function to combine the resolved value of the future and the result into a new future.
Example
use FutureResult;
async
let x = async ;
let y = Ok;
let res = x.merge;
assert_eq!;
merge2
Merges this future with two result values, producing a new future.
This function takes two additional results and a function to combine the resolved value of the future and the results into a new future.
Example
use FutureResult;
async
let x = async ;
let y = Ok;
let z = Ok;
let res = x.merge2;
assert_eq!;
merge3
Merges this future with three result values, producing a new future.
This function takes three additional results and a function to combine the resolved value of the future and the results into a new future.
Example
use FutureResult;
async
let x = async ;
let y = Ok;
let z = Ok;
let a = Ok;
let res = x.merge3;
assert_eq!;
merge4
Merges this future with four result values, producing a new future.
This function takes four additional results and a function to combine the resolved value of the future and the results into a new future.
Example
use FutureResult;
async
let x = async ;
let y = Ok;
let z = Ok;
let a = Ok;
let b = Ok;
let res = x.merge4;
assert_eq!;
Dynamic Serialization/Deserialization (gearbox::serde::dynamic)
Simple serde is as its said, a simplified implementation of multiple repositories for serialization and deserialization.
In Short the goal is to have a single tool for serialization and deserialization, with a common interface.
Usage
Simple Serde uses .encode and .decode for encoding and decoding. Decode can be done on any
Vec<u8> or &[u8] this allows for the cleanest implementation.
The same goes for anything that needs to be serialized/encoded. Any type that implements the
#[derive(Serialize)] can easily be encoded using .encode
Encode/Decode
.encode and .decode both takes a ContentType which defines what you are encoding/decoding
from/to.
an example would be [some Vec<u8>].decode("bson") or my_struct.encode("bson").
This is possible as ContentType implements the TryFrom trait for &str, String.
In case the implementation is unable to decode what type you are trying to encode/decode from/to
an Err result with Error::UnknownContentTypeMatchFromStr will be returned from the
encoder/decoder
Anything coming out of the encoder will be of type Vec<u8> further the Vec<u8> is wrapped in
a struct called Encoded this allow for further simplifications on implementation like,
TryToString which will automatically try to convert Encoded to a String, in addition
Encoded had implemented the Deref and DerefMut traits to make it easier to gain access to
encapsulated data.
Supported formats
- Bson
- Cbor
- FlexBuffers
- Json
- Json5
- Lexpr
- MessagePack
- Pickle
- Postcard
- Ron
- Toml
- Url
- Yaml
- Xml (Awaiting serde-xml-rs v. >0.51)
further all string definitions of ContentType is case insensitive, and has an alternate
application/[format]application/x-[format]
Serialization/Encode example
use Deref;
use Serialize;
use serde_derive;
use ;
let my_foo = Foo ;
let encoded: Encoded = my_foo
.encode
.expect;
assert_eq!;
assert_eq!
Deserialization/Decode example
use Deref;
use Deserialize;
use serde_derive;
use ;
let my_foo = Foo ;
let v_u8_data = &vec!;
let string_data = r#"---
bar: foobar
"#;
let decoded_from_v_u8: = v_u8_data.decode.expect;
let decoded_from_string: = string_data.decode.expect;
assert_eq!;
assert_eq!;
RwArc (gearbox::sync::rw_arc)
The rw_arc module provides a robust and flexible mechanism for handling concurrent data access in Rust, allowing either multiple readers or a single writer to access the data. This module is designed to provide high performance and safety in multi-threaded environments. It includes various structures such as ReadArc, DetachedArc, HyperWriteArc, UpgradableArc, and WriteArc, each tailored for specific concurrent access patterns. It combines the functionality of Arc and RwLock while avoiding the constraints of lifetimes, offering more versatility and some inherent risks.
Overview
Features
- Reader-Writer Locks: Allows multiple readers or a single writer to access the data, ensuring efficient data access without contention.
- Upgradeable Locks: Enables a lock to be upgraded from read to write access, allowing more flexible and efficient lock management.
- DetachedArcs: Provides a way to create detached instances of locks that can be later attached to the main lock, enabling flexible lock management across different contexts.
- HyperLocks (Write-on-Destruct): Efficient write operations that ensure data is written back to the main storage upon destruction, optimizing for cases where write operations are deferred.
Benefits and Risks
- Versatility: By combining the features of
ArcandRwLock,rw_arcallows for cross-clones and other operations that are not constrained by lifetimes. This increases the flexibility of concurrent programming. - Danger: The absence of lifetime constraints can make the
rw_arcmodule more dangerous to use, as it relies on manual guarantees of safety that are normally enforced by the Rust compiler's borrow checker.
Architecture
graph TD;
RwArc -->|Has| RwArcInner;
RwArcInner -->|Contains| Arc;
Arc -->|References| Data;
RwArc -->|Provides| ReadArc;
RwArc -->|Provides| WriteArc;
RwArc -->|Provides| UpgradableArc;
RwArc -->|Provides| DetachedArc;
RwArc -->|Provides| HyperReadArc;
RwArc -->|Provides| HyperWriteArc;
Usage Example
use RwArc;
use Arc;
let lock: = new;
// Read access
// Write access
// Upgradeable read//!
Detailed Descriptions
ReadArc
A read-only lock guard that allows multiple concurrent readers. Even if the underlying data is dropped, ReadArc will continue to hold the data, making it a true clone of the existing data.
Example
use RwArc;
use Arc;
let lock : = new;
let read_arc = lock.read;
println!;
DetachedArc
A detached lock guard that can be attached to an existing RwArc. This allows for flexible lock management where locks can be created and attached later.
Example
use ;
let rw_arc: = new;
let detached_arc = new;
if let Some = detached_arc.attach_read
HyperWriteArc
A write lock guard that ensures data written is properly stored back into the main data structure upon destruction. This is known as Write-on-Destruct (WOD) and is designed for efficient deferred write operations.
Example
use RwArc;
let lock: = new;
UpgradableArc
An upgradable read lock guard that can be upgraded to a write lock, allowing more flexible and efficient lock management.
Example
use RwArc;
let lock: = new;
WriteArc
A write lock guard that allows modifying the data while ensuring that the modifications are safely committed.
Example
use RwArc;
let lock: = new;
RwArc (gearbox::template)
Description:
The template module provides a robust and flexible mechanism for rendering templates in Rust, allowing dynamic content insertion and transformation through pipelines. This module is designed to provide high performance and safety in template rendering and data transformation. It includes various structures such as TemplateEngine, TemplateContext, PipelineManager, DatePipeline, and PrefixPipeline, each tailored for specific templating patterns. It combines the functionality of a template parser and a pipeline manager, offering versatility and simplicity in template rendering.
Features
- TemplateEngine: Responsible for rendering templates using context data and applying pipelines for data transformations.
- TemplateContext: Manages the context data for templates, allowing dynamic insertion and retrieval of values.
- PipelineManager: Manages the available pipelines for data transformation, supporting default and custom pipelines.
- DatePipeline: A pipeline for formatting dates.
- PrefixPipeline: A pipeline for prefixing strings.
Benefits and Risks
- Versatility: The module's flexibility enhances development by allowing dynamic template rendering and easy extension with custom pipelines.
- Danger: Potential risks include improper pipeline usage and performance overhead from locking mechanisms in multithreaded environments.
Thread Safety
The TemplateEngine and its associated components use atomic operations and locking mechanisms for thread safety. The Mutex used for PIPELINES ensures safe concurrent access, though it may introduce performance overhead.
Breaking Cycles with Weak References
The module does not directly use weak references but employs Mutex for safe concurrent access to shared resources.
Cloning References
Creating a new reference from an existing template engine or context is done using the Clone trait implemented for TemplateEngine and TemplateContext.
use ;
let engine = new;
let engine_clone = engine.clone;
let context = new;
let context_clone = context.clone;
Deref Behavior
TemplateContext provides direct access to its internal HashMap for managing context variables, allowing easy insertion and retrieval of values.
Usage Examples
Sharing Some Immutable Data Between Threads
use ;
use thread;
let engine = new;
let mut context = new;
context.insert;
for _ in 0..10
Sharing a Mutable Atomic Value
use ;
use ;
use Arc;
use thread;
let engine = new;
let val = new;
for _ in 0..10
Detailed Descriptions
TemplateEngine
The TemplateEngine structure is the core of the template module. It is responsible for rendering templates by applying context data and utilizing pipelines for data transformation. The TemplateEngine maintains a PipelineManager which holds all available pipelines.
-
Fields:
pipelines: APipelineManagerinstance that manages all registered pipelines.
-
Methods:
new() -> Self: Creates a newTemplateEngineinstance with default pipelines.get_pipelines_default() -> PipelineManager: Returns the default set of pipelines.update_pipeline<P: Pipeline + Send + Sync + 'static>(name: &str, pipeline: P): Updates or adds a pipeline with the specified name.get_pipeline(name: &str) -> Option<Box<dyn Pipeline + Send + Sync>>: Retrieves a specific pipeline by name.get_pipelines() -> PipelineManager: Retrieves all available pipelines.render(&self, template: &str, context: &TemplateContext) -> Result<String, DynTracerError>: Renders a template using the provided context.
use ;
let engine = new;
let mut context = new;
context.insert;
let result = engine.render.unwrap;
assert_eq!;
TemplateContext
The TemplateContext structure manages the context data used in templates. It allows for dynamic insertion and retrieval of values.
-
Fields:
variables: AHashMapthat stores the context variables.
-
Methods:
new() -> Self: Creates a newTemplateContextinstance.insert(&mut self, key: &str, value: Box<dyn PipelineValue + Send + Sync>): Inserts a new context variable.get(&self, key: &str) -> Option<&Box<dyn PipelineValue + Send + Sync>>: Retrieves a context variable by key.
use TemplateContext;
let mut context = new;
context.insert;
println!;
PipelineManager
The PipelineManager structure manages the available pipelines for data transformation.
-
Fields:
0: AHashMapthat stores the pipelines.
-
Methods:
- Implements
DerefandDerefMuttraits to provide access to the internalHashMap. - Implements
Clonetrait to allow cloning of thePipelineManageralong with its pipelines.
- Implements
use ;
let mut manager = default;
manager.insert;
println!;
DatePipeline
The DatePipeline structure is a pipeline for formatting dates. It implements the Pipeline trait.
-
Fields:
format: AStringthat stores the date format.
-
Methods:
new() -> Self: Creates a newDatePipelinewith a default format.format(&self, pipe_object: &Box<dyn PipelineValue + Send + Sync>) -> Box<dyn PipelineValue + Send + Sync>: Formats the input value using the pipeline's transformation.options(&self, options: &str) -> Box<dyn Pipeline + Send + Sync>: Creates a new instance of the pipeline with the specified options.boxed_clone(&self) -> Box<dyn Pipeline + Send + Sync>: Clones the pipeline into a boxed instance.
use ;
use DateTime;
let date_pipeline = new;
let date = from_date;
let value: = Boxnew;
let formatted = date_pipeline.format;
println!;
PrefixPipeline
The PrefixPipeline structure is a pipeline for prefixing strings. It implements the Pipeline trait.
-
Fields:
prefix: AStringthat stores the prefix.
-
Methods:
new() -> Self: Creates a newPrefixPipelinewith a default prefix.format(&self, pipe_object: &Box<dyn PipelineValue + Send + Sync>) -> Box<dyn PipelineValue + Send + Sync>: Formats the input value using the pipeline's transformation.options(&self, options: &str) -> Box<dyn Pipeline + Send + Sync>: Creates a new instance of the pipeline with the specified options.boxed_clone(&self) -> Box<dyn Pipeline + Send + Sync>: Clones the pipeline into a boxed instance.
use ;
let prefix_pipeline = new;
let value: = Boxnew;
let prefixed = prefix_pipeline.format;
println!;
Architectural Diagram
graph TD;
TemplateEngine -->|Uses| PipelineManager;
PipelineManager -->|Contains| DatePipeline;
PipelineManager -->|Contains| PrefixPipeline;
TemplateEngine -->|Uses| TemplateContext;
TemplateContext -->|Contains| Variables;
TemplateEngine -->|Uses| Pipelines;
TODO
- ( gearbox::log::* ) Clean up Log fmt/syslog, some of the code can be combined and cleaned up a bit better, also the formatter supports syslog, and bunyan, this should probably be cleared up a bit more, and separated better.
- ( gearbox::path::* ) current this system is mainly just exposing the dirs::* library, this should be removed.
- ( gearbox::* ) Remove usage for Vec or move usage of std::vec::Vec to another no-std library
Current version: 4.0.0-rc3
License: MIT