66 lines
1.4 KiB
Rust
66 lines
1.4 KiB
Rust
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use uuid::Uuid;
|
|
|
|
use crate::expense::Split;
|
|
|
|
pub trait Actor {
|
|
fn spends(&mut self, i: Split);
|
|
fn owes(&mut self, i: Split);
|
|
// name must be unique in a namespace
|
|
fn name(&self) -> &str;
|
|
fn id(&self) -> &Uuid;
|
|
}
|
|
|
|
impl PartialEq for dyn Actor {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.id() == other.id()
|
|
}
|
|
}
|
|
|
|
pub struct Person {
|
|
name: String,
|
|
spent: Vec<Split>,
|
|
owes: Vec<Split>,
|
|
id: Uuid,
|
|
}
|
|
|
|
impl Person {
|
|
pub fn new(name: String, id: Uuid) -> Self {
|
|
let owes = Vec::default();
|
|
let spent = Vec::default();
|
|
|
|
Self {
|
|
name,
|
|
spent,
|
|
owes,
|
|
id,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Actor for Person {
|
|
fn spends(&mut self, split: Split) {
|
|
if split.purchase.paid_by().name() == self.name() {
|
|
self.spent.push(split);
|
|
} else {
|
|
unimplemented!("Raise error: Actor didn't pay for purchase")
|
|
}
|
|
}
|
|
fn owes(&mut self, split: Split) {
|
|
if split.purchase.paid_by().name() == self.name() {
|
|
unimplemented!("Raise error: Actor didn't paid for purchase; can't own money")
|
|
} else {
|
|
self.owes.push(split);
|
|
}
|
|
}
|
|
fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
fn id(&self) -> &Uuid {
|
|
&self.id
|
|
}
|
|
}
|