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
use alloc::borrow::Cow;
use core::{
    fmt::{self, Display, Formatter},
    iter::FromIterator,
    ops::Deref,
};

/// A description of everything a particular proc block is capable of.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ProcBlockDescriptor<'a> {
    /// The name for this proc block's type.
    pub type_name: Cow<'a, str>,
    /// A human-friendly description of what this proc block does.
    ///
    /// Similar to how Rust types are normally documented, this should consist
    /// of a short one-line summary with more information in subsequent
    /// paragraphs.
    pub description: Cow<'a, str>,
    pub available_transforms: Cow<'a, [TransformDescriptor<'a>]>,
}

impl<'a> ProcBlockDescriptor<'a> {
    pub const CUSTOM_SECTION_NAME: &'static str = ".rune_proc_block";
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TransformDescriptor<'a> {
    pub inputs: TensorDescriptors<'a>,
    pub outputs: TensorDescriptors<'a>,
}

#[derive(
    Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize,
)]
pub struct TensorDescriptors<'a>(pub Cow<'a, [TensorDescriptor<'a>]>);

impl<'a> FromIterator<TensorDescriptor<'a>> for TensorDescriptors<'a> {
    fn from_iter<T: IntoIterator<Item = TensorDescriptor<'a>>>(
        iter: T,
    ) -> Self {
        TensorDescriptors(iter.into_iter().collect())
    }
}

impl<'a> From<TensorDescriptor<'a>> for TensorDescriptors<'a> {
    fn from(t: TensorDescriptor<'a>) -> Self {
        TensorDescriptors(Cow::Owned(alloc::vec![t]))
    }
}

impl<'a> Deref for TensorDescriptors<'a> {
    type Target = Cow<'a, [TensorDescriptor<'a>]>;

    fn deref(&self) -> &Self::Target { &self.0 }
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TensorDescriptor<'a> {
    pub element_type: hotg_rune_core::ElementType,
    pub dimensions: Dimensions<'a>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Dimensions<'a> {
    Finite(Cow<'a, [Dimension]>),
    Arbitrary,
}

impl<'a> Display for Dimensions<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Dimensions::Arbitrary => write!(f, ".."),
            Dimensions::Finite(dimensions) => {
                for (i, dimension) in dimensions.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", dimension)?;
                }

                Ok(())
            },
        }
    }
}

impl<'a, D: Into<Cow<'a, [Dimension]>>> From<D> for Dimensions<'a> {
    fn from(dims: D) -> Self { Dimensions::Finite(dims.into()) }
}

#[derive(
    Debug, Copy, Clone, PartialEq, serde::Serialize, serde::Deserialize,
)]
pub enum Dimension {
    Any,
    Value(usize),
}

impl Display for Dimension {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Dimension::Any => write!(f, "_"),
            Dimension::Value(dim) => write!(f, "{}", dim),
        }
    }
}