batch_mode_batch_workspace_interface/
workspace_interface.rs

1// ---------------- [ File: batch-mode-batch-workspace-interface/src/workspace_interface.rs ]
2crate::ix!();
3
4#[async_trait]
5pub trait BatchWorkspaceInterface
6: GetInputFilenameAtIndex
7+ LoadSeedByCustomId
8+ GetTargetDir
9+ GetTargetDirectoryFiles
10+ FindSimilarTargetPath
11+ GetOutputFilenameAtIndex
12+ GetErrorFilenameAtIndex
13+ GetMetadataFilenameAtIndex
14+ GetSeedManifestFilenameAtIndex
15+ GetDoneDirectory
16+ GetFailedJsonRepairsDir
17+ GetFailedItemsDir
18+ GetTextStoragePath
19+ GetWorkdir
20+ Send
21+ Sync
22+ Debug
23+ GetTargetPath<Item = Arc<dyn GetTargetPathForAIExpansion + Send + Sync + 'static>>
24{}
25
26#[async_trait]
27pub trait LoadSeedByCustomId {
28    /// Return the original seed whose request carried `custom_id`.
29    async fn load_seed_by_custom_id(
30        &self,
31        custom_id: &CustomRequestId,
32    ) -> Result<Box<dyn Named + Send + Sync>, BatchWorkspaceError>;
33}
34
35pub trait GetTargetDir {
36
37    fn get_target_dir(&self) -> PathBuf;
38}
39
40//--------------------------------------------------
41pub trait GetTargetDirectoryFiles {
42
43    fn get_target_directory_files(&self) -> Vec<PathBuf>;
44}
45
46impl<W> GetTargetDirectoryFiles for W 
47where W: GetTargetDir
48{
49    fn get_target_directory_files(&self) -> Vec<PathBuf> {
50        // Example implementation: scan the target directory for existing files
51        std::fs::read_dir(&self.get_target_dir())
52            .unwrap()
53            .filter_map(|entry| entry.ok().map(|e| e.path()))
54            .collect()
55    }
56}
57
58//--------------------------------------------------
59pub trait FindSimilarTargetPath {
60
61    fn find_similar_target_path(&self, target_path: &Path) -> Option<PathBuf>;
62}
63
64impl<W> FindSimilarTargetPath for W 
65where W: GetTargetDirectoryFiles
66{
67    fn find_similar_target_path(&self, target_path: &Path) -> Option<PathBuf> {
68
69        use strsim::levenshtein;
70
71        let existing_paths = self.get_target_directory_files();
72        let target_str     = target_path.to_string_lossy();
73
74        existing_paths
75            .iter()
76            .find(|&existing| levenshtein(&target_str, &existing.to_string_lossy()) <= 2)
77            .cloned()
78    }
79}
80
81//--------------------------------------------------
82pub trait GetInputFilenameAtIndex {
83
84    fn input_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
85}
86
87pub trait GetOutputFilenameAtIndex {
88
89    fn output_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
90}
91
92pub trait GetErrorFilenameAtIndex {
93
94    fn error_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
95}
96
97pub trait GetMetadataFilenameAtIndex {
98
99    fn metadata_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
100}
101
102pub trait GetSeedManifestFilenameAtIndex {
103
104    fn seed_manifest_filename(&self, batch_idx: &BatchIndex) -> PathBuf;
105}
106
107pub trait GetDoneDirectory {
108
109    fn get_done_directory(&self) -> &PathBuf;
110}
111
112pub trait GetTargetPath {
113
114    type Item;
115
116    fn target_path(
117        &self,
118        item:            &Self::Item, 
119        expected_content_type: &ExpectedContentType
120    ) -> PathBuf;
121}
122
123pub trait GetFailedJsonRepairsDir {
124
125    fn failed_json_repairs_dir(&self) -> PathBuf;
126}
127
128pub trait GetFailedItemsDir {
129
130    fn failed_items_dir(&self) -> PathBuf;
131}
132
133pub trait GetTextStoragePath {
134
135    fn text_storage_path(&self, batch_idx: &BatchIndex) -> PathBuf;
136}
137
138pub trait GetWorkdir {
139
140    fn workdir(&self) -> PathBuf;
141}
142
143pub trait GetTargetPathForAIExpansion {
144
145    fn target_path_for_ai_json_expansion(
146        &self, 
147        target_dir:            &Path,
148        expected_content_type: &ExpectedContentType,
149
150    ) -> PathBuf;
151}
152
153impl<T:Named> GetTargetPathForAIExpansion for T {
154
155    fn target_path_for_ai_json_expansion(
156        &self, 
157        target_dir:            &Path,
158        _expected_content_type: &ExpectedContentType,
159
160    ) -> PathBuf {
161
162        // Convert 'token_name' to snake_case
163        let snake_token_name = to_snake_case(&self.name());
164
165        // Determine the output filename based on custom_id
166        // You can customize this as needed, e.g., using token names
167        let filename = format!("{}.json", snake_token_name);
168
169        target_dir.to_path_buf().join(filename)
170    }
171}
172
173//-------------------------------------------------------
174pub trait HasAssociatedOutputName {
175
176    fn associated_output_name(&self) -> std::borrow::Cow<'_, str>;
177}
178
179pub trait GetTargetPathForAIExpansionFromSeed {
180
181    fn target_path_for_ai_json_expansion_from_seed(
182        &self, 
183        target_dir:            &Path,
184        expected_content_type: &ExpectedContentType,
185
186    ) -> PathBuf;
187}
188
189impl<T:Named+HasAssociatedOutputName> GetTargetPathForAIExpansionFromSeed for T {
190
191    fn target_path_for_ai_json_expansion_from_seed(
192        &self, 
193        target_dir:            &Path,
194        _expected_content_type: &ExpectedContentType,
195
196    ) -> PathBuf {
197
198        // Convert 'token_name' to snake_case
199        let snake_token_name = to_snake_case(&self.associated_output_name());
200
201        // Determine the output filename based on custom_id
202        // You can customize this as needed, e.g., using token names
203        let filename = format!("{}.json", snake_token_name);
204
205        target_dir.to_path_buf().join(filename)
206    }
207}