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 142 143 144 145 146 147 148 149 150
//! Module for types represented by the structure ID field of a SWC sample.
//!
//! Contains some known structure schemas and macros for creating more.
use std::collections::HashSet;
use std::convert::TryFrom;
use std::fmt::Debug;
/// Trait for the structures represented by samples, identified by an integer.
pub trait StructureIdentifier: Copy + Clone + Debug + TryFrom<isize> + Into<isize> {
/// If the structure field allows for any value, return `None`;
/// otherwise, return `Some(HashSet<allowed_values>)`.
/// `no_catchall`, if `true`, ignores the "catch-all" variant
/// in determining what is allowed, if present.
fn allowed_values(no_catchall: bool) -> Option<HashSet<isize>>;
}
/// Macro for creating a public enum implementing StructureIdentifier.
///
/// The first argument is the enum's identifier.
/// Subsequent arguments are given in the form `value = variant`:
/// that is, a signed integer literal, then an equals sign, then
/// the identifier of the enum variant that literal represents.
/// There is an optional final argument which is the identifier of a variant
/// which catches all other values, and stores the value originally given to it.
///
/// If the final argument is given, the enum implements From<isize>:
/// otherwise, it only implements TryFrom<isize>,
/// returning an error which contains the offending integer.
#[macro_export]
macro_rules! structure_mapping {
( $id:ident $(, $val:literal = $name:ident )* $(,)?) => {
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum $id {
$( $name, )+
}
impl TryFrom<isize> for $id {
type Error = isize;
fn try_from(val: isize) -> Result<Self, Self::Error> {
match val {
$( $val => Ok(Self::$name), )*
value => Err(value),
}
}
}
impl From<$id> for isize {
fn from(val: $id) -> Self {
match val {
$( $id::$name => $val, )*
}
}
}
impl StructureIdentifier for $id {
fn allowed_values(_no_catchall: bool) -> Option<HashSet<isize>> {
Some(vec![$( $val, )*].into_iter().collect())
}
}
};
( $id:ident $(, $val:literal = $name:ident )*, $othername:ident $(,)?) => {
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum $id {
$( $name, )*
$othername(isize),
}
impl From<isize> for $id {
fn from(val: isize) -> Self {
match val {
$( $val => Self::$name, )*
x => Self::$othername(x),
}
}
}
impl From<$id> for isize {
fn from(val: $id) -> Self {
match val {
$( $id::$name => $val, )*
$id::$othername(x) => x,
}
}
}
impl StructureIdentifier for $id {
fn allowed_values(no_catchall: bool) -> Option<HashSet<isize>> {
if no_catchall {
Some(vec![$( $val, )*].into_iter().collect())
} else {
None
}
}
}
};
}
/// Thin wrapper around the `structure_mapping` macro for producing enums
/// which extend the Neuromorpho SWC standard (i.e. have additional structure types in the `Custom` block, 5+).
///
/// Again, you can either allow or disallow the catch-all case.
#[macro_export]
macro_rules! neuromorpho_ext {
( $id:ident $(, $val:literal = $name:ident )* $(,)?) => {
structure_mapping!(
$id,
-1 = Root,
0 = Undefined,
1 = Soma,
2 = Axon,
3 = BasalDendrite,
4 = ApicalDendrite,
$($val = $name, )*
);
};
( $id:ident $(, $val:literal = $name:ident )*, $othername:ident $(,)?) => {
structure_mapping!(
$id,
-1 = Root,
0 = Undefined,
1 = Soma,
2 = Axon,
3 = BasalDendrite,
4 = ApicalDendrite,
$($val = $name, )*
$othername,
);
};
}
neuromorpho_ext!(NeuromorphoStructure, Custom);
neuromorpho_ext!(CnicStructure, 5 = ForkPoint, 6 = EndPoint, 7 = Custom);
neuromorpho_ext!(
NavisStructure,
5 = ForkPoint,
6 = EndPoint,
7 = Presynapse,
8 = Postsynapse,
9 = PreAndPostsynapse,
Custom,
);
neuromorpho_ext!(VnedStructure, 10 = SomaRelated, Custom);
structure_mapping!(GulyasStructure, IsoDiameterStructure);
structure_mapping!(AnyStructure, Any);