Skip to content

Commit a7b50e8

Browse files
committed
Avoid returning String when callers expect &str
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent d6c0a27 commit a7b50e8

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

crates/core/tedge_agent/src/operation_workflows/persist.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,7 @@ impl WorkflowRepository {
261261

262262
/// Copy the workflow definition file to the persisted state directory,
263263
/// unless this has already been done.
264-
async fn persist_workflow_definition(
265-
&mut self,
266-
operation: &OperationName,
267-
version: &WorkflowVersion,
268-
) {
264+
async fn persist_workflow_definition(&mut self, operation: &str, version: &str) {
269265
if version_is_builtin(version) {
270266
return;
271267
}
@@ -279,16 +275,12 @@ impl WorkflowRepository {
279275
if let Err(err) = tokio::fs::copy(source.clone(), target.clone()).await {
280276
error!("Fail to persist a copy of {source} as {target}: {err}");
281277
} else {
282-
self.in_use_copies.insert(version.clone(), 1);
278+
self.in_use_copies.insert(version.to_owned(), 1);
283279
}
284280
}
285281
}
286282

287-
fn workflow_copy_path(
288-
&self,
289-
operation: &OperationName,
290-
version: &WorkflowVersion,
291-
) -> Utf8PathBuf {
283+
fn workflow_copy_path(&self, operation: &str, version: &str) -> Utf8PathBuf {
292284
let filename = format!("{operation}-{version}");
293285
self.state_dir.join(filename).with_extension("toml")
294286
}
@@ -305,7 +297,7 @@ impl WorkflowRepository {
305297
}
306298
}
307299

308-
async fn release_in_use_copy(&mut self, operation: &OperationName, version: &WorkflowVersion) {
300+
async fn release_in_use_copy(&mut self, operation: &str, version: &str) {
309301
if version_is_builtin(version) {
310302
return;
311303
}
@@ -410,14 +402,14 @@ impl WorkflowRepository {
410402
operation: &OperationType,
411403
command_state: GenericCommandState,
412404
) -> Result<Option<GenericCommandState>, WorkflowExecutionError> {
405+
let operation_name = operation.to_string();
413406
if command_state.is_init() {
414407
// A new command instance must use the latest on-disk version of the operation workflow
415-
self.load_latest_version(&operation.to_string()).await;
408+
self.load_latest_version(&operation_name).await;
416409
} else if command_state.is_finished() {
417410
// Clear the cache if this happens to be the latest instance using that version of the workflow
418411
if let Some(version) = command_state.workflow_version() {
419-
self.release_in_use_copy(&operation.to_string(), &version)
420-
.await;
412+
self.release_in_use_copy(&operation_name, version).await;
421413
}
422414
}
423415

@@ -429,7 +421,7 @@ impl WorkflowRepository {
429421

430422
Some(new_state) if new_state.is_init() => {
431423
if let Some(version) = new_state.workflow_version() {
432-
self.persist_workflow_definition(&operation.to_string(), &version)
424+
self.persist_workflow_definition(&operation_name, version)
433425
.await;
434426
}
435427
Ok(Some(new_state))

crates/core/tedge_api/src/workflow/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub type WorkflowVersion = String;
3232

3333
const BUILT_IN: &str = "builtin";
3434

35-
pub fn version_is_builtin(version: &WorkflowVersion) -> bool {
35+
pub fn version_is_builtin(version: &str) -> bool {
3636
version == BUILT_IN
3737
}
3838

crates/core/tedge_api/src/workflow/state.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ impl GenericCommandState {
193193

194194
fn set_key_value(&mut self, key: &str, val: &str) {
195195
if let Some(o) = self.payload.as_object_mut() {
196-
o.insert(key.to_string(), val.into());
196+
o.insert(key.into(), val.into());
197197
}
198198
if key == STATUS {
199-
self.status = val.to_string();
199+
self.status = val.to_owned();
200200
}
201201
}
202202

@@ -216,11 +216,10 @@ impl GenericCommandState {
216216
self.set_key_value(OP_LOG_PATH_KEY, path.as_ref().as_str())
217217
}
218218

219-
pub fn workflow_version(&self) -> Option<String> {
219+
pub fn workflow_version(&self) -> Option<&str> {
220220
self.payload
221221
.get(OP_WORKFLOW_VERSION_KEY)
222222
.and_then(|val| val.as_str())
223-
.map(|str| str.to_string())
224223
}
225224

226225
pub fn with_workflow_version(mut self, version: &str) -> Self {

crates/core/tedge_api/src/workflow/supervisor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl WorkflowSupervisor {
137137
/// Return the current version if any.
138138
pub fn use_current_version(&mut self, operation: &OperationName) -> Option<WorkflowVersion> {
139139
self.workflows
140-
.get_mut(&operation.as_str().into())
141-
.map(WorkflowVersions::use_current_version)?
140+
.get_mut(&operation.as_str().into())?
141+
.use_current_version()
142142
.cloned()
143143
}
144144

@@ -192,7 +192,7 @@ impl WorkflowSupervisor {
192192
});
193193
};
194194

195-
let Some(version) = &command_state.workflow_version() else {
195+
let Some(version) = command_state.workflow_version() else {
196196
return Err(WorkflowExecutionError::MissingVersion);
197197
};
198198

@@ -421,7 +421,7 @@ impl WorkflowVersions {
421421
self.in_use.contains_key(BUILT_IN)
422422
}
423423

424-
fn get(&self, version: &WorkflowVersion) -> Result<&OperationWorkflow, WorkflowExecutionError> {
424+
fn get(&self, version: &str) -> Result<&OperationWorkflow, WorkflowExecutionError> {
425425
self.in_use
426426
.get(version)
427427
.ok_or(WorkflowExecutionError::UnknownVersion {

0 commit comments

Comments
 (0)