-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
#[getter] | ||
pub fn r#type(&self) -> String { | ||
self.0.r#type.to_string() | ||
} | ||
|
||
#[getter] | ||
pub fn name(&self) -> String { | ||
self.0.name.clone() | ||
} | ||
#[getter] | ||
pub fn status(&self) -> &str { | ||
self.0.status.as_ref() | ||
} | ||
|
||
#[getter] | ||
pub fn alias(&self) -> Option<String> { | ||
self.0.alias.clone() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
)) | ||
} | ||
|
||
#[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(), | ||
)) | ||
} | ||
|
||
#[getter] | ||
pub fn options(&self) -> PyResult<HashMap<String, String>> { | ||
let store = self.0.store.read().unwrap(); | ||
Ok(store.options.clone()) | ||
} | ||
} | ||
|
||
|
||
|
||
#[getter] | ||
pub fn accounts(&self) -> PyResult<HashMap<ast::Account, domain::AccountDomain>> { | ||
let store = self.0.store.read().unwrap(); | ||
Ok(store | ||
.accounts | ||
.clone() | ||
.into_iter() | ||
.map(|(key, value)| (ast::Account(key), domain::AccountDomain(value))) | ||
.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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters