Add type synonyms for Uuids

closes 58
embed
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;
type CardId = Uuid;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct AddressBook {
display_name: String,
created: DateTime<Local>,
last_edited: DateTime<Local>,
cards: FnvHashMap<Uuid, Card>
cards: FnvHashMap<CardId, Card>
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Card {
uuid: Uuid,
id: CardId,
title: String,
firstname: String,
lastname: String,
@ -62,20 +64,20 @@ impl AddressBook {
}
}
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) {
self.cards.remove(&card_uuid);
pub fn remove_card(&mut self, card_id: CardId) {
self.cards.remove(&card_id);
}
pub fn card_exists(&self, card_uuid: Uuid) -> bool {
self.cards.contains_key(&card_uuid)
pub fn card_exists(&self, card_id: CardId) -> bool {
self.cards.contains_key(&card_id)
}
}
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
}
}
@ -84,7 +86,7 @@ impl Deref for AddressBook {
impl Card {
pub fn new() -> Card {
Card {
uuid: Uuid::new_v4(),
id: Uuid::new_v4(),
title: String::new(),
firstname: String::new(),
lastname: String::new(),
@ -103,8 +105,8 @@ impl Card {
}
}
pub fn uuid(&self) -> &Uuid {
&self.uuid
pub fn id(&self) -> &CardId {
&self.id
}
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_RIGHT: char = '╚';
type EntityId = Uuid;
/// `Entity` is a container for Components.
#[derive(Debug)]
pub struct Entity {
id: Uuid,
id: EntityId,
pub component: Box<Component>, // more than one?
}
@ -128,7 +132,7 @@ impl Deref for Entity {
}
impl Entity {
pub fn uuid(&self) -> &Uuid {
pub fn id(&self) -> &EntityId {
&self.id
}
/// Pass events to child component.
@ -147,8 +151,8 @@ pub trait Component: Display + Debug {
true
}
fn set_dirty(&mut self);
fn kill(&mut self, uuid: Uuid) {}
fn set_id(&mut self, uuid: Uuid) {}
fn kill(&mut self, id: EntityId) {}
fn set_id(&mut self, id: EntityId) {}
}
/*

View File

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

View File

@ -570,7 +570,7 @@ impl Component for Composer {
},
Cursor::To | Cursor::Cc | Cursor::Bcc => {
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));
},
Cursor::Attachments => {

View File

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