Skip to main content

Crate velomorph

Crate velomorph 

Source
Expand description

Core runtime library for Velomorph.

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.

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()?;

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.