Skip to main content

Crate velomorph

Crate velomorph 

Source
Expand description

Core runtime library for Velomorph.

Version 1.0 stabilizes the public API (Morph, TryMorph, MorphError, and optional Janitor when the janitor feature is enabled) for semver-compatible evolution within the 1.x line.

This crate provides:

  • Morph, a derive macro re-export for generating transformations.
  • TryMorph, the trait implemented by generated code.
  • Janitor (feature: janitor), an optional background deallocation helper.
  • Blanket list mapping: TryMorph<Vec<U>> for Vec<T> when T: TryMorph<U>.

Notes:

  • Derive behavior is generated by velomorph-derive.
  • Supported derive controls include: from, with, default, skip, validate, and enum variant remapping.
  • Janitor is available to user code and to generated implementations via the try_morph(self, &Janitor) signature when the janitor feature is enabled.

§Quick Example

use std::borrow::Cow;
use uuid::Uuid;
use velomorph::{Morph, TryMorph};
#[cfg(feature = "janitor")]
use velomorph::Janitor;

pub struct RawInput<'a> {
    // Legacy/external names from an upstream system:
    pub uuid_v4: Option<Uuid>,
    pub user_str: &'a str,
    pub payload: Option<Vec<u8>>,
}

#[derive(Morph, Debug)]
pub struct Event<'a> {
    // Rename `uuid_v4` -> `id` while enforcing presence:
    #[morph(from = "uuid_v4")]
    pub id: Uuid,

    // Rename `user_str` -> `username` while borrowing the string:
    #[morph(from = "user_str")]
    pub username: Cow<'a, str>,
}

#[cfg(feature = "janitor")]
let janitor = Janitor::new();
let raw = RawInput {
    uuid_v4: Some(Uuid::new_v4()),
    user_str: "edge-a",
    payload: Some(vec![1, 2, 3]),
};
#[cfg(feature = "janitor")]
let event: Event = raw.try_morph(&janitor)?;
#[cfg(not(feature = "janitor"))]
let event: Event = raw.try_morph()?;

§List Mapping

You can morph whole vectors when each item supports TryMorph:

use velomorph::TryMorph;
let input: Vec<Source> = vec![Source, Source];
let output: Vec<Target> = input.try_morph()?;

With janitor enabled, the same mapping accepts &Janitor:

use velomorph::{Janitor, TryMorph};
let janitor = Janitor::new();
let input: Vec<Source> = vec![Source, Source];
let output: Vec<Target> = input.try_morph(&janitor)?;

Structs§

Janitor
Background deallocation helper that minimizes latency spikes.

Enums§

MorphError
Errors that can occur while transforming an input type into a target type.

Traits§

TryMorph
Trait for fallible transformations from a source type to Target.

Derive Macros§

Morph
Derive macro used to generate TryMorph implementations. Derives a velomorph::TryMorph<Target> implementation.