format file containing state module

This commit is contained in:
2022-02-04 13:30:44 +01:00
parent 4f9794442a
commit 841bcd67aa

View File

@@ -1,50 +1,46 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::constants::NONE_STRING_OPTION;
#[derive(Debug, Clone)]
pub struct State{
pub struct State {
stacks: Vec<AnnotationZStack>,
stack_index: Option<usize>,
pub image_index: Option<usize>,
}
}
impl State {
impl State {
pub fn new() -> Self {
State{
State {
stacks: Vec::new(),
stack_index: None,
image_index: None,
}
}
pub fn set_image_index(&mut self, image_index : Option<usize>) {
pub fn set_image_index(&mut self, image_index: Option<usize>) {
self.image_index = image_index;
}
pub fn get_current_annotation_image(&self) -> Option<AnnotationImage>{
pub fn get_current_annotation_image(&self) -> Option<AnnotationImage> {
match self.image_index {
Some(image_index) => {
let stack = self.get_current_focus_stack();
match stack {
Some(stack) => {
stack.images.get(image_index).map(|x| x.clone())
},
_ => None
Some(stack) => stack.images.get(image_index).map(|x| x.clone()),
_ => None,
}
}
_ => None
}
}
pub fn get_current_focus_stack(&self) -> Option<&AnnotationZStack> {
match self.stack_index {
Some(stack_index) => {
self.stacks.get(stack_index)
}
_ => None,
}
}
pub fn replace_foucs_stacks(&mut self, mut stacks : Vec<AnnotationZStack>){
pub fn get_current_focus_stack(&self) -> Option<&AnnotationZStack> {
match self.stack_index {
Some(stack_index) => self.stacks.get(stack_index),
_ => None,
}
}
pub fn replace_foucs_stacks(&mut self, mut stacks: Vec<AnnotationZStack>) {
self.stacks.clear();
self.stacks.append(&mut stacks);
eprintln!("{}", stacks.len());
@@ -66,7 +62,7 @@ pub struct State{
pub fn get_current_foucs_stack_best_index(&self) -> Option<usize> {
match self.get_current_focus_stack() {
Some(stack) => stack.best_index,
_ => None
_ => None,
}
}
@@ -74,8 +70,8 @@ pub struct State{
let len = self.stacks.len();
if len == 0 {
self.stack_index = None;
} else if self.stack_index.map_or_else(|| false, |x| {x + 1 < len}) {
self.stack_index = self.stack_index.map(|x| x+1)
} else if self.stack_index.map_or_else(|| false, |x| x + 1 < len) {
self.stack_index = self.stack_index.map(|x| x + 1)
}
eprintln!("{:?}", self.stack_index)
@@ -95,25 +91,24 @@ pub struct State{
if len == 0 {
self.stack_index = None;
} else if self.stack_index.map_or_else(|| false, |x| x > 0) {
self.stack_index = self.stack_index.map(|x| x-1)
self.stack_index = self.stack_index.map(|x| x - 1)
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotationZStack {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotationZStack {
pub images: Vec<AnnotationImage>,
pub best_index: Option<usize>,
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotationImage {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotationImage {
pub image_path: String,
pub neighbours: [Option<String>; 8],
}
}
impl AnnotationImage {
impl AnnotationImage {
pub fn from_vec(image_path: String, neighbours: Vec<Option<String>>) -> Self {
let mut _neighbours = [NONE_STRING_OPTION; 8];
for (index, element) in (0..8).zip(neighbours.iter()) {
@@ -125,4 +120,4 @@ pub struct State{
neighbours: _neighbours,
}
}
}
}