Module util

Module util 

Source
Expand description

§Common utilities

This module contains common functions and data structures used to build the ACORN command line interface as well as support open science endeavors.

§Example Uses

§Work with semantic versions

use acorn_lib::util::SemanticVersion;

let version = SemanticVersion::from_string("1.2.3");
assert_eq!(version.minor, 2);

if let Some(version) = SemanticVersion::from_command("cargo") {
    println!("cargo version: {version}");
}

§Perform file read and write operations

use acorn_lib::util::{checksum, read_file, write_file};
use std::path::PathBuf;

// Verify file integrity
assert_eq!(checksum(PathBuf::from("/path/to/file")), "somesha256hashvaluethatisreallylong");

// Read file contents
let contents = read_file(PathBuf::from("/path/to/this/file"));

// Write file contents
write_file(PathBuf::from("/path/to/that/file"), contents);

Modules§

citeas
Module for communicating with CiteAs API
cli
Command line interface (CLI) utilities

Structs§

Constant
Struct for using and sharing constants
GitlabMergeRequestDiffResponse
Struct for parsing GitLab API merge request diff responses
Label
Struct for using and sharing colorized logging labels
SemanticVersion
Semantic version
SemanticVersionBuilder
Use builder syntax to set the inputs and finish with build().
StringList
Struct for adding ToStringList functionality

Enums§

License
SPDX compliant license identifier
MimeType
Supports an incomplete list of common MIME types

Traits§

ToAbsoluteString
Trait for augmenting path value functionality with absolute path string conversion
ToStringChunks
Trait for adding chunking functionality
ToStrings
Trait for converting a vector of PathBufs to a vector of strings

Functions§

checksum
Get SHA256 hash of a file
command_exists
Checks if a given command exists in current terminal context.
download_binary
Downloads a binary file from the given URL to the destination path.
extension
Get file extension
extract_zip
Extract zip archive
files_all
Returns a vector of PathBuf containing all files in a directory that match at least one of the given extensions.
files_from_git_branch
Returns a vector of PathBuf containing all files changed in the given Git branch relative to the default branch.
files_from_git_commit
Returns a vector of PathBuf containing all files changed in the given Git commit.
files_from_gitlab_merge_request
Returns a vector of PathBuf containing all files changed in a GitLab merge request, as determined by the CI_API_V4_URL, CI_MERGE_REQUEST_PROJECT_ID, and CI_MERGE_REQUEST_IID environment variables.
filter_ignored
Return file paths in a vector that don’t match the ignore pattern
find_first
Return fisrt key/value pair with key that matches pattern
generate_guid
Generates a random GUID using a custom alphabet.
git_branch_name
Returns the current Git branch name if the git command is available and executed successfully.
git_default_branch_name
Returns the default Git branch name if the git command is available and executed successfully.
image_paths
Returns a vector of PathBuf representing paths to all images found in the given directory and all of its subdirectories.
make_executable
Makes the given file executable.
parent
Returns the absolute path of the parent directory for the given path.
print_changes
Prints a diff of changes between two strings.
print_values_as_table
Prints the given values as a table.
read_file
Reads the given file and returns its contents as a string.
regex_capture_lookup
Helper function to create a lookup dictionary for regex captures
reqwest_request
Utility method to employ best practices when using a Reqwest client to make HTTP requests.
snake_case
Converts the given string to snake case.
standard_project_folder
Returns path to a folder in the operating system’s cache directory that is unique to the given namespace with a random UUID as the name of the final folder.
suffix
Returns “s” if the given value is not 1, otherwise returns an empty string.
text_diff_changes
Computes the differences between two strings line by line and returns a vector of changes.
to_absolute_string
Converts a PathBuf into a String representation of the absolute path.
to_string
Convert a vector of string slices to a vector of strings
tokio_runtime
Create a new Tokio runtime
write_file
Writes the given content to a file at the given path.