Skip to content

Commit f647e2b

Browse files
authored
fix(examples): async_chain_ops uses a temp dir; creator points at shipped chain (#388) (#390)
- `async_chain_ops`: `save_to_json_async`/`save_to_csv_async` take a *directory* and append `{title}.{ext}`. The example previously passed "async_chain.json" as a directory, so the inner `File::create` hit ENOENT. Route the paths through `std::env::temp_dir().join("optionstratlib-async-chain-ops")` and create it up front. - `creator`: point the hard-coded JSON path at the Germany-40 chain that actually ships in `examples/Chains/` (`Germany-40-2025-05-27-15-29-00-UTC-24209.json`) instead of the `2025-06-13-16:00:00-UTC-23794.5` file that was never committed. Both example binaries now run to completion. Closes #388.
1 parent 0c12afd commit f647e2b

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

examples/examples_chain/src/bin/async_chain_ops.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::error::Error;
55
async fn main() -> Result<(), Box<dyn Error>> {
66
println!("--- Async Option Chain Operations ---");
77

8-
// 1. Create a dummy chain
98
let chain = OptionChain::new(
109
"AAPL",
1110
pos_or_panic!(150.0),
@@ -14,37 +13,41 @@ async fn main() -> Result<(), Box<dyn Error>> {
1413
None,
1514
);
1615

17-
// 2. Save it asynchronously to JSON
18-
let json_path = "async_chain.json";
19-
println!("Saving chain to {} asynchronously...", json_path);
20-
chain.save_to_json_async(json_path).await?;
16+
// `save_to_json_async` / `save_to_csv_async` take a *directory* and append
17+
// `{title}.{ext}` from the chain's metadata. Use a scratch subdir under
18+
// the OS temp dir so the example never collides with other processes.
19+
let dir = std::env::temp_dir().join("optionstratlib-async-chain-ops");
20+
std::fs::create_dir_all(&dir)?;
21+
let dir_str = dir.to_string_lossy().to_string();
22+
let filename_json = format!("{}.json", chain.get_title());
23+
let filename_csv = format!("{}.csv", chain.get_title());
24+
let json_path = dir.join(&filename_json);
25+
let csv_path = dir.join(&filename_csv);
26+
27+
println!("Saving chain to {} asynchronously...", json_path.display());
28+
chain.save_to_json_async(&dir_str).await?;
2129
println!("Successfully saved to JSON.");
2230

23-
// 3. Load it asynchronously from JSON
24-
println!("Loading chain from {} asynchronously...", json_path);
25-
let loaded_json = OptionChain::load_from_json_async(json_path).await?;
31+
println!("Loading chain from {} asynchronously...", json_path.display());
32+
let loaded_json = OptionChain::load_from_json_async(json_path.to_str().unwrap()).await?;
2633
println!(
2734
"Successfully loaded from JSON. Symbol: {}",
2835
loaded_json.symbol
2936
);
3037

31-
// 4. Save it asynchronously to CSV
32-
let csv_path = "async_chain.csv";
33-
println!("Saving chain to {} asynchronously...", csv_path);
34-
chain.save_to_csv_async(csv_path).await?;
38+
println!("Saving chain to {} asynchronously...", csv_path.display());
39+
chain.save_to_csv_async(&dir_str).await?;
3540
println!("Successfully saved to CSV.");
3641

37-
// 5. Load it asynchronously from CSV
38-
println!("Loading chain from {} asynchronously...", csv_path);
39-
let loaded_csv = OptionChain::load_from_csv_async(csv_path).await?;
42+
println!("Loading chain from {} asynchronously...", csv_path.display());
43+
let loaded_csv = OptionChain::load_from_csv_async(csv_path.to_str().unwrap()).await?;
4044
println!(
4145
"Successfully loaded from CSV. Symbol: {}",
4246
loaded_csv.symbol
4347
);
4448

45-
// Clean up
46-
std::fs::remove_file(json_path)?;
47-
std::fs::remove_file(csv_path)?;
49+
let _ = std::fs::remove_file(&json_path);
50+
let _ = std::fs::remove_file(&csv_path);
4851

4952
println!("--- Async Chain Operations Completed ---");
5053
Ok(())

examples/examples_chain/src/bin/creator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() -> Result<(), optionstratlib::error::Error> {
1313
let symbol = "GER400";
1414
setup_logger();
1515
let option_chain = OptionChain::load_from_json(
16-
"examples/Chains/Germany-40-2025-06-13-16:00:00-UTC-23794.5.json",
16+
"examples/Chains/Germany-40-2025-05-27-15-29-00-UTC-24209.json",
1717
)?;
1818
info!("Successfully retrieved option chain for {}", symbol);
1919
info!("{}", option_chain);

0 commit comments

Comments
 (0)