imag/doc/src/01010-architecture.md

112 lines
4.9 KiB
Markdown
Raw Normal View History

# Architecture of the imag code
2017-10-14 19:35:55 +00:00
The imag codebase has a rather simple overall architecture.
In this chapter the types of crates, architecture of an imag module
and the type structure are described.
## Crate types
2017-10-14 13:30:06 +00:00
There are different types of crates in the imag world. A crate is a rust
project.
First of all, there are core crates. These crates provide the very core of imag
and almost all other crates use them:
2017-10-14 19:35:55 +00:00
* libimagstore - The imag store is the abstraction over the filesystem. It
2017-10-14 13:30:06 +00:00
provides primitives to get, write and manipulate store entries and their
header information.
* libimagrt - The runtime library, which provides functionality to create a
store object from libimagstore, helps with configurarion loading and
commandline argument handling (through the external "clap" crate).
* libimagerror - Error handling library for handling errors the imag way. Used
in all other crates, even the store itself. It also offers functionality to
log and trace errors as well as exiting the application, if necessary.
* libimagutil - Utilities.
2017-10-14 13:30:06 +00:00
The next type of imag crates are entry extension libraries. Those provide
extensional functionality for the types from libimagstore. For example, there is
"libimagentrylink" which provides functionality to link two entries in the
store.
2017-10-14 19:35:55 +00:00
The third kind of crate is the one that offers end-user functionality for a imag
domain, for example "libimagtodo" provides functionality to track todos.
2017-10-14 13:30:06 +00:00
And last, but not least, the commandline frontend crates provide the user
interface. These are the kind of crates that are not library crates, but
binaries.
2017-10-14 19:35:55 +00:00
Besides these, there are some other utility crates.
## Architecture of an imag module
With the things from above, a module could have the following architecture:
```
+---------------------------------------------+
| imag-foo |
+-----------------------------------+---------+
| libimagfoo | |
+-----------------+-----------------+ |
| | | |
| libimagentrybar | libimagentrybaz | |
| | | lib |
+-----------------+-----------------+ |
| | |
| ... | |
| | imag |
+-----------------------------------+ |
| | |
| libimagrt | |
| | error |
+-----------------------------------+ |
| | |
| libimagstore | |
| | |
+-----------------------------------+---------+
```
2017-10-14 19:35:55 +00:00
The foundation of all imag modules is the store, as one can see in the image.
Above the store library there is the libimagrt, which provides the basic runtime
and access to the `Store` object.
Cross-cutting, there is the error library (and possibly
2017-10-14 13:30:06 +00:00
the util library, but we do not care about this one here), which is used through
all levels. The highest level of all imag modules is the commandline interface
on top of the domain library. In between can be any number of entry extension
libraries, or none if not needed.
2017-10-14 19:35:55 +00:00
Theoretically, the commandline interface crate could be replaced to build a
terminal user interface, graphical user interface or web interface.
## Types
2017-10-14 13:30:06 +00:00
The imag core, hence the libimagstore, libimagrt and libimagerror, provide a set
of types that a user (as in a library writer) should be aware of.
2017-10-14 19:35:55 +00:00
First of all, there is the `Runtime` type which is provided by the libimagrt. It
2017-10-14 13:30:06 +00:00
provides basic access to whether debugging or verbosity is enabled as well as
the most important core object: The `Store`.
2017-10-14 19:35:55 +00:00
The `Store` type is provided by the libimagstore library, the heart of
everything.
2017-10-14 13:30:06 +00:00
When interacting with the store, two types are visible: `FileLockEntry` and
`Entry` whereas the former derefs to the latter, which basically means that the
former wraps the latter. The `FileLockEntry` is a necessary wrapper for
ensuring that when working concurrently with the store, an entry is only
_borrowed_ once from the store. It also ensures that the object is alive as long
as the store is.
The `Entry` type provides functionality like reading the actual content, its
header and so on. Extensions for its functionality are implemented on this type,
not on the `FileLockEntry`.
The `Entry` provides access to its header, which is a `toml::Value`, where toml
2017-10-14 19:35:55 +00:00
is the toml-rs crate (external project). Convenience functionality is provided
2017-10-14 13:30:06 +00:00
via the `toml-query` crate, which is an external project which was initiated and
extracted from the imag project.
2017-10-14 19:35:55 +00:00
Error types are also important.
All errors in imag projects should be created with `error-chain`.
libimagerror provides functionality to enhance the experience with `Result`
types and general tracing of errors.