Skip to main content

Module persistence

Module persistence 

Source
Expand description

Drawing persistence – JSON serialization and storage.

This module provides types for serializing drawings to and from JSON. The key design decision is that anchors are stored as (timestamp, price) pairs (StoredCoord) rather than bar indices or screen positions, so drawings survive pan/zoom, resolution changes and history being prepended.

§Conversion flow

Drawing (in-memory)                    StoredDrawing (serializable)
─────────────────                      ────────────────────────────
chart_points: Vec<ChartPoint> ──────►  points: Vec<StoredCoord>
(bar index + price)  drawing_to_stored()  (timestamp + price)
tool type and settings        ──────►  drawing: Drawing

chart_points: Vec<ChartPoint> ◄──────  points: Vec<StoredCoord>
                     stored_to_drawing()

Screen points are not stored: Drawing::update_screen_coords rebuilds them from the anchors on the next frame.

A restored drawing keeps the id, creation time and notes of its record in Drawing::stored_id, created_at and notes, and is saved back under them. Restore drawings through DrawingManager::add_synced_drawing, which refuses a drawing that saving would refuse; the manager then gives a new drawing an id that no restored drawing uses. Two managers restoring the same storage can still hand out the same id: one storage per manager is the supported use.

§Storage format

Drawings are grouped by symbol into a DrawingStorage container that includes a format version, metadata, and an optional timeframe filter. Loading migrates versions 1 to 5, rejects versions it does not know and skips any drawing it cannot read (see from_json).

Structs§

DrawingStorage
Drawing storage for saving/loading drawings
SkippedDrawing
A drawing DrawingStorage::from_json left out because it could not be read or its id was already taken.
StorageMetadata
Storage metadata
StoredCoord
A coordinate stored as a (timestamp, price) pair for persistence.
StoredDrawing
A fully serializable representation of a drawing with all its properties.
StoredPane
A study pane identified by its studies rather than by a chart’s study ids.

Enums§

LoadError
Why drawings were not loaded, by DrawingStorage::from_json, Chart::load_drawings or Chart::paste_drawings, before any drawing changed.
SaveError
Why DrawingStorage::to_json did not save.
SkipReason
Why DrawingStorage::from_json left a drawing out.
StorageError
A failed read or write of automatic drawing storage, from Chart::take_storage_error.

Functions§

drawing_to_stored
Convert a Drawing to StoredDrawing, its anchors mapped from bar index to timestamp, under the drawing’s stored id and creation time when it was restored from storage, and under its id and the current time otherwise.
stored_to_drawing
Convert StoredDrawing back to a completed Drawing with id next_id, its anchors mapped from timestamp to bar index and the record’s id, creation time and notes kept; the screen points follow on the next Drawing::update_screen_coords.