Skip to content

Commit

Permalink
feat: add account access method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilerd committed Oct 8, 2023
1 parent dd224bf commit 8fe8b00
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
16 changes: 16 additions & 0 deletions bindings/python/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


use pyo3::prelude::*;

#[pyclass]
#[derive(Hash, Eq, PartialEq)]
pub struct Account(pub zhang_ast::Account);

#[pymethods]
impl Account {

#[getter]
pub fn name(&self) -> String {
self.0.content.clone()

Check warning on line 14 in bindings/python/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/ast.rs#L13-L14

Added lines #L13 - L14 were not covered by tests
}
}
35 changes: 35 additions & 0 deletions bindings/python/src/domain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use pyo3::prelude::*;
use pyo3::types::PyDateTime;

#[pyclass]
pub struct AccountDomain(pub zhang_core::domains::schemas::AccountDomain);



#[pymethods]
impl AccountDomain {

#[getter]
pub fn datetime(&self) -> i64 {
self.0.date.timestamp()

Check warning on line 14 in bindings/python/src/domain.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/domain.rs#L13-L14

Added lines #L13 - L14 were not covered by tests
}
#[getter]
pub fn r#type(&self) -> String {
self.0.r#type.to_string()

Check warning on line 18 in bindings/python/src/domain.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/domain.rs#L17-L18

Added lines #L17 - L18 were not covered by tests
}

#[getter]
pub fn name(&self) -> String {
self.0.name.clone()

Check warning on line 23 in bindings/python/src/domain.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/domain.rs#L22-L23

Added lines #L22 - L23 were not covered by tests
}
#[getter]
pub fn status(&self) -> &str {
self.0.status.as_ref()

Check warning on line 27 in bindings/python/src/domain.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/domain.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
}

#[getter]
pub fn alias(&self) -> Option<String> {
self.0.alias.clone()

Check warning on line 32 in bindings/python/src/domain.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/domain.rs#L31-L32

Added lines #L31 - L32 were not covered by tests
}

}
41 changes: 26 additions & 15 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
use pyo3::prelude::*;
use std::collections::HashMap;
use std::env::temp_dir;
use std::path::PathBuf;
use std::str::FromStr;
use pyo3::prelude::*;
use zhang_ast::{Spanned};

use zhang_core::text::parser::parse;
use zhang_ast::Spanned;
use zhang_core::text::transformer::TextTransformer;

/// Formats the sum of two numbers as string.

pub mod ast;
pub mod domain;
#[pyclass]
pub struct Directive(Spanned<zhang_ast::Directive>);


#[pyclass]
pub struct Ledger(zhang_core::ledger::Ledger);

#[pymethods]
impl Ledger {

#[new]
pub fn new(path: &str, endpoint: &str) -> PyResult<Self> {
let pathbuf = PathBuf::from_str(path)?;
Ok(Ledger(zhang_core::ledger::Ledger::load::<TextTransformer>(pathbuf, endpoint.to_owned()).unwrap()))
Ok(Ledger(
zhang_core::ledger::Ledger::load::<TextTransformer>(pathbuf, endpoint.to_owned()).unwrap(),

Check warning on line 23 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L20-L23

Added lines #L20 - L23 were not covered by tests
))
}

#[staticmethod]
pub fn from_str(content: &str) -> PyResult<Self> {
let t_dir = temp_dir();
let endpoint = t_dir.join("main.zhang");
std::fs::write( endpoint, content)?;
Ok(Ledger(zhang_core::ledger::Ledger::load::<TextTransformer>(t_dir, "main.zhang".to_owned()).unwrap()))
std::fs::write(endpoint, content)?;
Ok(Ledger(
zhang_core::ledger::Ledger::load::<TextTransformer>(t_dir, "main.zhang".to_owned()).unwrap(),

Check warning on line 33 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L28-L33

Added lines #L28 - L33 were not covered by tests
))
}

#[getter]
pub fn options(&self) -> PyResult<HashMap<String, String>> {
let store = self.0.store.read().unwrap();
Ok(store.options.clone())

Check warning on line 40 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L38-L40

Added lines #L38 - L40 were not covered by tests
}
}



#[getter]
pub fn accounts(&self) -> PyResult<HashMap<ast::Account, domain::AccountDomain>> {
let store = self.0.store.read().unwrap();
Ok(store

Check warning on line 46 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
.accounts
.clone()
.into_iter()
.map(|(key, value)| (ast::Account(key), domain::AccountDomain(value)))

Check warning on line 50 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L50

Added line #L50 was not covered by tests
.collect())
}
}

/// A Python module implemented in Rust.
#[pymodule]
fn zhang(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Ledger>()?;
m.add_class::<ast::Account>()?;
m.add_class::<domain::AccountDomain>()?;
Ok(())

Check warning on line 61 in bindings/python/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

bindings/python/src/lib.rs#L56-L61

Added lines #L56 - L61 were not covered by tests
}
}
4 changes: 3 additions & 1 deletion bindings/python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
print("loading examples/example.zhang")
ledger = Ledger("../../examples", "example.zhang")
print("printing options")
pprint(ledger.options())
pprint(ledger.options)
print("printing accounts info")
pprint(ledger.accounts)

0 comments on commit 8fe8b00

Please sign in to comment.