blob: c9bfc1e6959178f8fd7afbce44789924e03c85e3 [file] [log] [blame]
// Copyright 2025 Google LLC
//
// 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.
//! 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)
}