ConverterBuilder

Struct ConverterBuilder 

Source
pub struct ConverterBuilder<F>
where F: Fn(Value) -> DataResult<Value> + Send + Sync + 'static,
{ /* private fields */ }
Expand description

Builder that turns a closure into a Converter.

use daedalus_data::convert::{ConverterBuilder, ConverterGraph};
use daedalus_data::errors::{DataError, DataErrorCode};
use daedalus_data::model::{TypeExpr, Value, ValueType};

let conv = ConverterBuilder::new(
    "int_to_string",
    TypeExpr::Scalar(ValueType::Int),
    TypeExpr::Scalar(ValueType::String),
    |v| match v {
        Value::Int(i) => Ok(Value::String(i.to_string().into())),
        _ => Err(DataError::new(DataErrorCode::InvalidType, "expected int")),
    },
)
.cost(2u64)
.feature_flag("fmt")
.build_boxed();

let mut graph = ConverterGraph::new();
graph.register(conv);
let res = graph
    .resolve_with_context(
        &TypeExpr::Scalar(ValueType::Int),
        &TypeExpr::Scalar(ValueType::String),
        &["fmt".into()],
        true,
    )
    .expect("resolution");
assert_eq!(res.provenance.steps.len(), 1);

Implementations§

Source§

impl<F> ConverterBuilder<F>
where F: Fn(Value) -> DataResult<Value> + Send + Sync + 'static,

Source

pub fn new( id: impl Into<String>, input: TypeExpr, output: TypeExpr, func: F, ) -> Self

Source

pub fn cost(self, cost: u64) -> Self

Source

pub fn feature_flag(self, flag: impl Into<String>) -> Self

Source

pub fn requires_gpu(self, requires: bool) -> Self

Source

pub fn build(self) -> FnConverter<F>

Source

pub fn build_boxed(self) -> Box<dyn Converter>

Auto Trait Implementations§

§

impl<F> Freeze for ConverterBuilder<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for ConverterBuilder<F>
where F: RefUnwindSafe,

§

impl<F> Send for ConverterBuilder<F>

§

impl<F> Sync for ConverterBuilder<F>

§

impl<F> Unpin for ConverterBuilder<F>
where F: Unpin,

§

impl<F> UnwindSafe for ConverterBuilder<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.