Skip to content

Commit 14456fa

Browse files
author
Olivier D'Ancona
committed
static builtin strings
1 parent 65c8219 commit 14456fa

4 files changed

Lines changed: 141 additions & 168 deletions

File tree

crates/code2prompt-core/src/builtin_templates.rs

Lines changed: 134 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -4,177 +4,148 @@
44
//! directly into the binary, making them available even when the crate is
55
//! installed from crates.io without access to the source file structure.
66
7-
use std::collections::HashMap;
7+
use std::{collections::HashMap, sync::OnceLock};
88

99
/// Information about a built-in template
10-
#[derive(Debug, Clone)]
10+
#[derive(Debug, Clone, Copy)]
1111
pub struct BuiltinTemplate {
12-
pub name: String,
13-
pub content: String,
14-
pub description: String,
12+
pub name: &'static str,
13+
pub content: &'static str,
14+
pub description: &'static str,
1515
}
1616

1717
/// All built-in templates embedded as static strings
1818
pub struct BuiltinTemplates;
1919

20+
static TEMPLATES: OnceLock<HashMap<&'static str, BuiltinTemplate>> = OnceLock::new();
21+
2022
impl BuiltinTemplates {
2123
/// Get all available built-in templates
22-
pub fn get_all() -> HashMap<String, BuiltinTemplate> {
23-
let mut templates = HashMap::new();
24-
25-
// Default Markdown Template
26-
templates.insert(
27-
"default-markdown".to_string(),
28-
BuiltinTemplate {
29-
name: "Default (Markdown)".to_string(),
30-
content: include_str!("default_template_md.hbs").to_string(),
31-
description: "Default markdown template for code analysis".to_string(),
32-
},
33-
);
34-
35-
// Default XML Template
36-
templates.insert(
37-
"default-xml".to_string(),
38-
BuiltinTemplate {
39-
name: "Default (XML)".to_string(),
40-
content: include_str!("default_template_xml.hbs").to_string(),
41-
description: "Default XML template for code analysis".to_string(),
42-
},
43-
);
44-
45-
// Binary Exploitation CTF Solver
46-
templates.insert(
47-
"binary-exploitation-ctf-solver".to_string(),
48-
BuiltinTemplate {
49-
name: "Binary Exploitation CTF Solver".to_string(),
50-
content: include_str!("../templates/binary-exploitation-ctf-solver.hbs")
51-
.to_string(),
52-
description: "Template for solving binary exploitation CTF challenges".to_string(),
53-
},
54-
);
55-
56-
// Clean Up Code
57-
templates.insert(
58-
"clean-up-code".to_string(),
59-
BuiltinTemplate {
60-
name: "Clean Up Code".to_string(),
61-
content: include_str!("../templates/clean-up-code.hbs").to_string(),
62-
description: "Template for code cleanup and refactoring".to_string(),
63-
},
64-
);
65-
66-
// Cryptography CTF Solver
67-
templates.insert(
68-
"cryptography-ctf-solver".to_string(),
69-
BuiltinTemplate {
70-
name: "Cryptography CTF Solver".to_string(),
71-
content: include_str!("../templates/cryptography-ctf-solver.hbs").to_string(),
72-
description: "Template for solving cryptography CTF challenges".to_string(),
73-
},
74-
);
75-
76-
// Document the Code
77-
templates.insert(
78-
"document-the-code".to_string(),
79-
BuiltinTemplate {
80-
name: "Document the Code".to_string(),
81-
content: include_str!("../templates/document-the-code.hbs").to_string(),
82-
description: "Template for generating code documentation".to_string(),
83-
},
84-
);
85-
86-
// Find Security Vulnerabilities
87-
templates.insert(
88-
"find-security-vulnerabilities".to_string(),
89-
BuiltinTemplate {
90-
name: "Find Security Vulnerabilities".to_string(),
91-
content: include_str!("../templates/find-security-vulnerabilities.hbs").to_string(),
92-
description: "Template for security vulnerability analysis".to_string(),
93-
},
94-
);
95-
96-
// Fix Bugs
97-
templates.insert(
98-
"fix-bugs".to_string(),
99-
BuiltinTemplate {
100-
name: "Fix Bugs".to_string(),
101-
content: include_str!("../templates/fix-bugs.hbs").to_string(),
102-
description: "Template for bug fixing and debugging".to_string(),
103-
},
104-
);
105-
106-
// Improve Performance
107-
templates.insert(
108-
"improve-performance".to_string(),
109-
BuiltinTemplate {
110-
name: "Improve Performance".to_string(),
111-
content: include_str!("../templates/improve-performance.hbs").to_string(),
112-
description: "Template for performance optimization".to_string(),
113-
},
114-
);
115-
116-
// Refactor
117-
templates.insert(
118-
"refactor".to_string(),
119-
BuiltinTemplate {
120-
name: "Refactor".to_string(),
121-
content: include_str!("../templates/refactor.hbs").to_string(),
122-
description: "Template for code refactoring".to_string(),
123-
},
124-
);
125-
126-
// Reverse Engineering CTF Solver
127-
templates.insert(
128-
"reverse-engineering-ctf-solver".to_string(),
129-
BuiltinTemplate {
130-
name: "Reverse Engineering CTF Solver".to_string(),
131-
content: include_str!("../templates/reverse-engineering-ctf-solver.hbs")
132-
.to_string(),
133-
description: "Template for solving reverse engineering CTF challenges".to_string(),
134-
},
135-
);
136-
137-
// Web CTF Solver
138-
templates.insert(
139-
"web-ctf-solver".to_string(),
140-
BuiltinTemplate {
141-
name: "Web CTF Solver".to_string(),
142-
content: include_str!("../templates/web-ctf-solver.hbs").to_string(),
143-
description: "Template for solving web CTF challenges".to_string(),
144-
},
145-
);
146-
147-
// Write Git Commit
148-
templates.insert(
149-
"write-git-commit".to_string(),
150-
BuiltinTemplate {
151-
name: "Write Git Commit".to_string(),
152-
content: include_str!("../templates/write-git-commit.hbs").to_string(),
153-
description: "Template for generating git commit messages".to_string(),
154-
},
155-
);
156-
157-
// Write GitHub Pull Request
158-
templates.insert(
159-
"write-github-pull-request".to_string(),
160-
BuiltinTemplate {
161-
name: "Write GitHub Pull Request".to_string(),
162-
content: include_str!("../templates/write-github-pull-request.hbs").to_string(),
163-
description: "Template for generating GitHub pull request descriptions".to_string(),
164-
},
165-
);
166-
167-
// Write GitHub README
168-
templates.insert(
169-
"write-github-readme".to_string(),
170-
BuiltinTemplate {
171-
name: "Write GitHub README".to_string(),
172-
content: include_str!("../templates/write-github-readme.hbs").to_string(),
173-
description: "Template for generating GitHub README files".to_string(),
174-
},
175-
);
176-
177-
templates
24+
pub fn get_all() -> &'static HashMap<&'static str, BuiltinTemplate> {
25+
TEMPLATES.get_or_init(|| {
26+
HashMap::from([
27+
(
28+
"default-markdown",
29+
BuiltinTemplate {
30+
name: "Default (Markdown)",
31+
content: include_str!("default_template_md.hbs"),
32+
description: "Default markdown template for code analysis",
33+
},
34+
),
35+
(
36+
"default-xml",
37+
BuiltinTemplate {
38+
name: "Default (XML)",
39+
content: include_str!("default_template_xml.hbs"),
40+
description: "Default XML template for code analysis",
41+
},
42+
),
43+
(
44+
"binary-exploitation-ctf-solver",
45+
BuiltinTemplate {
46+
name: "Binary Exploitation CTF Solver",
47+
content: include_str!("../templates/binary-exploitation-ctf-solver.hbs"),
48+
description: "Template for solving binary exploitation CTF challenges",
49+
},
50+
),
51+
(
52+
"clean-up-code",
53+
BuiltinTemplate {
54+
name: "Clean Up Code",
55+
content: include_str!("../templates/clean-up-code.hbs"),
56+
description: "Template for code cleanup and refactoring",
57+
},
58+
),
59+
(
60+
"cryptography-ctf-solver",
61+
BuiltinTemplate {
62+
name: "Cryptography CTF Solver",
63+
content: include_str!("../templates/cryptography-ctf-solver.hbs"),
64+
description: "Template for solving cryptography CTF challenges",
65+
},
66+
),
67+
(
68+
"document-the-code",
69+
BuiltinTemplate {
70+
name: "Document the Code",
71+
content: include_str!("../templates/document-the-code.hbs"),
72+
description: "Template for generating code documentation",
73+
},
74+
),
75+
(
76+
"find-security-vulnerabilities",
77+
BuiltinTemplate {
78+
name: "Find Security Vulnerabilities",
79+
content: include_str!("../templates/find-security-vulnerabilities.hbs"),
80+
description: "Template for security vulnerability analysis",
81+
},
82+
),
83+
(
84+
"fix-bugs",
85+
BuiltinTemplate {
86+
name: "Fix Bugs",
87+
content: include_str!("../templates/fix-bugs.hbs"),
88+
description: "Template for bug fixing and debugging",
89+
},
90+
),
91+
(
92+
"improve-performance",
93+
BuiltinTemplate {
94+
name: "Improve Performance",
95+
content: include_str!("../templates/improve-performance.hbs"),
96+
description: "Template for performance optimization",
97+
},
98+
),
99+
(
100+
"refactor",
101+
BuiltinTemplate {
102+
name: "Refactor",
103+
content: include_str!("../templates/refactor.hbs"),
104+
description: "Template for code refactoring",
105+
},
106+
),
107+
(
108+
"reverse-engineering-ctf-solver",
109+
BuiltinTemplate {
110+
name: "Reverse Engineering CTF Solver",
111+
content: include_str!("../templates/reverse-engineering-ctf-solver.hbs"),
112+
description: "Template for solving reverse engineering CTF challenges",
113+
},
114+
),
115+
(
116+
"web-ctf-solver",
117+
BuiltinTemplate {
118+
name: "Web CTF Solver",
119+
content: include_str!("../templates/web-ctf-solver.hbs"),
120+
description: "Template for solving web CTF challenges",
121+
},
122+
),
123+
(
124+
"write-git-commit",
125+
BuiltinTemplate {
126+
name: "Write Git Commit",
127+
content: include_str!("../templates/write-git-commit.hbs"),
128+
description: "Template for generating git commit messages",
129+
},
130+
),
131+
(
132+
"write-github-pull-request",
133+
BuiltinTemplate {
134+
name: "Write GitHub Pull Request",
135+
content: include_str!("../templates/write-github-pull-request.hbs"),
136+
description: "Template for generating GitHub pull request descriptions",
137+
},
138+
),
139+
(
140+
"write-github-readme",
141+
BuiltinTemplate {
142+
name: "Write GitHub README",
143+
content: include_str!("../templates/write-github-readme.hbs"),
144+
description: "Template for generating GitHub README files",
145+
},
146+
),
147+
])
148+
})
178149
}
179150

180151
/// Get a specific template by its key
@@ -183,8 +154,8 @@ impl BuiltinTemplates {
183154
}
184155

185156
/// Get all template keys
186-
pub fn get_template_keys() -> Vec<String> {
187-
Self::get_all().keys().cloned().collect()
157+
pub fn get_template_keys() -> Vec<&'static str> {
158+
Self::get_all().keys().copied().collect()
188159
}
189160

190161
/// Check if a template exists

crates/code2prompt/src/model/template/editor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ impl Default for EditorState {
4545
{
4646
builtin_template.content
4747
} else {
48-
// Fallback content if template not found
49-
"# {{project_name}}\n\n{{#if files}}\n{{#each files}}\n## {{path}}\n\n```{{extension}}\n{{content}}\n```\n\n{{/each}}\n{{/if}}".to_string()
48+
"# {{project_name}}\n\n{{#if files}}\n{{#each files}}\n## {{path}}\n\n```{{extension}}\n{{content}}\n```\n\n{{/each}}\n{{/if}}"
5049
};
5150

5251
let editor = TextArea::from(content.lines());
5352

5453
let mut state = Self {
55-
content: content.clone(),
54+
content: content.to_string(),
5655
editor,
5756
current_template_name: "Default (Markdown)".to_string(),
5857
is_valid: true,

crates/code2prompt/src/model/template/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ impl TemplateState {
131131
if let Some(builtin_template) =
132132
code2prompt_core::builtin_templates::BuiltinTemplates::get_template(template_key)
133133
{
134-
(builtin_template.content, builtin_template.name)
134+
(
135+
builtin_template.content.to_string(),
136+
builtin_template.name.to_string(),
137+
)
135138
} else {
136139
return Err(format!("Built-in template '{}' not found", template_key));
137140
}

crates/code2prompt/src/model/template/picker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl PickerState {
6464

6565
for (key, template) in template_entries {
6666
self.default_templates.push(TemplateFile {
67-
name: template.name,
67+
name: template.name.to_string(),
6868
path: PathBuf::from(format!("builtin://{}", key)),
6969
});
7070
}

0 commit comments

Comments
 (0)