added helper functions to reduce bloat
This commit is contained in:
@@ -80,4 +80,51 @@ pub fn random_range(min: u64, max: u64) -> u64 {
|
||||
pub fn choose_random<T: Clone>(items: &[T]) -> T {
|
||||
let mut rng = StdRng::from_rng(&mut rand::rng());
|
||||
items.choose(&mut rng).unwrap().clone()
|
||||
}
|
||||
|
||||
/// Extract first valid Yahoo ticker from company
|
||||
pub fn extract_first_yahoo_ticker(company: &CompanyCrossPlatformInfo) -> Option<String> {
|
||||
for tickers in company.isin_tickers_map.values() {
|
||||
for ticker in tickers {
|
||||
if ticker.starts_with("YAHOO:")
|
||||
&& ticker != "YAHOO:NO_RESULTS"
|
||||
&& ticker != "YAHOO:ERROR"
|
||||
{
|
||||
return Some(ticker.trim_start_matches("YAHOO:").to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Sanitize company name for file system use
|
||||
pub fn sanitize_company_name(name: &str) -> String {
|
||||
name.replace("/", "_")
|
||||
.replace("\\", "_")
|
||||
.replace(":", "_")
|
||||
.replace("*", "_")
|
||||
.replace("?", "_")
|
||||
.replace("\"", "_")
|
||||
.replace("<", "_")
|
||||
.replace(">", "_")
|
||||
.replace("|", "_")
|
||||
}
|
||||
|
||||
/// Load companies from JSONL file
|
||||
pub async fn load_companies_from_jsonl(
|
||||
path: &std::path::Path
|
||||
) -> anyhow::Result<Vec<CompanyCrossPlatformInfo>> {
|
||||
let content = tokio::fs::read_to_string(path).await?;
|
||||
let mut companies = Vec::new();
|
||||
|
||||
for line in content.lines() {
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
if let Ok(company) = serde_json::from_str::<CompanyCrossPlatformInfo>(line) {
|
||||
companies.push(company);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(companies)
|
||||
}
|
||||
Reference in New Issue
Block a user