#utility #file-utility #execution #library #read #reader #random

best_skn_utils

A Simple Utility Library For Rust

6 stable releases

1.2.3 Aug 17, 2024
1.2.0 Jul 13, 2024
1.1.0 Jun 19, 2024
1.0.0 Jun 9, 2024

#112 in Configuration

MIT license

17KB
51 lines

SKN Rust Utility Library

rust

Rust

Crates IO MIT License

 

RustDocs:

Read the Rustdoc for the main Modules

 

Introduction:

This is a simple Rust Library for some essential utility functions

I made this library so that I can use it in all of my rust projects without writing the same codes over and over again

The main Modules of this library are env, execution, stdio, random & args

 

Details:

env Module:

  • It has 1 function which builds external config file functionality to get data from outside the program
  • init_config function takes generic
  • The file name is rustenv.toml from which you can get data after invoking the function
  • The config file rustenv.toml must be placed in the root directory where Cargo.toml file is
  • To read values from the file properly you need serde crate
  • See Usage section to get an example of how to use it

execution Module

  • It has 2 functions which help to execute commands in the terminal
  • execute_command function can run any command in the terminal
    • it takes the command name as first argument
    • it takes a reference of string slice (&str) array as command arguments
  • gnome_execute_command function can open a new gnome terminal and executes commands in it
    • it takes only one argument which is a string slice (&str)
    • if multiple command needs to be executed then the commands must be separated by ;
  • See Usage section to get an example of how to use it

stdio Module

  • It has 1 function which reads user input and returns it
  • read_line function returns String if it successfully reads the input else returns Error
  • See Usage section to get an example of how to use it

random Module

  • It has 1 function that generates random number from a given range
  • gen_random_number function takes two parameters to set a range. One is low and the other one is high
  • The parameters can either be Integer or Float
  • parameter low and high must be of same type i.e. you cannot set a range of say from 1 to 10.1
  • The second parameter high is inclusive i.e. a range of 1 and 10 will mean the range includes from 1 to 10
  • See Usage section to get an example of how to use it

args Module

  • It has 1 function that provides a collection of arguments passed in command line interface
  • get_args function returns a collection as a vector of String
  • See Usage section to get an example of how to use it

 

Use Case:

  • Rust

 

Requirements:

  • ๐Ÿ’€ Minimum Rust Version: 1.80.0
  • ๐Ÿ’€ Crates:

 

Usage:

To install the package, type the following in console

cargo add best_skn_utils

Inside your Rust Code, import the package like this

use best_skn_utils::{env, execution, stdio, random, args};

Use the modules like the following (Just an example)

(1) For env module, you can use like this

(a) Suppose the rustenv.toml file contains the data like this
[author]
name = "SKN"
email = "skn437physx@gmail.com"
(b) Then the usage of the module can be like this
use best_skn_utils::env::init_config;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Author {
  name: String,
  email: String,
}

impl Author {
  fn new() -> Self {
    Self {
      name: String::new(),
      email: String::new(),
    }
  }
}

#[derive(Debug, Deserialize)]
struct ConfigData {
  author: Author,
}

impl ConfigData {
  fn new() -> Self {
    let config = init_config::<Self>();

    match config {
      | Ok(value) => value,
      | Err(e) => {
        println!("Error: {}", e);
        Self {
          author: Author::new(),
        }
      }
    }
  }
}

let config_data: ConfigData = ConfigData::new();

println!("Name: {}, Email: {}", config_data.author.name, config_data.author.email);

(2) For execution module, you can use like this

use best_skn_utils::execution::{execute_command, gnome_execute_command};

execute_command("cargo", &["doc", "--open"]);

gnome_execute_command("printf 'Hello SKN! \n'; printf 'Build was successful! โœ… \n'; read -n 1 KEY");

(3) For stdio module, you can use like this

use best_skn_utils::stdio::read_line;
use std::io::Error;

let input: Result<String, Error> = read_line("Write your name:");

match input {
  | Ok(value) => println!("Your name: {}", value),
  | Err(e) => println!("Error: {}", e),
}

(4) For random module, you can use like this

use best_skn_utils::random::gen_random_number;

let num1: i32 = gen_random_number(1, 10);
let num2: f64 = gen_random_number(1.5, 7.5);

(5) For args module, you can use like this

use best_skn_utils::args::get_args;

let args: Vec<String> = get_args();

println!("{:?}", args);

 

Dedicated To:

  • ๐Ÿ‘ฉโ€โš•๏ธTanjila Hasan Trina: The long lost love of my life. The course of nature separated us from our paths and put us in separate places far away from each other. But no matter how separated we are right now, each and every moment of mine is only dedicated to you. We may not see each other in this lifetime as it seems but I will find you again in the next life. I just want to say: ไธ–็•Œใฏๆฎ‹้…ทใ  ใใ‚Œใงใ‚‚ๅ›ใ‚’ๆ„›ใ™ใ‚ˆ
  • ๐Ÿ’ฏMy Parents: The greatest treasures of my life ever.

 

License:

Copyright (C) 2024 SKN Shukhan

Licensed under the MIT License

Dependencies

~4โ€“12MB
~135K SLoC