Skip to content

Commit 8f53984

Browse files
author
Olivier D'Ancona
committed
output format new syntax
1 parent 9c3596c commit 8f53984

File tree

5 files changed

+27
-36
lines changed

5 files changed

+27
-36
lines changed

crates/code2prompt-core/src/configuration.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub struct TomlConfig {
121121
pub full_directory_tree: bool,
122122

123123
/// Output format
124-
pub output_format: Option<String>,
124+
pub output_format: Option<OutputFormat>,
125125

126126
/// Sort method
127127
pub sort_method: Option<FileSortMethod>,
@@ -172,11 +172,7 @@ impl TomlConfig {
172172
.absolute_path(self.absolute_path)
173173
.full_directory_tree(self.full_directory_tree);
174174

175-
if let Some(output_format) = &self.output_format
176-
&& let Ok(format) = output_format.parse::<OutputFormat>()
177-
{
178-
builder.output_format(format);
179-
}
175+
builder.output_format(self.output_format.unwrap_or_default());
180176

181177
builder.sort_method(self.sort_method);
182178

@@ -224,7 +220,7 @@ pub fn export_config_to_toml(config: &Code2PromptConfig) -> Result<String, toml:
224220
line_numbers: config.line_numbers,
225221
absolute_path: config.absolute_path,
226222
full_directory_tree: config.full_directory_tree,
227-
output_format: Some(config.output_format.to_string()),
223+
output_format: Some(config.output_format),
228224
sort_method: config.sort_method,
229225
encoding: Some(config.encoding),
230226
token_format: Some(config.token_format),

crates/code2prompt-core/src/template.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use anyhow::{Result, anyhow};
44
use handlebars::{Handlebars, no_escape};
55
use regex::Regex;
6+
use serde::{Deserialize, Serialize};
67
use std::io::Write;
7-
use std::str::FromStr;
88

99
/// Set up the Handlebars template engine with a template string and a template name.
1010
///
@@ -85,7 +85,8 @@ pub fn write_to_file(output_path: &str, rendered: &str) -> Result<()> {
8585
}
8686

8787
/// Enum to represent the output format.
88-
#[derive(Default, Debug, Clone, PartialEq, Eq)]
88+
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
89+
#[serde(rename_all = "lowercase")]
8990
pub enum OutputFormat {
9091
#[default]
9192
Markdown,
@@ -102,19 +103,3 @@ impl std::fmt::Display for OutputFormat {
102103
}
103104
}
104105
}
105-
106-
impl FromStr for OutputFormat {
107-
type Err = anyhow::Error;
108-
109-
fn from_str(s: &str) -> Result<Self> {
110-
match s.to_lowercase().as_str() {
111-
"markdown" | "md" => Ok(OutputFormat::Markdown),
112-
"json" => Ok(OutputFormat::Json),
113-
"xml" => Ok(OutputFormat::Xml),
114-
_ => Err(anyhow!(
115-
"Invalid output format: {}. Allowed values: markdown, json, xml",
116-
s
117-
)),
118-
}
119-
}
120-
}

crates/code2prompt/src/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ pub struct Cli {
4545
#[clap(
4646
short = 'F',
4747
long = "output-format",
48-
default_value = "markdown",
49-
value_name = "markdown, json, xml"
48+
value_name = "markdown, json, xml",
49+
value_parser = ValueParser::new(parse_serde::<OutputFormat>)
5050
)]
51-
pub output_format: OutputFormat,
51+
pub output_format: Option<OutputFormat>,
5252

5353
/// Optional Path to a custom Handlebars template
5454
#[clap(short, long, value_name = "TEMPLATE")]

crates/code2prompt/src/config.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
77
use anyhow::{Context, Result};
88
use code2prompt_core::{
9-
configuration::Code2PromptConfig, session::Code2PromptSession, sort::FileSortMethod,
10-
template::extract_undefined_variables, tokenizer::TokenizerType,
9+
configuration::Code2PromptConfig,
10+
session::Code2PromptSession,
11+
sort::FileSortMethod,
12+
template::{OutputFormat, extract_undefined_variables},
13+
tokenizer::TokenizerType,
1114
};
1215
use inquire::Text;
1316
use log::error;
@@ -55,7 +58,6 @@ pub fn build_session(
5558
expand_comma_separated_patterns(&args.exclude),
5659
)
5760
};
58-
5961
configuration
6062
.include_patterns(include_patterns)
6163
.exclude_patterns(exclude_patterns);
@@ -64,14 +66,20 @@ pub fn build_session(
6466
let cfg_line_numbers = cfg.map(|c| c.line_numbers).unwrap_or(false);
6567
let cfg_absolute = cfg.map(|c| c.absolute_path).unwrap_or(false);
6668
let cfg_full_tree = cfg.map(|c| c.full_directory_tree).unwrap_or(false);
67-
6869
configuration
6970
.line_numbers(args.line_numbers || cfg_line_numbers)
7071
.absolute_path(args.absolute_paths || cfg_absolute)
7172
.full_directory_tree(args.full_directory_tree || cfg_full_tree);
7273

73-
// Output format: CLI value
74-
configuration.output_format(args.output_format.clone());
74+
// Output format: CLI overrides config
75+
let output_format = if let Some(output_format_str) = args.output_format {
76+
output_format_str
77+
} else if let Some(c) = cfg {
78+
c.output_format.unwrap_or(OutputFormat::Markdown)
79+
} else {
80+
OutputFormat::Markdown
81+
};
82+
configuration.output_format(output_format);
7583

7684
// Sort method: CLI overrides config
7785
let sort_method = if let Some(sort_str) = args.sort {
@@ -81,9 +89,9 @@ pub fn build_session(
8189
} else {
8290
FileSortMethod::NameAsc
8391
};
84-
8592
configuration.sort_method(sort_method);
8693

94+
// Tokenizer settings: CLI overrides config
8795
let tokenizer_type = if let Some(encoding) = args.encoding {
8896
encoding
8997
} else if let Some(c) = cfg {
@@ -92,6 +100,7 @@ pub fn build_session(
92100
TokenizerType::Cl100kBase
93101
};
94102

103+
// Token format: CLI overrides config
95104
let token_format = if let Some(format) = args.token_format {
96105
format
97106
} else if let Some(c) = cfg {

crates/code2prompt/tests/config_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod common;
77

88
use assert_cmd::Command;
99
use code2prompt_core::sort::FileSortMethod;
10+
use code2prompt_core::template::OutputFormat;
1011
use common::*;
1112
use predicates::prelude::*;
1213
use predicates::str::contains;
@@ -53,7 +54,7 @@ author = "ODAncona"
5354
assert!(config.line_numbers);
5455
assert!(!config.absolute_path);
5556
assert!(!config.full_directory_tree);
56-
assert_eq!(config.output_format, Some("markdown".to_string()));
57+
assert_eq!(config.output_format, Some(OutputFormat::Markdown));
5758
assert_eq!(config.sort_method, Some(FileSortMethod::NameAsc));
5859
assert_eq!(
5960
config.encoding,

0 commit comments

Comments
 (0)