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
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate mech_core;
extern crate hashbrown;

use hashbrown::HashMap;
use mech_core::{Table, Value, Aliases, Transaction, Interner, TableId, Core, Constraint, Register};

// ## Client Message

#[derive(Serialize, Deserialize, Debug)]
pub enum WebsocketMessage {
  Listening(Register),
  Control(u8),
  Code(MechCode),
  Table(NetworkTable),
  RemoveBlock(usize),
  Transaction(Transaction),
}

// Run loop messages are sent to the run loop from the client

#[derive(Debug, Clone)]
pub enum RunLoopMessage {
  Stop,
  StepBack,
  StepForward,
  Pause,
  Resume,
  Clear,
  PrintCore(Option<u64>),
  PrintRuntime,
  Listening(Register),
  Table(NetworkTable),
  GetTable(u64),
  Transaction(Transaction),
  Code((u64,MechCode)),
  EchoCode(String),
  Blocks(Vec<MiniBlock>),
  //Core(Core),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MiniBlock {
  pub constraints: Vec<(String, Vec<Constraint>)>,
}

impl MiniBlock {
  
  pub fn new() -> MiniBlock { 
    MiniBlock {
      constraints: Vec::with_capacity(1),
    }
  }

}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MechCode {
  String(String),
  MiniBlocks(Vec<MiniBlock>),
}

// TODO This is a kludge to get around having to write a serialize method for
// hashmaps.... fix this!
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NetworkTable {
  pub id: u64,
  pub rows: u64,
  pub columns: u64,
  pub column_aliases: Vec<(u64, u64)>,
  pub column_index_to_alias: Vec<Option<u64>>, 
  pub row_aliases: Vec<(u64, u64)>,
  pub data: Vec<Vec<Value>>,
}

impl NetworkTable {
  pub fn new(table: &Table) -> NetworkTable {
    let mut column_aliases: Vec<(u64,u64)> = Vec::new(); 
    for (k,v) in table.column_aliases.iter() {
      column_aliases.push((k.clone(),v.clone()));
    };
    let mut row_aliases: Vec<(u64,u64)> = Vec::new(); 
    for (k,v) in table.row_aliases.iter() {
      row_aliases.push((k.clone(),v.clone()));
    };
    NetworkTable {
      id: table.id.clone(),
      rows: table.rows.clone(),
      columns: table.columns.clone(),
      column_aliases,
      column_index_to_alias: table.column_index_to_alias.clone(),
      row_aliases,
      data: table.data.clone(),
    }
  }

  pub fn to_table(&mut self) -> Table {
    let mut column_aliases = Aliases::new(); 
    for (k,v) in self.column_aliases.iter() {
      column_aliases.insert(k.clone(),v.clone());
    };
    let mut row_aliases: HashMap<u64,u64> = HashMap::new(); 
    for (k,v) in self.row_aliases.iter() {
      row_aliases.insert(k.clone(),v.clone());
    };
    Table {
      id: self.id.clone(),
      rows: self.rows.clone(),
      columns: self.columns.clone(),
      column_aliases,
      column_index_to_alias: self.column_index_to_alias.clone(),
      row_aliases: row_aliases,
      data: self.data.clone(),
    }
  }


}