batch_mode_batch_workspace_interface/
workspace_interface.rs1crate::ix!();
3
4pub trait BatchWorkspaceInterface
5: GetInputFilenameAtIndex
6+ GetTargetDir
7+ GetTargetDirectoryFiles
8+ FindSimilarTargetPath
9+ GetOutputFilenameAtIndex
10+ GetErrorFilenameAtIndex
11+ GetMetadataFilenameAtIndex
12+ GetDoneDirectory
13+ GetFailedJsonRepairsDir
14+ GetFailedItemsDir
15+ GetTextStoragePath
16+ GetWorkdir
17+ Send
18+ Sync
19+ Debug
20+ GetTargetPath<Item = Arc<dyn GetTargetPathForAIExpansion + Send + Sync + 'static>>
21{}
22
23pub trait GetTargetDir {
24 fn get_target_dir(&self) -> PathBuf;
25}
26
27pub trait GetTargetDirectoryFiles {
29 fn get_target_directory_files(&self) -> Vec<PathBuf>;
30}
31
32impl<W> GetTargetDirectoryFiles for W
33where W: GetTargetDir
34{
35 fn get_target_directory_files(&self) -> Vec<PathBuf> {
36 std::fs::read_dir(&self.get_target_dir())
38 .unwrap()
39 .filter_map(|entry| entry.ok().map(|e| e.path()))
40 .collect()
41 }
42}
43
44pub trait FindSimilarTargetPath {
46 fn find_similar_target_path(&self, target_path: &Path) -> Option<PathBuf>;
47}
48
49impl<W> FindSimilarTargetPath for W
50where W: GetTargetDirectoryFiles
51{
52 fn find_similar_target_path(&self, target_path: &Path) -> Option<PathBuf> {
53
54 use strsim::levenshtein;
55
56 let existing_paths = self.get_target_directory_files();
57 let target_str = target_path.to_string_lossy();
58
59 existing_paths
60 .iter()
61 .find(|&existing| levenshtein(&target_str, &existing.to_string_lossy()) <= 2)
62 .cloned()
63 }
64}
65
66pub trait GetInputFilenameAtIndex {
68 fn input_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
69
70}
71pub trait GetOutputFilenameAtIndex {
72 fn output_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
73}
74
75pub trait GetErrorFilenameAtIndex {
76 fn error_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
77}
78
79pub trait GetMetadataFilenameAtIndex {
80 fn metadata_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
81}
82
83pub trait GetDoneDirectory {
84 fn get_done_directory(&self) -> &PathBuf;
85}
86
87pub trait GetTargetPath {
88 type Item;
89 fn target_path(
90 &self,
91 item: &Self::Item,
92 expected_content_type: &ExpectedContentType
93 ) -> PathBuf;
94}
95
96pub trait GetFailedJsonRepairsDir {
97 fn failed_json_repairs_dir(&self) -> PathBuf;
98}
99
100pub trait GetFailedItemsDir {
101 fn failed_items_dir(&self) -> PathBuf;
102}
103
104pub trait GetTextStoragePath {
105 fn text_storage_path(&self, batch_idx: &BatchIndex) -> PathBuf;
106}
107
108pub trait GetWorkdir {
109 fn workdir(&self) -> PathBuf;
110}
111
112pub trait GetTargetPathForAIExpansion {
113
114 fn target_path_for_ai_json_expansion(
115 &self,
116 target_dir: &Path,
117 expected_content_type: &ExpectedContentType,
118
119 ) -> PathBuf;
120}
121
122impl<T:Named> GetTargetPathForAIExpansion for T {
123
124 fn target_path_for_ai_json_expansion(
125 &self,
126 target_dir: &Path,
127 _expected_content_type: &ExpectedContentType,
128
129 ) -> PathBuf {
130
131 let snake_token_name = to_snake_case(&self.name());
133
134 let filename = format!("{}.json", snake_token_name);
137
138 target_dir.to_path_buf().join(filename)
139 }
140}
141
142pub trait HasAssociatedOutputName {
144 fn associated_output_name(&self) -> std::borrow::Cow<'_, str>;
145}
146
147pub trait GetTargetPathForAIExpansionFromSeed {
148
149 fn target_path_for_ai_json_expansion_from_seed(
150 &self,
151 target_dir: &Path,
152 expected_content_type: &ExpectedContentType,
153
154 ) -> PathBuf;
155}
156
157impl<T:Named+HasAssociatedOutputName> GetTargetPathForAIExpansionFromSeed for T {
158
159 fn target_path_for_ai_json_expansion_from_seed(
160 &self,
161 target_dir: &Path,
162 _expected_content_type: &ExpectedContentType,
163
164 ) -> PathBuf {
165
166 let snake_token_name = to_snake_case(&self.associated_output_name());
168
169 let filename = format!("{}.json", snake_token_name);
172
173 target_dir.to_path_buf().join(filename)
174 }
175}