docs: 📝 add README.md for lib/rustlers

This commit is contained in:
Lucas Colombo 2024-05-21 02:37:21 -03:00
parent e7bcf5f1d4
commit b3a9301264
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
3 changed files with 109 additions and 1 deletions

11
.github/img/doc-title-rustler.svg vendored Normal file
View File

@ -0,0 +1,11 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 116 54">
<style>
.a { fill: #000000; }
@media (prefers-color-scheme: dark) {
.a { fill: #ffffff; }
}
</style>
<path class="a" d="M70,26H68a4,4,0,0,1,4,4H68a3.99443,3.99443,0,0,1-3.30475-1.74884A11.95966,11.95966,0,0,1,56,32H50.5a.5.5,0,0,1-.5-.5v-11a.5.5,0,0,1,.5-.5H67.48285l.0144.00018a.50528.50528,0,0,1,.49176.51831,12.03909,12.03909,0,0,1-.312,2.236A3.97,3.97,0,0,1,70,22h4A4,4,0,0,1,70,26Zm6-10H42V12h8V4.5a.5.5,0,0,1,.5-.5H68v8h8Z" />
<path class="a" d="M10,46H22v2H10Zm84,0h12v2H94Z" />
<path class="a" d="M30.84546,48.46484h1.42773v-3.4082H30.84546V43.66406h3.20361v1.752h.084a3.55107,3.55107,0,0,1,.23437-.65332,2.13106,2.13106,0,0,1,.38379-.56446,1.69883,1.69883,0,0,1,.56982-.38965,2.00651,2.00651,0,0,1,.792-.14453h.58838v1.63282H35.3811a1.24421,1.24421,0,0,0-.9956.38769,1.40477,1.40477,0,0,0-.33643.95117v1.8291h2.04v1.39161H30.84546Zm10.91016.26368h-.07227a2.20387,2.20387,0,0,1-.6416.90039A1.77169,1.77169,0,0,1,39.85962,50a2.16822,2.16822,0,0,1-.81592-.14941,1.65809,1.65809,0,0,1-.63623-.4502,2.18,2.18,0,0,1-.41406-.73242,3.02043,3.02043,0,0,1-.1499-.99512V43.66406h1.77587v3.76856q0,1.1997,1.044,1.20019a1.5192,1.5192,0,0,0,.40234-.05469,1.00553,1.00553,0,0,0,.34766-.168.92264.92264,0,0,0,.24609-.28125.78686.78686,0,0,0,.0962-.39649V43.66406h1.77587v6.19239H41.75562ZM47.67358,50a4.71586,4.71586,0,0,1-1.79394-.31152,2.54291,2.54291,0,0,1-1.146-.85157l.9961-.9248a2.566,2.566,0,0,0,.84619.61816,2.6358,2.6358,0,0,0,1.10986.22266,1.76649,1.76649,0,0,0,.82813-.16211.53185.53185,0,0,0,.2998-.498.37347.37347,0,0,0-.2041-.36621,2.03377,2.03377,0,0,0-.564-.16211l-.99609-.15625a4.12407,4.12407,0,0,1-.77978-.18554,2.042,2.042,0,0,1-.624-.3418,1.59818,1.59818,0,0,1-.41992-.52832,1.67581,1.67581,0,0,1-.15625-.75586,1.79276,1.79276,0,0,1,.74414-1.52441,3.446,3.446,0,0,1,2.08789-.55176,4.35406,4.35406,0,0,1,1.5542.24609,2.45377,2.45377,0,0,1,1.02588.70215l-.8877,1.00781a2.26014,2.26014,0,0,0-.708-.5039,2.43517,2.43517,0,0,0-1.04444-.20411q-1.032,0-1.03174.624a.38372.38372,0,0,0,.20411.37793,2.02864,2.02864,0,0,0,.564.16211l.98389.15625a4.12407,4.12407,0,0,1,.77978.18554,2.13007,2.13007,0,0,1,.63037.3418,1.57252,1.57252,0,0,1,.42579.52832,1.67581,1.67581,0,0,1,.15625.75586,1.82005,1.82005,0,0,1-.75,1.53613A3.4907,3.4907,0,0,1,47.67358,50Zm7.56006-.14355a1.85876,1.85876,0,0,1-1.42822-.50293,1.804,1.804,0,0,1-.46777-1.28028v-3.0166H51.65747V43.66406h1.09229a.82345.82345,0,0,0,.564-.15527.77537.77537,0,0,0,.168-.57617V41.48047h1.63184v2.18359h2.4121v1.39258h-2.4121v3.4082h2.4121v1.39161Zm3.60547-1.39161H60.7356V42.36816H58.83911v-1.3916H62.512v7.48828h1.89551v1.39161H58.83911ZM68.64282,50a3.06922,3.06922,0,0,1-2.36328-.86328,3.3174,3.3174,0,0,1-.80469-2.35254,4.14575,4.14575,0,0,1,.21-1.36133,2.93815,2.93815,0,0,1,.60059-1.03223,2.53993,2.53993,0,0,1,.94824-.64843,3.37487,3.37487,0,0,1,1.25391-.22168,3.3327,3.3327,0,0,1,1.24805.22168,2.61169,2.61169,0,0,1,.93554.62988,2.82413,2.82413,0,0,1,.59375.99023,3.80393,3.80393,0,0,1,.21,1.30176v.52832H67.2395v.1084a1.295,1.295,0,0,0,.37207.96582,1.4917,1.4917,0,0,0,1.0918.36621,2.00488,2.00488,0,0,0,.94824-.21,2.2576,2.2576,0,0,0,.6836-.55859l.96,1.04394a3.01449,3.01449,0,0,1-1.00782.76856A3.67242,3.67242,0,0,1,68.64282,50Zm-.14355-5.208a1.216,1.216,0,0,0-.918.35449,1.31281,1.31281,0,0,0-.3418.95411v.0957h2.4961v-.0957a1.34032,1.34032,0,0,0-.33008-.96A1.18957,1.18957,0,0,0,68.49927,44.792Zm4.127,3.67285h1.42871v-3.4082H72.62622V43.66406h3.2041v1.752h.084a3.48128,3.48128,0,0,1,.23437-.65332,2.11952,2.11952,0,0,1,.38379-.56446,1.69117,1.69117,0,0,1,.57031-.38965,2.00144,2.00144,0,0,1,.792-.14453h.58789v1.63282H77.16235a1.24731,1.24731,0,0,0-.99609.38769,1.40683,1.40683,0,0,0-.33594.95117v1.8291h2.04v1.39161H72.62622ZM82.27466,50a4.71416,4.71416,0,0,1-1.79395-.31152,2.53909,2.53909,0,0,1-1.14648-.85157l.99609-.9248a2.57423,2.57423,0,0,0,.84571.61816,2.63962,2.63962,0,0,0,1.11035.22266,1.76779,1.76779,0,0,0,.82812-.16211.531.531,0,0,0,.29981-.498.37423.37423,0,0,0-.2041-.36621,2.035,2.035,0,0,0-.56348-.16211l-.9961-.15625a4.12948,4.12948,0,0,1-.78027-.18554,2.04206,2.04206,0,0,1-.624-.3418,1.59818,1.59818,0,0,1-.41992-.52832,1.66918,1.66918,0,0,1-.15625-.75586,1.79273,1.79273,0,0,1,.74414-1.52441,3.447,3.447,0,0,1,2.08789-.55176,4.34753,4.34753,0,0,1,1.55371.24609,2.44925,2.44925,0,0,1,1.02637.70215l-.8877,1.00781a2.25441,2.25441,0,0,0-.708-.5039,2.43589,2.43589,0,0,0-1.04394-.20411q-1.03272,0-1.03223.624a.38294.38294,0,0,0,.2041.37793,2.03488,2.03488,0,0,0,.56348.16211l.98437.15625a4.1294,4.1294,0,0,1,.78028.18554,2.13369,2.13369,0,0,1,.62988.3418,1.57832,1.57832,0,0,1,.42578.52832,1.68222,1.68222,0,0,1,.15625.75586,1.81881,1.81881,0,0,1-.75,1.53613A3.4881,3.4881,0,0,1,82.27466,50Z" />
</svg>

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -34,5 +34,7 @@
"rustler.db-wal": true,
"rustler.db.bkp": true
},
"editor.formatOnSave": true
"[rust]": {
"editor.formatOnSave": true
}
}

95
lib/rustlers/README.md Normal file
View File

@ -0,0 +1,95 @@
<h1 align="center"><img src="../../.github/img/doc-title-rustler.svg" height="264"></h1>
<br>
<br>
## `rustler.rs`
Contains the `rustler!` macro, which is used to define a `Rustler`.
`Rustler` is a trait that extends the `RustlerAccessor` trait.
Together, they define the interface and the common functionalities for all Rustlers.
### The `RustlerAccessor` trait
The `RustlerAccessor` trait defines the interface for accessing the Rustler's data (`getters` and `setters` that all Rustlers must implement).
Some of the expected accessors are:
- `status` and `set_status`
- `tickers` and `set_tickers`
- `msg_sender` and `set_msg_sender`
### The `Rustler` trait
The `Rustler` trait extends the `RustlerAccessor` trait and defines the interface for common Rustler's functionalities:
- `start` the Rustler, calling abstract `connect` if the Rustler is set to connect on start
- status change handling
- `add` new tickers to the Rustler (calling `on_add` at the end if tickers were added). Also calls `connect` if the Rustler is set to connect on add and the Rustler is not already connected
- `delete` tickers from the Rustler (calling `on_delete` at the end if tickers were deleted). Also calls `disconnect` if there are no more tickers in the Rustler.
The `Rustler` trait also defines the following abstract methods that must be implemented by each `Rustler` implementation.
- `connect` method that connects the Rustler to the data source
- `disconnect` method that disconnects the Rustler from the data source
- `on_add` method that is called when new tickers are added to the Rustler. This method is called when new tickers are added to the Rustler and must implement the logic to start tracking and rustling the new tickers.
- `on_delete` method that is called when tickers are deleted from the Rustler. This method is called when tickers are deleted from the Rustler and must implement the logic to stop tracking and rustling the deleted tickers.
### The `rustler!` macro
The `rustler!` macro is used to define a `Rustler` and to automatically implement the `RustlerAccessor` trait. This adds the necessary fields and accessors to the struct.
**Example:**
```rust
rustler! {
pub struct MyRustler { }
}
```
Now we have a `MyRustler` struct that implements the `RustlerAccessor`
trait and has all the necessary fields and accessors :)
## `rustlerjar.rs`
This files defines the `RustlerJar` struct.
A `RustlerJar` is a collection of `Rustler`s and their corresponding mappings to markets. Such mapping indicates which Rustler should be used for a given market.
It provides methods to retrieve Rustlers by `Market`.
### The `rustlerjar!` macro
The `rustlerjar!` macro is used to create an instance of a `RustlerJar` on an easy way.
**Example:**
```rust
let rustler_jar = rustlerjar! {
"NYSE", "NASDAQ" => MarketRustler::create,
"BINANCE" => BinanceRustler::create(url),
};
let rustler = rustler_jar.get(&market);
```
the `rustlerjar!` expects a mapping of **market names** pointing to **`Rustler` creation functions (constructors)** and will return a `RustlerJar` instance.
Note: the `rustlerjar!` macro executes the `create` function for each `Rustler`, so in the example above, we assume that `BinaceRustler::create(url)` returns a function that creates a new instance of `BinanceRustler` and not an instance of `BinanceRustler`.
## `svc.rs`
Contains the `RustlersSvc` struct.
The `RustlersSvc` struct is a service that manages the execution of several `Rustler`s from a `RustlerJar`.
It is responsible for starting and stopping the Rustlers on the right schedule.
It contains a `MarketService`, which connects to the database and is used to retrieve the markets (including their schedules) and their tickets. Then, for each market, it retrieves the corresponding Rustler from the `RustlerJar`, adds the tickers to the it, and starts it.
> [!NOTE]
>
> <img alt="unimplemented" src="./../../.github/img/todo.svg" height="12">
>
> Although it's not yet implemented, the `RustlersSvc` will also be responsible for adding and deleting tickers and rustlers at runtime.