Skip to content

Commit

Permalink
initializing the master account
Browse files Browse the repository at this point in the history
  • Loading branch information
alensiljak committed Aug 9, 2023
1 parent f24c531 commit 0f68469
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
16 changes: 11 additions & 5 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ use crate::{
#[derive(Debug, PartialEq)]
pub struct Account {
pub(crate) parent: *const Account,
// pub parent_index: Option<AccountIndex>,
pub name: String,
// note
// depth
pub accounts: HashMap<String, *const Account>,
// pub posts: Vec<Post>,
/// indices of Posts in the Journal.Posts array.
// pub post_indices: Vec<PostIndex>,
pub posts: Vec<*const Post>,
// deferred posts
// value_expr
Expand Down Expand Up @@ -56,7 +52,17 @@ impl Account {
}

pub fn find_account(&self, name: &str) -> Option<*const Account> {
Some(*self.accounts.get(name).unwrap())
self.find_or_create(name, true)
}

/// The variant with all the parameters.
/// account_t * find_account(const string& name, bool auto_create = true);
pub fn find_or_create(&self, name: &str, auto_create: bool) -> Option<*const Account> {
if let Some(found) = self.accounts.get(name) {
return Some(*found);
}

todo!("create")
}

pub fn get_account(&self, acct_ptr: *const Account) -> &Account {
Expand Down
18 changes: 10 additions & 8 deletions src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ impl Journal {
// sources: Vec<fileinfo?>
};

// Add master account
j.add_account(Account::new(""));
// Create master account
let master = j.add_account(Account::new(""));
j.master = master;

j
}
Expand Down Expand Up @@ -103,22 +104,22 @@ impl Journal {

// todo: expand_aliases

let account_ptr = self.create_sub_account(std::ptr::null(), name, true);
let account_ptr = self.create_sub_account(self.master, name, true);

// todo: add any validity checks here.

account_ptr
}

pub fn find_account(&self, name: &str) -> Option<&Account> {
let Some(index) = self.find_account_index(name)
let Some(ptr) = self.find_account_index(name)
else {return None};

Some(self.get_account(index))
Some(self.get_account(ptr))
}

pub fn find_account_index(&self, name: &str) -> Option<*const Account> {
self.find_sub_account(std::ptr::null(), name)
self.find_sub_account(self.master, name)
}

/// Finds account by full name.
Expand Down Expand Up @@ -171,10 +172,10 @@ impl Journal {
acct_name: &str,
auto_create: bool,
) -> Option<*const Account> {
// let parent = self.accounts.get(root_ptr).unwrap();
let parent = self.get_account(root_ptr);
if parent.accounts.contains_key(acct_name) {
return Some(*parent.accounts.get(acct_name).unwrap());
let ptr = parent.accounts.get(acct_name).unwrap();
return Some(*ptr);
}

// if not found, try to break down
Expand Down Expand Up @@ -320,6 +321,7 @@ mod tests {
let actual = j.get_master_account();

assert_eq!("", actual.name);
assert_ne!(std::ptr::null(), actual);
}

#[test]
Expand Down
13 changes: 2 additions & 11 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ impl CommodityPool {
self.annotated_commodities.insert(symbol.to_owned(), ann);
}

// let mut_cdty = self.commodities.entry(symbol.to_owned())
// .or_insert(c);

// add to map
// move to map
self.commodities.insert(symbol.to_string(), c);
// get the new variable address.
// get the new address.
let new_commodity = self.commodities.get(symbol).unwrap();

// add to price history graph.
Expand Down Expand Up @@ -109,12 +106,6 @@ impl CommodityPool {
x.unwrap().graph_index
}

pub fn find_or_create_by_name(&self, symbol: &str) {
todo!()
// find
// create
}

/// Finds a commodity with the given symbol, or creates one.
///
pub fn find_or_create(
Expand Down

0 comments on commit 0f68469

Please sign in to comment.