-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'RAGIB/price-data' into tep/tep-1015-price-data
GH-40 Signed-off-by: 35V LG84 <[email protected]>
- Loading branch information
Showing
9 changed files
with
271 additions
and
10 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
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
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,49 @@ | ||
/* | ||
* Copyright 2024-2025 E257.FI and Muhammad Ragib Hasin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
use rust_decimal::Decimal; | ||
use std::{fmt::Write, sync::Arc}; | ||
use time::OffsetDateTime; | ||
use winnow::{seq, PResult, Parser}; | ||
|
||
use crate::parser::parts::timestamp::parse_timestamp; | ||
// use crate::parser::parts::txn_comment::parse_txn_comment; | ||
// use crate::parser::parts::txn_header_code::parse_txn_code; | ||
// use crate::parser::parts::txn_header_desc::parse_txn_description; | ||
// use crate::parser::parts::txn_metadata::{parse_txn_meta, TxnMeta}; | ||
use crate::parser::{from_error, make_semantic_error, Stream}; | ||
use tackler_api::txn_header::{Comments, TxnHeader}; | ||
use winnow::ascii::{line_ending, space1}; | ||
use winnow::combinator::{cut_err, opt, preceded, repeat}; | ||
use winnow::error::{StrContext, StrContextValue}; | ||
|
||
use super::Commodity; | ||
|
||
/// Entry in the price database | ||
#[derive(Debug)] | ||
pub struct PriceEntry { | ||
/// Timestamp with Zone information | ||
pub timestamp: jiff::Zoned, | ||
/// The commodity for which price is being noted | ||
pub base_commodity: Arc<Commodity>, | ||
/// Price of base in _eq_ commodity | ||
pub eq_amount: Decimal, | ||
/// The equivalence commodity in which price is being noted | ||
pub eq_commodity: Arc<Commodity>, | ||
/// Comments | ||
pub comments: Option<String>, | ||
} |
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
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
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
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,101 @@ | ||
/* | ||
* Copyright 2024-2025 E257.FI and Muhammad Ragib Hasin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
use winnow::{ | ||
ascii::{line_ending, space0, space1}, | ||
combinator::opt, | ||
error::{StrContext, StrContextValue}, | ||
seq, PResult, Parser, | ||
}; | ||
|
||
use crate::model::price_entry::PriceEntry; | ||
use crate::parser::{from_error, parts::timestamp::parse_timestamp, Stream}; | ||
|
||
use super::{comment::p_comment, identifier::p_identifier, number::p_number}; | ||
|
||
#[allow(clippy::type_complexity)] | ||
pub(crate) fn parse_price_entry(is: &mut Stream<'_>) -> PResult<PriceEntry> { | ||
let (timestamp, base_commodity, eq_amount, eq_commodity, comments) = seq!( | ||
_: 'P'.context(StrContext::Expected(StrContextValue::Description("price entry starts with `P`"))), | ||
_: space1, | ||
parse_timestamp, | ||
_: space1, | ||
p_identifier | ||
.context(StrContext::Expected(StrContextValue::Description("price entry must have base commodity"))), | ||
_: space1, | ||
p_number | ||
.context(StrContext::Expected(StrContextValue::Description("price entry must have equivalent amount"))), | ||
_: space1, | ||
p_identifier | ||
.context(StrContext::Expected(StrContextValue::Description("price entry must have equivalent commodity"))), | ||
_: space0, | ||
opt(p_comment), | ||
_: line_ending, | ||
) | ||
.parse_next(is)?; | ||
|
||
let base_commodity = is | ||
.state | ||
.get_or_create_commodity(Some(base_commodity)) | ||
.map_err(|e| from_error(is, &*e))?; | ||
|
||
let eq_commodity = is | ||
.state | ||
.get_or_create_commodity(Some(eq_commodity)) | ||
.map_err(|e| from_error(is, &*e))?; | ||
|
||
let comments = comments.map(String::from); | ||
|
||
Ok(PriceEntry { | ||
timestamp, | ||
base_commodity, | ||
eq_amount, | ||
eq_commodity, | ||
comments, | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::kernel::Settings; | ||
|
||
#[test] | ||
fn test_parse_price_entry() { | ||
let tests = [ | ||
"P 2024-12-30 XAU 2659.64 USD\n", | ||
"P 2024-12-30T20:21:22 XAU 2659.64 USD ; space\n", | ||
"P 2024-12-30 XAU 2659.64 USD; no space\n", | ||
"P 2024-12-30T20:21:22Z XAU 2659.64 USD\n", | ||
"P 2024-12-30T20:21:22+02:00 XAU 2659.64 USD\n", | ||
"P 2024-12-30T20:21:22.12 XAU 2659.64 USD\n", | ||
]; | ||
|
||
for s in tests { | ||
let mut settings = Settings::default(); | ||
|
||
let mut is = Stream { | ||
input: s, | ||
state: &mut settings, | ||
}; | ||
|
||
let res = parse_price_entry(&mut is); | ||
|
||
assert!(res.is_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
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,108 @@ | ||
/* | ||
* Copyright 2023-2025 E257.FI and Muhammad Ragib Hasin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
use winnow::{ | ||
combinator::{eof, opt, preceded, repeat_till}, | ||
Parser, | ||
}; | ||
|
||
use crate::kernel::Settings; | ||
use crate::model::price_entry::PriceEntry; | ||
use crate::parser::Stream; | ||
|
||
use super::parts::{pricedb::parse_price_entry, txns::multispace0_line_ending}; | ||
|
||
use std::error::Error; | ||
use std::path::Path; | ||
|
||
pub(crate) fn pricedb_from_str( | ||
input: &mut &str, | ||
settings: &mut Settings, | ||
) -> Result<Vec<PriceEntry>, Box<dyn Error>> { | ||
let is = Stream { | ||
input, | ||
state: settings, | ||
}; | ||
|
||
preceded( | ||
opt(multispace0_line_ending), | ||
repeat_till(1.., parse_price_entry, eof), | ||
) | ||
.parse(is) | ||
.map(|(price_entries, _)| price_entries) | ||
.map_err(|err| err.to_string().into()) | ||
// .map_err(|err| { | ||
// let mut msg = "Failed to process txn input\n".to_string(); | ||
// //let _ = writeln!(msg, "Error: {}", err); | ||
// match err.into_inner() { | ||
// Some(ce) => { | ||
// if let Some(cause) = ce.cause() { | ||
// let _ = writeln!(msg, "Cause:\n{}\n", cause); | ||
// } | ||
// let _ = writeln!(msg, "Error backtrace:"); | ||
// for c in ce.context() { | ||
// let _ = writeln!(msg, " {}", c); | ||
// } | ||
// } | ||
// None => { | ||
// let _ = write!(msg, "No detailed error information available"); | ||
// } | ||
// } | ||
// let i = is.input.lines().next().unwrap_or(is.input); | ||
// let i_err = if i.chars().count() < 1024 { | ||
// i.to_string() | ||
// } else { | ||
// i.chars().take(1024).collect::<String>() | ||
// }; | ||
|
||
// let _ = write!(msg, "Failed input:\n{}\n\n", i_err); | ||
|
||
// msg.into() | ||
// }) | ||
} | ||
|
||
pub(crate) fn pricedb_from_file( | ||
path: &Path, | ||
settings: &mut Settings, | ||
) -> Result<Vec<PriceEntry>, Box<dyn Error>> { | ||
let pricedb_str = std::fs::read_to_string(path) | ||
.map_err(|err| format!("Can't open file: '{}' - {}", path.display(), err))?; | ||
|
||
// todo: error log | ||
pricedb_from_str(&mut &*pricedb_str, settings) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::kernel::Settings; | ||
|
||
#[test] | ||
fn test_parse_pricedb() { | ||
let test = r#" | ||
P 2024-01-09 XAU 2659.645203 USD | ||
P 2024-01-09 USD 121.306155 BDT | ||
P 2024-01-09 XAG 3652.77663 BDT | ||
"#; | ||
|
||
let mut settings = Settings::default(); | ||
|
||
let res = pricedb_from_str(&mut &*test, &mut settings); | ||
|
||
assert!(res.is_ok()); | ||
} | ||
} |