1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! # aflak - Computational mAKE
//!
//! A crate to manage a graph of interdependent functions.
extern crate rayon;

extern crate boow;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate variant_name;

mod dst;
mod export;
mod transform;

pub use dst::*;
pub use export::*;
pub use transform::*;

pub use self::variant_name::VariantName;

/// Trait to define a default value for each variant of an enumeration.
pub trait DefaultFor: VariantName {
    fn default_for(variant_name: &str) -> Self;
}

/// Trait to discriminate editable variants from constant variants of an
/// enumeration.
///
/// Especially used for a node editor.
pub trait EditableVariants: VariantName {
    /// Get list of editable variants.
    fn editable_variants() -> &'static [&'static str];
    /// Check if given variant is editable or not.
    fn editable(variant_name: &str) -> bool {
        Self::editable_variants().contains(&variant_name)
    }
}

/// Make it easier to define a function used for a transform. Used internally
/// by [`cake_transform`]. You probably want to directly use [`cake_transform`].
#[doc(hidden)]
#[macro_export]
macro_rules! cake_fn {
    // Special case where no argument is provided
    ($fn_name: ident<$enum_name: ident, $err_type: ty>() $fn_block: block) => {
        fn $fn_name(
            _: Vec<::std::borrow::Cow<$enum_name>>,
        ) -> Vec<Result<$enum_name, $err_type>> {
            $fn_block
        }
    };
    // Standard case
    ($fn_name: ident<$enum_name: ident, $err_type: ty>($($x: ident: $x_type: ident),*) $fn_block: block) => {
        fn $fn_name(
            input: Vec<::std::borrow::Cow<$enum_name>>,
        ) -> Vec<Result<$enum_name, $err_type>> {
            #[allow(non_camel_case_types)]
            enum Args { $($x,)* }
            if let ($(&$enum_name::$x_type(ref $x), )*) = ($(&*input[Args::$x as usize], )*) {
                $fn_block
            } else {
                panic!("Unexpected argument!")
            }
        }
    };
}

/// Create a new transform from a rust function.
///
/// # Example
///
/// ```rust
/// #[macro_use] extern crate variant_name_derive;
/// #[macro_use] extern crate aflak_cake;
/// use aflak_cake::*;
///
/// #[derive(Clone, PartialEq, Debug, VariantName)]
/// pub enum AlgoIO {
///     Integer(u64),
///     Image2d(Vec<Vec<f64>>),
/// }
///
/// pub enum E {}
///
/// //                                                        Error type   Input arguments    Output types
/// //      key identifying transformation  In/Out types_____/  __________/   _______________/
/// //                                   \       |     /       /             /
/// let plus_one_trans = cake_transform!(plus1<AlgoIO, E>(i: Integer = 0) -> Integer {
///     // Define the body of the transformation.                      \
///     // Must return a Vec<Result<AlgoIO, !>>!                        ~~ Default value (optional)
///     vec![Ok(AlgoIO::Integer(i + 1))]
/// });
/// ```
#[macro_export]
macro_rules! cake_transform {
    ($fn_name: ident<$enum_name: ident, $err_type: ty>($($x: ident: $x_type: ident $(= $x_default_val: expr), *),*) -> $($out_type: ident),* $fn_block: block) => {{
        cake_fn!{$fn_name<$enum_name, $err_type>($($x: $x_type),*) $fn_block}
        $crate::Transformation {
            name: stringify!($fn_name),
            input: vec![$((stringify!($x_type), {
                cake_some_first_value!($( $enum_name::$x_type($x_default_val) ),*)
            }), )*],
            output: vec![$(stringify!($out_type), )*],
            algorithm: $crate::Algorithm::Function($fn_name),
        }
    }};
}

/// Make a constant.
///
/// Subject for deprecation.
/// You'd probably better use [`Transformation::new_constant`].
#[macro_export]
macro_rules! cake_constant {
    ($const_name: ident, $($x: expr),*) => {{
        use $crate::VariantName;
        let constant = vec![$($x, )*];
        $crate::Transformation {
            name: stringify!($const_name),
            input: vec![],
            output: constant.iter().map(|c| c.variant_name()).collect(),
            algorithm: $crate::Algorithm::Constant(constant),
        }
    }};
}

/// Helper macro for internal use.
#[doc(hidden)]
#[macro_export]
macro_rules! cake_some_first_value {
    () => {
        None
    };
    ($x:expr) => {
        Some($x)
    };
    ($x:expr, $($xs:expr)+) => {
        compile_error!("Only zero or one value is expected.")
    };
}