use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum FlowInputLocation {
InputForOutput {
output_index: usize,
input_index: usize,
},
MemoryAddress {
memory_index: usize,
input_index: usize,
},
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum FlowOutputLocation {
Output(usize),
MemoryAccess(usize),
}
impl FlowOutputLocation {
pub fn input(&self, input_index: usize) -> FlowInputLocation {
match *self {
FlowOutputLocation::Output(output_index) => FlowInputLocation::InputForOutput {
output_index,
input_index,
},
FlowOutputLocation::MemoryAccess(memory_index) => FlowInputLocation::MemoryAddress {
memory_index,
input_index,
},
}
}
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum FlowValueLocation {
Output(usize),
InputForOutput {
output_index: usize,
input_index: usize,
},
MemoryAddress {
memory_index: usize,
input_index: usize,
},
}
impl FlowValueLocation {
pub fn is_address(&self) -> bool {
matches!(self, FlowValueLocation::MemoryAddress { .. })
}
pub fn is_in_output(&self, index_to_find: usize) -> bool {
match *self {
FlowValueLocation::Output(output_index) => output_index == index_to_find,
FlowValueLocation::InputForOutput {
output_index, ..
} => output_index == index_to_find,
FlowValueLocation::MemoryAddress {
..
} => false,
}
}
}
impl From<FlowInputLocation> for FlowValueLocation {
fn from(location: FlowInputLocation) -> Self {
match location {
FlowInputLocation::InputForOutput {
output_index,
input_index,
} => FlowValueLocation::InputForOutput {
output_index,
input_index,
},
FlowInputLocation::MemoryAddress {
memory_index,
input_index,
} => FlowValueLocation::MemoryAddress {
memory_index,
input_index,
},
}
}
}
impl TryFrom<FlowValueLocation> for FlowInputLocation {
type Error = ();
fn try_from(location: FlowValueLocation) -> Result<Self, ()> {
Ok(match location {
FlowValueLocation::Output(_) => return Err(()),
FlowValueLocation::InputForOutput {
output_index,
input_index,
} => FlowInputLocation::InputForOutput {
output_index,
input_index,
},
FlowValueLocation::MemoryAddress {
memory_index,
input_index,
} => FlowInputLocation::MemoryAddress {
memory_index,
input_index,
},
})
}
}
impl PartialEq<FlowValueLocation> for FlowInputLocation {
fn eq(&self, other: &FlowValueLocation) -> bool {
&FlowValueLocation::from(*self) == other
}
}