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 | 0.0% | 0 | 2 | |
| src/collections/const_hash_map | 0.0% | 0 | 240 | |
| src/collections/hash_map | 25.76% | 51 | 198 | |
| src/collections/simple_linked_list | 100.0% | 146 | 146 | |
| src/collections/vec_deque | 96.02% | 193 | 201 | |
| src/common | 91.3% | 21 | 23 | |
| src/error | 35.14% | 13 | 37 | |
| src/error/tracer | 41.47% | 265 | 639 | |
| src/log/fmt | 36.42% | 409 | 1123 | |
| src/log/fmt/layer | 0.0% | 0 | 130 | |
| src/log/syslog | 100.0% | 166 | 166 | |
| src/net | 74.29% | 52 | 70 | |
| src/net/http/request | 42.33% | 527 | 1245 | |
| src/net/http/request/header | 47.48% | 151 | 318 | |
| src/net/http/request_chaining | 95.04% | 613 | 645 | |
| src/net/http/test | 86.76% | 59 | 68 | |
| src/net/signature | 83.3% | 404 | 485 | |
| src/rails/ext/blocking | 32.71% | 174 | 532 | |
| src/rails/ext/future | 98.49% | 718 | 729 | |
| src/rails/ext/future/ext/option | 84.19% | 378 | 449 | |
| src/rails/ext/future/ext/result | 80.98% | 315 | 389 | |
| src/rails/tracing | 100.0% | 115 | 115 | |
| src/serde/dynamic | 76.09% | 261 | 343 | |
| src/serde/dynamic/test | 14.71% | 5 | 34 | |
| src/storage | 0.0% | 0 | 39 | |
| src/storage/io/file | 58.44% | 180 | 308 | |
| src/sync | 81.15% | 538 | 663 | |
| src/task | 96.84% | 306 | 316 | |
| src/task/multicommand | 62.64% | 114 | 182 | |
| src/template | 84.38% | 281 | 333 | |
| src/template/pipelines | 81.16% | 56 | 69 | |
| src/time | 47.11% | 1116 | 2369 |
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.
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!;
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!;
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: 2.0.0
License: MIT