Skip to content

Commit

Permalink
style: improve code readability
Browse files Browse the repository at this point in the history
This commit removes redundant comments and empty lines or adds empty lines
where it makes function bodies more readable.
  • Loading branch information
ekkolon committed Mar 28, 2024
1 parent 546c95d commit 193e94f
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/ops/work_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ where

let result = (*self.function)(&mut self.context, response);
let completed = self.results.send(result).is_err();

self.completed = completed;
}
}
Expand Down
23 changes: 1 addition & 22 deletions src/ops/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ where
"Licensa is already initialized in the current directory",
));
}

Ok(())
}
pub fn throw_no_workspace_config_exists<P>(workspace_root: P) -> Result<()>
Expand Down Expand Up @@ -266,29 +267,19 @@ mod tests {

#[test]
fn test_write_config_file_successful() {
// Create a temporary directory for testing
let temp_dir = tempdir().expect("Failed to create temporary directory");

// Define the target directory
let target_dir = temp_dir.path();

// Define the output file path
let config_file_path = target_dir.join(DEFAULT_CONFIG_FILENAME);

// Create a sample configuration
let sample_config = ExampleWorkspace {
prop1: "hello world".to_string(),
prop2: 1234,
};

// Test writing the config file
let write_result = save_workspace_config(target_dir, &sample_config);
assert!(write_result.is_ok());

// Verify that the config file exists
assert!(config_file_path.exists());

// Verify the content of the config file
let mut file = File::open(&config_file_path).expect("Failed to open config file");
let mut file_content = String::new();
file.read_to_string(&mut file_content)
Expand All @@ -304,18 +295,12 @@ mod tests {

#[test]
fn test_write_config_file_existing_file() {
// Create a temporary directory for testing
let temp_dir = tempdir().expect("Failed to create temporary directory");

// Define the target directory
let target_dir = temp_dir.path();

// Create an existing config file in the temporary directory
let existing_config_filename = POSSIBLE_CONFIG_FILENAMES[0];
let existing_config_path = target_dir.join(existing_config_filename);
File::create(&existing_config_path).expect("Failed to create existing config file");

// Create a sample configuration
let sample_config = ExampleWorkspace {
prop1: "hello world".to_string(),
prop2: 1234,
Expand All @@ -332,18 +317,12 @@ mod tests {

#[test]
fn test_find_config_file_single_file_exists() {
// Create a temporary directory for testing
let temp_dir = tempdir().expect("Failed to create temporary directory");

// Define the target directory
let target_dir = temp_dir.path();

// Create a sample config file in the temporary directory
let sample_config_filename = POSSIBLE_CONFIG_FILENAMES[0];
let sample_config_path = target_dir.join(sample_config_filename);
File::create(&sample_config_path).expect("Failed to create sample config file");

// Test finding the config file
let result: Result<Value> = resolve_workspace_config(target_dir);
assert!(result.is_err());

Expand Down
5 changes: 1 addition & 4 deletions src/template/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ where
/// * `item` - An item implementing the `Cachable` trait.
pub fn set(&mut self, item: T) {
let mut cache = self.inner.lock().unwrap();

let cache_id = item.cache_id();

cache.entry(cache_id).or_insert_with(|| Arc::new(item));
}

Expand All @@ -50,9 +48,7 @@ where
/// * `item` - An item implementing the `Cachable` trait.
pub fn add(&self, item: T) {
let mut cache = self.inner.lock().unwrap();

let cache_id = item.cache_id();

cache.entry(cache_id).or_insert_with(|| Arc::new(item));
}

Expand Down Expand Up @@ -158,6 +154,7 @@ mod tests {
pub template: String,
pub extension: String,
}

impl Cachable for TemplateItem {
fn cache_id(&self) -> String {
self.extension.clone()
Expand Down
8 changes: 0 additions & 8 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,10 @@ where
///
/// Returns an error if there are issues creating or writing to the file.
pub fn write_json<P: AsRef<Path>>(file_path: P, json_data: &serde_json::Value) -> Result<()> {
// Create or open the file for writing
let mut file = File::create(&file_path)?;

// Serialize the JSON data to a pretty-printed string
let json_string = serde_json::to_string_pretty(json_data)?;

// Write the pretty-printed JSON string to the file
file.write_all(json_string.as_bytes())?;

// Flush the buffer to ensure the data is written to the file
file.flush()?;

Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion src/workspace/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ where
{
let workspace_root = workspace_root.as_ref();
ensure_dir(workspace_root)?;

if let Some(path) = resolve_config_path(workspace_root, file_name) {
let content =
fs::read_to_string(path).with_context(|| "failed to read .licensarc config file")?;
Expand Down Expand Up @@ -193,18 +194,20 @@ where
{
let workspace_root = workspace_root.as_ref();
ensure_dir(workspace_root)?;

let config = serde_json::to_value(config.borrow())?;
// Ensure config is an object
if !config.is_object() {
let err = anyhow!(WorkspaceError::InvalidConfigDataType)
.context("failed to save workspace config file");
return Err(err.into());
}

let config = remove_null_fields(config);
let config = serde_json::to_string_pretty(&config)
.with_context(|| "failed to serialize .licensarc config file")?;
let out_path = workspace_root.join(file_name.as_ref());
fs::write(out_path, config).with_context(|| "failed to save .licensarc config file")?;

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/workspace/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ mod tests {
// Act
let rx = walker.run_task();

// Ensure config is an object
// Assert
// Add assertions for receiving results from the workspace walk
}
Expand Down

0 comments on commit 193e94f

Please sign in to comment.