blob: f458165c8b9c881cb0f74ff437f98b4d51844050 [file] [log] [blame]
//! This module contains functions and structures related to xpath handling
use crate::app_state::AppState;
use crate::handlers::chassis::{append_to_urls, handle_xpath_segment};
use anyhow::Context;
use std::collections::HashMap;
use std::sync::Arc;
/// Retrieves URLs based on the provided XPath-like segments.
///
/// # Arguments
///
/// * `state` - The application state.
/// * `identifiers` - A map of identifiers and their corresponding values.
/// * `segments` - A slice of string slices representing the XPath-like segments.
///
/// # Returns
///
/// A Result containing a vector of URLs or an error.
pub async fn get_xpath_urls(
state: &Arc<AppState>,
identifiers: &HashMap<String, Vec<String>>,
segments: &[&str],
) -> Result<Vec<String>, anyhow::Error> {
let mut urls = vec![String::from("http://localhost")];
for segment in segments {
if segment.starts_with('{') && segment.ends_with('}') {
// SAFETY: range access is checked
let placeholder = &segment[1..segment.len() - 1];
if let Some(values) = identifiers.get(placeholder) {
if values.contains(&"*".to_string()) {
if values.len() != 1 {
return Err(anyhow::anyhow!(
"Wildcard must be the only value for placeholder '{}'",
placeholder
));
}
handle_xpath_segment(&mut urls, placeholder, state)
.await
.context(format!(
"Failed to handle XPath segment for placeholder '{}'",
placeholder
))?;
// TODO: implement filter logic based on the protobuf message format
// urls = filter_urls(&urls, placeholder, filters, state).await?;
}
}
} else {
append_to_urls(&mut urls, segment);
}
}
Ok(urls)
}