use schemars::{JsonSchema, Schema};
use serde::{Deserialize, Deserializer, Serialize};
use crate::live_index::search;
pub(crate) fn lenient_u32<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<u32>, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum NumOrStr {
Num(u32),
Str(String),
Null,
}
match NumOrStr::deserialize(deserializer)? {
NumOrStr::Num(n) => Ok(Some(n)),
NumOrStr::Str(s) if s.is_empty() => Ok(None),
NumOrStr::Str(s) => s.parse::<u32>().map(Some).map_err(serde::de::Error::custom),
NumOrStr::Null => Ok(None),
}
}
pub(crate) fn lenient_bool<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<bool>, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum BoolOrStr {
Bool(bool),
Str(String),
Null,
}
match BoolOrStr::deserialize(deserializer)? {
BoolOrStr::Bool(b) => Ok(Some(b)),
BoolOrStr::Str(s) => match s.as_str() {
"true" | "1" => Ok(Some(true)),
"false" | "0" => Ok(Some(false)),
"" => Ok(None),
_ => Err(serde::de::Error::custom(format!(
"expected boolean or \"true\"/\"false\", got \"{s}\""
))),
},
BoolOrStr::Null => Ok(None),
}
}
pub(crate) fn lenient_bool_required<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<bool, D::Error> {
lenient_bool(deserializer).map(|value| value.unwrap_or(false))
}
const INCLUDE_TESTS_SECTION_MARKER: &str = "__symforge_include_tests";
pub(crate) fn encode_include_tests_marker(
mut sections: Option<Vec<String>>,
include_tests: bool,
) -> Option<Vec<String>> {
if include_tests {
sections
.get_or_insert_with(Vec::new)
.push(INCLUDE_TESTS_SECTION_MARKER.to_string());
}
sections
}
pub(crate) fn include_tests_from_sections(sections: Option<&Vec<String>>) -> bool {
sections
.map(|items| {
items
.iter()
.any(|section| section == INCLUDE_TESTS_SECTION_MARKER)
})
.unwrap_or(false)
}
pub(crate) fn visible_sections(sections: &Option<Vec<String>>) -> Option<Vec<String>> {
sections.as_ref().and_then(|items| {
let had_marker = items
.iter()
.any(|section| section.as_str() == INCLUDE_TESTS_SECTION_MARKER);
let visible = items
.iter()
.filter(|section| section.as_str() != INCLUDE_TESTS_SECTION_MARKER)
.cloned()
.collect::<Vec<_>>();
if visible.is_empty() && had_marker {
None
} else {
Some(visible)
}
})
}
fn add_include_tests_schema(schema: &mut Schema) {
if let Some(serde_json::Value::Object(properties)) = schema.get_mut("properties") {
properties.insert(
"include_tests".to_string(),
serde_json::json!({
"type": "boolean",
"default": false,
"description": "Include expanded test modules in context output."
}),
);
}
}
pub(crate) fn lenient_u32_required<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<u32, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum NumOrStr {
Num(u32),
Str(String),
}
match NumOrStr::deserialize(deserializer)? {
NumOrStr::Num(n) => Ok(n),
NumOrStr::Str(s) => s.parse::<u32>().map_err(serde::de::Error::custom),
}
}
pub(crate) fn lenient_u64<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<u64>, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum NumOrStr {
Num(u64),
Str(String),
Null,
}
match NumOrStr::deserialize(deserializer)? {
NumOrStr::Num(n) => Ok(Some(n)),
NumOrStr::Str(s) if s.is_empty() => Ok(None),
NumOrStr::Str(s) => s.parse::<u64>().map(Some).map_err(serde::de::Error::custom),
NumOrStr::Null => Ok(None),
}
}
pub(crate) fn lenient_i64<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<i64>, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum NumOrStr {
Num(i64),
Str(String),
Null,
}
match NumOrStr::deserialize(deserializer)? {
NumOrStr::Num(n) => Ok(Some(n)),
NumOrStr::Str(s) if s.is_empty() => Ok(None),
NumOrStr::Str(s) => s.parse::<i64>().map(Some).map_err(serde::de::Error::custom),
NumOrStr::Null => Ok(None),
}
}
pub(crate) fn lenient_option_vec<'de, D, T>(deserializer: D) -> Result<Option<Vec<T>>, D::Error>
where
D: Deserializer<'de>,
T: serde::de::DeserializeOwned,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum VecOrStr {
Vec(Vec<serde_json::Value>),
Str(String),
Null,
}
match VecOrStr::deserialize(deserializer)? {
VecOrStr::Vec(values) => {
let result: Result<Vec<T>, _> = values
.into_iter()
.enumerate()
.map(|(i, v)| match serde_json::from_value::<T>(v.clone()) {
Ok(item) => Ok(item),
Err(direct_err) => {
if let serde_json::Value::String(ref s) = v {
serde_json::from_str::<T>(s).map_err(|_| {
serde::de::Error::custom(format!("element {i}: {direct_err}"))
})
} else {
Err(serde::de::Error::custom(format!(
"element {i}: {direct_err}"
)))
}
}
})
.collect();
result.map(Some)
}
VecOrStr::Str(s) if s.is_empty() || s == "null" => Ok(None),
VecOrStr::Str(s) => serde_json::from_str::<Vec<T>>(&s)
.map(Some)
.map_err(serde::de::Error::custom),
VecOrStr::Null => Ok(None),
}
}
pub(crate) fn lenient_vec_required<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: serde::de::DeserializeOwned,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum VecOrStr {
Vec(Vec<serde_json::Value>),
Str(String),
}
match VecOrStr::deserialize(deserializer)? {
VecOrStr::Vec(values) => {
values
.into_iter()
.enumerate()
.map(|(i, v)| {
match serde_json::from_value::<T>(v.clone()) {
Ok(item) => Ok(item),
Err(direct_err) => {
if let serde_json::Value::String(ref s) = v {
serde_json::from_str::<T>(s).map_err(|_| {
serde::de::Error::custom(format!("element {i}: {direct_err}"))
})
} else {
Err(serde::de::Error::custom(format!(
"element {i}: {direct_err}"
)))
}
}
}
})
.collect()
}
VecOrStr::Str(s) => serde_json::from_str::<Vec<T>>(&s).map_err(serde::de::Error::custom),
}
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct GetSymbolInput {
#[serde(default)]
pub project: Option<String>,
#[serde(default)]
pub path: String,
#[serde(default)]
pub name: String,
pub kind: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub symbol_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_option_vec")]
#[schemars(with = "Vec<SymbolTarget>")]
pub targets: Option<Vec<SymbolTarget>>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default)]
pub max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_bool")]
pub force_refresh: Option<bool>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct SymbolTarget {
pub path: String,
pub name: Option<String>,
pub kind: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub symbol_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub start_byte: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub end_byte: Option<u32>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetFileContentInput {
#[serde(default)]
pub project: Option<String>,
pub path: String,
#[serde(default)]
pub mode: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub start_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub end_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub chunk_index: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub max_lines: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub around_line: Option<u32>,
pub around_match: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub match_occurrence: Option<u32>,
pub around_symbol: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub symbol_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub context_lines: Option<u32>,
#[serde(default, deserialize_with = "lenient_bool")]
pub show_line_numbers: Option<bool>,
#[serde(default, deserialize_with = "lenient_bool")]
pub header: Option<bool>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_u32")]
pub offset: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub limit: Option<u32>,
#[serde(default)]
pub max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_bool")]
pub force_refresh: Option<bool>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct SymforgeRetrieveInput {
pub hash: String,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub(crate) struct ValidateFileSyntaxInput {
#[serde(default)]
pub project: Option<String>,
pub path: String,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct FindDependentsInput {
#[serde(default)]
pub project: Option<String>,
pub path: String,
#[serde(default)]
pub name: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub limit: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub max_per_file: Option<u32>,
pub format: Option<String>,
#[serde(default, deserialize_with = "lenient_bool")]
pub compact: Option<bool>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct GetRepoMapInput {
#[serde(default)]
pub project: Option<String>,
pub detail: Option<String>,
pub path: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub depth: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub max_files: Option<u32>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = add_include_tests_schema)]
pub struct GetFileContextInput {
#[serde(default)]
pub project: Option<String>,
pub path: String,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_option_vec")]
#[schemars(with = "Vec<String>")]
pub sections: Option<Vec<String>>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_bool")]
pub force_refresh: Option<bool>,
}
impl<'de> Deserialize<'de> for GetFileContextInput {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
struct Raw {
#[serde(default)]
project: Option<String>,
path: String,
#[serde(default, deserialize_with = "lenient_u64")]
max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_option_vec")]
sections: Option<Vec<String>>,
#[serde(default, deserialize_with = "lenient_bool_required")]
include_tests: bool,
#[serde(default, deserialize_with = "lenient_bool")]
estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_bool")]
force_refresh: Option<bool>,
}
let raw = Raw::deserialize(deserializer)?;
Ok(Self {
project: raw.project,
path: raw.path,
max_tokens: raw.max_tokens,
sections: encode_include_tests_marker(raw.sections, raw.include_tests),
estimate: raw.estimate,
force_refresh: raw.force_refresh,
})
}
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = add_include_tests_schema)]
pub struct GetSymbolContextInput {
#[serde(default)]
pub project: Option<String>,
pub name: String,
pub file: Option<String>,
pub path: Option<String>,
pub symbol_kind: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub symbol_line: Option<u32>,
pub verbosity: Option<String>,
#[serde(default, deserialize_with = "lenient_bool")]
pub bundle: Option<bool>,
#[serde(default, deserialize_with = "lenient_option_vec")]
#[schemars(with = "Vec<String>")]
pub sections: Option<Vec<String>>,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
}
impl<'de> Deserialize<'de> for GetSymbolContextInput {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
struct Raw {
#[serde(default)]
project: Option<String>,
name: String,
file: Option<String>,
path: Option<String>,
symbol_kind: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
symbol_line: Option<u32>,
verbosity: Option<String>,
#[serde(default, deserialize_with = "lenient_bool")]
bundle: Option<bool>,
#[serde(default, deserialize_with = "lenient_option_vec")]
sections: Option<Vec<String>>,
#[serde(default, deserialize_with = "lenient_bool_required")]
include_tests: bool,
#[serde(default, deserialize_with = "lenient_u64")]
max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_bool")]
estimate: Option<bool>,
}
let raw = Raw::deserialize(deserializer)?;
let sections = match raw.sections {
Some(sections) => encode_include_tests_marker(Some(sections), raw.include_tests),
None => None,
};
Ok(Self {
project: raw.project,
name: raw.name,
file: raw.file,
path: raw.path,
symbol_kind: raw.symbol_kind,
symbol_line: raw.symbol_line,
verbosity: raw.verbosity,
bundle: raw.bundle,
sections,
max_tokens: raw.max_tokens,
estimate: raw.estimate,
})
}
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct TraceSymbolInput {
pub path: String,
pub name: String,
pub kind: Option<String>,
#[serde(default, deserialize_with = "lenient_u32")]
pub symbol_line: Option<u32>,
#[serde(default, deserialize_with = "lenient_option_vec")]
pub sections: Option<Vec<String>>,
pub verbosity: Option<String>,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
}
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct InspectMatchInput {
pub path: String,
#[serde(deserialize_with = "lenient_u32_required")]
pub line: u32,
#[serde(default, deserialize_with = "lenient_u32")]
pub context: Option<u32>,
#[serde(default, deserialize_with = "lenient_u32")]
pub sibling_limit: Option<u32>,
#[serde(default, deserialize_with = "lenient_bool")]
pub estimate: Option<bool>,
#[serde(default, deserialize_with = "lenient_u64")]
pub max_tokens: Option<u64>,
}
pub(crate) fn normalize_file_content_aliases(
input: &mut GetFileContentInput,
) -> Result<(), String> {
if input.offset.is_none() && input.limit.is_none() {
return Ok(());
}
let mut conflicts: Vec<&str> = Vec::new();
if input.start_line.is_some() {
conflicts.push("`start_line`");
}
if input.end_line.is_some() {
conflicts.push("`end_line`");
}
if input.around_line.is_some() {
conflicts.push("`around_line`");
}
if input.around_match.is_some() {
conflicts.push("`around_match`");
}
if input.around_symbol.is_some() {
conflicts.push("`around_symbol`");
}
if input.chunk_index.is_some() {
conflicts.push("`chunk_index`");
}
if input.mode.is_some() {
conflicts.push("`mode`");
}
if !conflicts.is_empty() {
return Err(format!(
"Invalid get_file_content request: `offset`/`limit` aliases cannot be combined with {}. Use native params instead.",
conflicts.join(", ")
));
}
if input.limit == Some(0) {
return Err("Invalid get_file_content request: `limit` must be 1 or greater.".to_string());
}
let offset = input.offset.unwrap_or(0);
input.start_line = Some(offset.saturating_add(1));
if let Some(limit) = input.limit {
input.end_line = Some(offset.saturating_add(limit));
}
input.offset = None;
input.limit = None;
Ok(())
}
pub(crate) fn file_content_options_from_input(
input: &GetFileContentInput,
) -> Result<search::FileContentOptions, String> {
let show_line_numbers = input.show_line_numbers.unwrap_or(false);
let header = input.header.unwrap_or(false);
if let Some(mode) = &input.mode {
let mode_str = mode.as_str();
let result = match mode_str {
"lines" => validate_lines_mode(input),
"symbol" => validate_symbol_mode(input),
"match" => validate_match_mode(input),
"chunk" => validate_chunk_mode(input),
"search" => Err("mode 'search' is not yet implemented".to_string()),
other => Err(format!(
"Unknown mode '{other}'. Valid modes: lines, symbol, match, chunk."
)),
};
return result.map(|mut opts| {
opts.content_context.mode_name = Some(mode_str.to_string());
opts.content_context.mode_explicit = true;
opts
});
}
if input.symbol_line.is_some() && input.around_symbol.is_none() {
return Err(
"Invalid get_file_content request: `symbol_line` requires `around_symbol`.".to_string(),
);
}
if input.match_occurrence.is_some() && input.around_match.is_none() {
return Err(
"Invalid get_file_content request: `match_occurrence` requires `around_match`."
.to_string(),
);
}
if matches!(input.match_occurrence, Some(0)) {
return Err(
"Invalid get_file_content request: `match_occurrence` must be 1 or greater."
.to_string(),
);
}
if let Some(raw_around_symbol) = input.around_symbol.as_deref() {
let around_symbol = raw_around_symbol.trim();
if around_symbol.is_empty() {
return Err(
"Invalid get_file_content request: `around_symbol` must not be empty.".to_string(),
);
}
if input.start_line.is_some()
|| input.end_line.is_some()
|| input.around_line.is_some()
|| input.around_match.is_some()
|| input.chunk_index.is_some()
{
return Err(
"Invalid get_file_content request: `around_symbol` cannot be combined with `start_line`, `end_line`, `around_line`, `around_match`, or `chunk_index`. Valid with `around_symbol`: `symbol_line`, `context_lines`, `max_lines`."
.to_string(),
);
}
let mut opts = search::FileContentOptions::for_explicit_path_read_around_symbol(
input.path.clone(),
around_symbol,
input.symbol_line,
input.context_lines,
input.max_lines,
show_line_numbers,
header,
);
opts.content_context.mode_name = Some("symbol".to_string());
opts.content_context.mode_explicit = false;
return Ok(opts);
}
if input.max_lines.is_some() && input.chunk_index.is_none() {
return Err(
"Invalid get_file_content request: `max_lines` requires `chunk_index`.".to_string(),
);
}
if let Some(chunk_index) = input.chunk_index {
let Some(max_lines) = input.max_lines else {
return Err(
"Invalid get_file_content request: `chunk_index` requires `max_lines`.".to_string(),
);
};
if chunk_index == 0 {
return Err(
"Invalid get_file_content request: `chunk_index` must be 1 or greater.".to_string(),
);
}
if max_lines == 0 {
return Err(
"Invalid get_file_content request: `max_lines` must be 1 or greater.".to_string(),
);
}
if input.start_line.is_some()
|| input.end_line.is_some()
|| input.around_line.is_some()
|| input.around_match.is_some()
{
return Err(
"Invalid get_file_content request: chunked reads (`chunk_index` + `max_lines`) cannot be combined with `start_line`, `end_line`, `around_line`, or `around_match`."
.to_string(),
);
}
let mut opts = search::FileContentOptions::for_explicit_path_read_chunk(
input.path.clone(),
chunk_index,
max_lines,
);
opts.content_context.mode_name = Some("chunk".to_string());
opts.content_context.mode_explicit = false;
return Ok(opts);
}
if let Some(raw_around_match) = input.around_match.as_deref() {
let around_match = raw_around_match.trim();
if around_match.is_empty() {
return Err(
"Invalid get_file_content request: `around_match` must not be empty.".to_string(),
);
}
if input.start_line.is_some() || input.end_line.is_some() || input.around_line.is_some() {
return Err(
"Invalid get_file_content request: `around_match` cannot be combined with `start_line`, `end_line`, or `around_line`. Valid with `around_match`: `context_lines`."
.to_string(),
);
}
let mut opts = search::FileContentOptions::for_explicit_path_read_around_match(
input.path.clone(),
around_match,
input.match_occurrence,
input.context_lines,
show_line_numbers,
header,
);
opts.content_context.mode_name = Some("match".to_string());
opts.content_context.mode_explicit = false;
return Ok(opts);
}
if input.around_line.is_some() && (input.start_line.is_some() || input.end_line.is_some()) {
return Err(
"Invalid get_file_content request: `around_line` cannot be combined with `start_line` or `end_line`. Valid with `around_line`: `context_lines`."
.to_string(),
);
}
let mut opts = match input.around_line {
Some(around_line) => search::FileContentOptions::for_explicit_path_read_around_line(
input.path.clone(),
around_line,
input.context_lines,
show_line_numbers,
header,
),
None => search::FileContentOptions::for_explicit_path_read_with_format(
input.path.clone(),
input.start_line,
input.end_line,
show_line_numbers,
header,
),
};
opts.content_context.mode_name = Some("lines".to_string());
opts.content_context.mode_explicit = false;
Ok(opts)
}
fn describe_received_flags(input: &GetFileContentInput) -> String {
let mut flags = Vec::new();
if input.start_line.is_some() {
flags.push("start_line");
}
if input.end_line.is_some() {
flags.push("end_line");
}
if input.around_line.is_some() {
flags.push("around_line");
}
if input.around_match.is_some() {
flags.push("around_match");
}
if input.match_occurrence.is_some() {
flags.push("match_occurrence");
}
if input.around_symbol.is_some() {
flags.push("around_symbol");
}
if input.symbol_line.is_some() {
flags.push("symbol_line");
}
if input.chunk_index.is_some() {
flags.push("chunk_index");
}
if input.max_lines.is_some() {
flags.push("max_lines");
}
if input.context_lines.is_some() {
flags.push("context_lines");
}
if input.show_line_numbers.is_some() {
flags.push("show_line_numbers");
}
if input.header.is_some() {
flags.push("header");
}
flags.join(", ")
}
fn validate_lines_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
let cross: Vec<&str> = [
input.around_symbol.as_ref().map(|_| "around_symbol"),
input.around_match.as_ref().map(|_| "around_match"),
input.match_occurrence.map(|_| "match_occurrence"),
input.chunk_index.map(|_| "chunk_index"),
]
.into_iter()
.flatten()
.collect();
if !cross.is_empty() {
return Err(format!(
"mode=lines conflicts with {}. Use mode={}. Received: {}",
cross.join(", "),
if input.around_symbol.is_some() {
"symbol"
} else if input.around_match.is_some() || input.match_occurrence.is_some() {
"match"
} else {
"chunk"
},
describe_received_flags(input),
));
}
let show_line_numbers = input.show_line_numbers.unwrap_or(false);
let header = input.header.unwrap_or(false);
if input.around_line.is_some() && (input.start_line.is_some() || input.end_line.is_some()) {
return Err(
"Invalid get_file_content request: `around_line` cannot be combined with `start_line` or `end_line`. Valid with `around_line`: `context_lines`."
.to_string(),
);
}
Ok(match input.around_line {
Some(around_line) => search::FileContentOptions::for_explicit_path_read_around_line(
input.path.clone(),
around_line,
input.context_lines,
show_line_numbers,
header,
),
None => search::FileContentOptions::for_explicit_path_read_with_format(
input.path.clone(),
input.start_line,
input.end_line,
show_line_numbers,
header,
),
})
}
fn validate_symbol_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
let Some(raw_around_symbol) = input.around_symbol.as_deref() else {
return Err("mode=symbol requires around_symbol".to_string());
};
let around_symbol = raw_around_symbol.trim();
if around_symbol.is_empty() {
return Err("mode=symbol requires around_symbol".to_string());
}
let cross: Vec<&str> = [
input.start_line.map(|_| "start_line"),
input.end_line.map(|_| "end_line"),
input.around_line.map(|_| "around_line"),
input.around_match.as_ref().map(|_| "around_match"),
input.match_occurrence.map(|_| "match_occurrence"),
input.chunk_index.map(|_| "chunk_index"),
]
.into_iter()
.flatten()
.collect();
if !cross.is_empty() {
return Err(format!(
"mode=symbol conflicts with {}. Received: {}",
cross.join(", "),
describe_received_flags(input),
));
}
let show_line_numbers = input.show_line_numbers.unwrap_or(false);
let header = input.header.unwrap_or(false);
Ok(
search::FileContentOptions::for_explicit_path_read_around_symbol(
input.path.clone(),
around_symbol,
input.symbol_line,
input.context_lines,
input.max_lines,
show_line_numbers,
header,
),
)
}
fn validate_match_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
let Some(raw_around_match) = input.around_match.as_deref() else {
return Err("mode=match requires around_match".to_string());
};
let around_match = raw_around_match.trim();
if around_match.is_empty() {
return Err("mode=match requires around_match".to_string());
}
if matches!(input.match_occurrence, Some(0)) {
return Err(
"Invalid get_file_content request: `match_occurrence` must be 1 or greater."
.to_string(),
);
}
let cross: Vec<&str> = [
input.start_line.map(|_| "start_line"),
input.end_line.map(|_| "end_line"),
input.around_line.map(|_| "around_line"),
input.around_symbol.as_ref().map(|_| "around_symbol"),
input.chunk_index.map(|_| "chunk_index"),
]
.into_iter()
.flatten()
.collect();
if !cross.is_empty() {
return Err(format!(
"mode=match conflicts with {}. Use mode={}. Received: {}",
cross.join(", "),
if input.around_symbol.is_some() {
"symbol"
} else if input.start_line.is_some()
|| input.end_line.is_some()
|| input.around_line.is_some()
{
"lines"
} else {
"chunk"
},
describe_received_flags(input),
));
}
let show_line_numbers = input.show_line_numbers.unwrap_or(false);
let header = input.header.unwrap_or(false);
Ok(
search::FileContentOptions::for_explicit_path_read_around_match(
input.path.clone(),
around_match,
input.match_occurrence,
input.context_lines,
show_line_numbers,
header,
),
)
}
fn validate_chunk_mode(input: &GetFileContentInput) -> Result<search::FileContentOptions, String> {
let chunk_index = input
.chunk_index
.ok_or_else(|| "mode=chunk requires chunk_index".to_string())?;
let max_lines = input
.max_lines
.ok_or_else(|| "mode=chunk requires max_lines".to_string())?;
if chunk_index == 0 {
return Err(
"Invalid get_file_content request: `chunk_index` must be 1 or greater.".to_string(),
);
}
if max_lines == 0 {
return Err(
"Invalid get_file_content request: `max_lines` must be 1 or greater.".to_string(),
);
}
let cross: Vec<&str> = [
input.around_symbol.as_ref().map(|_| "around_symbol"),
input.around_match.as_ref().map(|_| "around_match"),
input.match_occurrence.map(|_| "match_occurrence"),
input.around_line.map(|_| "around_line"),
input.start_line.map(|_| "start_line"),
input.end_line.map(|_| "end_line"),
]
.into_iter()
.flatten()
.collect();
if !cross.is_empty() {
return Err(format!(
"mode=chunk conflicts with {}. Use mode={}. Received: {}",
cross.join(", "),
if input.around_symbol.is_some() {
"symbol"
} else if input.around_match.is_some() || input.match_occurrence.is_some() {
"match"
} else {
"lines"
},
describe_received_flags(input),
));
}
Ok(search::FileContentOptions::for_explicit_path_read_chunk(
input.path.clone(),
chunk_index,
max_lines,
))
}