Add type synonyms for Uuids

closes 58
master
Manos Pitsidianakis 2019-02-21 15:44:26 +02:00
parent ba6c7d0d7b
commit bbaf87e345
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
5 changed files with 38 additions and 32 deletions

View File

@ -24,17 +24,19 @@ use fnv::FnvHashMap;
use std::ops::Deref; use std::ops::Deref;
type CardId = Uuid;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct AddressBook { pub struct AddressBook {
display_name: String, display_name: String,
created: DateTime<Local>, created: DateTime<Local>,
last_edited: DateTime<Local>, last_edited: DateTime<Local>,
cards: FnvHashMap<Uuid, Card> cards: FnvHashMap<CardId, Card>
} }
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Card { pub struct Card {
uuid: Uuid, id: CardId,
title: String, title: String,
firstname: String, firstname: String,
lastname: String, lastname: String,
@ -62,20 +64,20 @@ impl AddressBook {
} }
} }
pub fn add_card(&mut self, card: Card) { pub fn add_card(&mut self, card: Card) {
self.cards.insert(card.uuid, card); self.cards.insert(card.id, card);
} }
pub fn remove_card(&mut self, card_uuid: Uuid) { pub fn remove_card(&mut self, card_id: CardId) {
self.cards.remove(&card_uuid); self.cards.remove(&card_id);
} }
pub fn card_exists(&self, card_uuid: Uuid) -> bool { pub fn card_exists(&self, card_id: CardId) -> bool {
self.cards.contains_key(&card_uuid) self.cards.contains_key(&card_id)
} }
} }
impl Deref for AddressBook { impl Deref for AddressBook {
type Target = FnvHashMap<Uuid, Card>; type Target = FnvHashMap<CardId, Card>;
fn deref(&self) -> &FnvHashMap<Uuid, Card> { fn deref(&self) -> &FnvHashMap<CardId, Card> {
&self.cards &self.cards
} }
} }
@ -84,7 +86,7 @@ impl Deref for AddressBook {
impl Card { impl Card {
pub fn new() -> Card { pub fn new() -> Card {
Card { Card {
uuid: Uuid::new_v4(), id: Uuid::new_v4(),
title: String::new(), title: String::new(),
firstname: String::new(), firstname: String::new(),
lastname: String::new(), lastname: String::new(),
@ -103,8 +105,8 @@ impl Card {
} }
} }
pub fn uuid(&self) -> &Uuid { pub fn id(&self) -> &CardId {
&self.uuid &self.id
} }
pub fn title(&self) -> &str { pub fn title(&self) -> &str {

View File

@ -75,10 +75,14 @@ const _DOUBLE_DOWN_AND_LEFT: char = '╗';
const _DOUBLE_UP_AND_LEFT: char = '╝'; const _DOUBLE_UP_AND_LEFT: char = '╝';
const _DOUBLE_UP_AND_RIGHT: char = '╚'; const _DOUBLE_UP_AND_RIGHT: char = '╚';
type EntityId = Uuid;
/// `Entity` is a container for Components. /// `Entity` is a container for Components.
#[derive(Debug)] #[derive(Debug)]
pub struct Entity { pub struct Entity {
id: Uuid, id: EntityId,
pub component: Box<Component>, // more than one? pub component: Box<Component>, // more than one?
} }
@ -128,7 +132,7 @@ impl Deref for Entity {
} }
impl Entity { impl Entity {
pub fn uuid(&self) -> &Uuid { pub fn id(&self) -> &EntityId {
&self.id &self.id
} }
/// Pass events to child component. /// Pass events to child component.
@ -147,8 +151,8 @@ pub trait Component: Display + Debug {
true true
} }
fn set_dirty(&mut self); fn set_dirty(&mut self);
fn kill(&mut self, uuid: Uuid) {} fn kill(&mut self, id: EntityId) {}
fn set_id(&mut self, uuid: Uuid) {} fn set_id(&mut self, id: EntityId) {}
} }
/* /*

View File

@ -5,7 +5,7 @@ const MAX_COLS: usize = 500;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
enum ViewMode { enum ViewMode {
List, List,
View(Uuid), View(EntityId),
} }
#[derive(Debug)] #[derive(Debug)]
@ -16,7 +16,7 @@ pub struct ContactList {
length: usize, length: usize,
content: CellBuffer, content: CellBuffer,
uuid_positions: Vec<Uuid>, id_positions: Vec<EntityId>,
mode: ViewMode, mode: ViewMode,
initialized: bool, initialized: bool,
@ -44,7 +44,7 @@ impl ContactList {
new_cursor_pos: 0, new_cursor_pos: 0,
length: 0, length: 0,
account_pos: 0, account_pos: 0,
uuid_positions: Vec::new(), id_positions: Vec::new(),
mode: ViewMode::List, mode: ViewMode::List,
content, content,
initialized: false, initialized: false,
@ -59,13 +59,13 @@ impl ContactList {
self.content.resize(MAX_COLS, book.len(), Cell::with_char(' ')); self.content.resize(MAX_COLS, book.len(), Cell::with_char(' '));
eprintln!("{:?}", book); eprintln!("{:?}", book);
self.uuid_positions.clear(); self.id_positions.clear();
if self.uuid_positions.capacity() < book.len() { if self.id_positions.capacity() < book.len() {
self.uuid_positions.reserve(book.len()); self.id_positions.reserve(book.len());
} }
for (i, c) in book.values().enumerate() { for (i, c) in book.values().enumerate() {
self.uuid_positions.push(*c.uuid()); self.id_positions.push(*c.id());
write_string_to_grid( write_string_to_grid(
c.email(), c.email(),
@ -109,7 +109,7 @@ impl Component for ContactList {
UIEventType::Input(Key::Char('e')) => { UIEventType::Input(Key::Char('e')) => {
let account = &mut context.accounts[self.account_pos]; let account = &mut context.accounts[self.account_pos];
let book = &mut account.address_book; let book = &mut account.address_book;
let card = book[&self.uuid_positions[self.cursor_pos]].clone(); let card = book[&self.id_positions[self.cursor_pos]].clone();
let mut manager = ContactManager::default(); let mut manager = ContactManager::default();
manager.card = card; manager.card = card;
@ -117,7 +117,7 @@ impl Component for ContactList {
let entity = Entity::from(Box::new(manager)); let entity = Entity::from(Box::new(manager));
self.mode = ViewMode::View(*entity.uuid()); self.mode = ViewMode::View(*entity.id());
self.view = Some(entity); self.view = Some(entity);
self.set_dirty(); self.set_dirty();

View File

@ -570,7 +570,7 @@ impl Component for Composer {
}, },
Cursor::To | Cursor::Cc | Cursor::Bcc => { Cursor::To | Cursor::Cc | Cursor::Bcc => {
let account = &context.accounts[self.account_cursor]; let account = &context.accounts[self.account_cursor];
let mut entries = account.address_book.values().map(|v| (v.uuid().as_bytes().to_vec(), v.email().to_string())).collect(); let mut entries = account.address_book.values().map(|v| (v.id().as_bytes().to_vec(), v.email().to_string())).collect();
self.mode = ViewMode::Selector(Selector::new(entries, true)); self.mode = ViewMode::Selector(Selector::new(entries, true));
}, },
Cursor::Attachments => { Cursor::Attachments => {

View File

@ -808,20 +808,20 @@ impl Component for Tabbed {
return true; return true;
} }
UIEventType::Action(Tab(Close)) => { UIEventType::Action(Tab(Close)) => {
let uuid = *self.children[self.cursor_pos].uuid(); let id = *self.children[self.cursor_pos].id();
self.children[self.cursor_pos].kill(uuid); self.children[self.cursor_pos].kill(id);
return true; return true;
} }
UIEventType::Action(Tab(Kill(ref uuid))) => { UIEventType::Action(Tab(Kill(ref id))) => {
if let Some(c_idx) = self.children.iter().position(|x| x.uuid() == uuid) { if let Some(c_idx) = self.children.iter().position(|x| x.id() == id) {
self.children.remove(c_idx); self.children.remove(c_idx);
self.cursor_pos = self.cursor_pos.saturating_sub(1); self.cursor_pos = self.cursor_pos.saturating_sub(1);
self.set_dirty(); self.set_dirty();
return true; return true;
} else { } else {
eprintln!( eprintln!(
"DEBUG: Child entity with uuid {:?} not found.\nList: {:?}", "DEBUG: Child entity with id {:?} not found.\nList: {:?}",
uuid, self.children id, self.children
); );
} }
} }