docs(project): added docstrings to all code files
This commit is contained in:
parent
47acdec22a
commit
0d354efb8e
24 changed files with 1826 additions and 107 deletions
|
|
@ -7,10 +7,22 @@ use tracing::debug;
|
|||
|
||||
use crate::config::GLOBAL_CONFIG;
|
||||
|
||||
/// Struct representing the command line arguments parsed by clap.
|
||||
///
|
||||
/// It uses the clap library to define the command line arguments and their
|
||||
/// attributes. The version of the application is obtained from the cargo.toml
|
||||
/// file.
|
||||
///
|
||||
/// The `command` field is an optional subcommand. It is represented by the
|
||||
/// `Commands` enum which defines the different subcommands that can be used.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version = env!("CARGO_PKG_VERSION"), about = "Send and receive files securely")]
|
||||
#[command(long_about = None)]
|
||||
pub struct Args {
|
||||
/// The subcommand to run.
|
||||
///
|
||||
/// It is an optional field. If it is not provided, the program will run without
|
||||
/// any specific subcommand.
|
||||
#[command(subcommand)]
|
||||
pub command: Option<Commands>,
|
||||
}
|
||||
|
|
@ -32,10 +44,6 @@ pub enum Commands {
|
|||
#[arg(short, long)]
|
||||
relay: Option<String>,
|
||||
|
||||
/// Overwrite existing Files
|
||||
#[arg(short, long)]
|
||||
overwrite: bool,
|
||||
|
||||
/// Name of Transfer to download files
|
||||
#[arg(value_name = "Transfer_Name")]
|
||||
name: String,
|
||||
|
|
@ -51,34 +59,69 @@ pub enum Commands {
|
|||
},
|
||||
}
|
||||
|
||||
|
||||
/// Default implementation of the `Args` struct.
|
||||
///
|
||||
/// This implementation uses the `new` method to create a new instance of `Args`.
|
||||
impl Default for Args {
|
||||
/// Creates a new instance of `Args` by calling the `new` method.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A new instance of `Args`.
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Struct representing the parsed command line arguments.
|
||||
///
|
||||
/// This struct implements the `Default` trait to create a new instance of `Args` by calling the
|
||||
/// `new` method.
|
||||
///
|
||||
/// The `run` method is used to execute the corresponding command based on the parsed arguments.
|
||||
impl Args {
|
||||
/// Creates a new instance of `Args` by calling the `parse` method.
|
||||
pub fn new() -> Self {
|
||||
Self::parse()
|
||||
}
|
||||
|
||||
/// Executes the corresponding command based on the parsed arguments.
|
||||
///
|
||||
/// This method takes no parameters.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A `Result` that either returns `Ok(())` indicating successful execution or an `Err`
|
||||
/// indicating an error.
|
||||
pub async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
// Retrieve the global configuration
|
||||
let cfg = &GLOBAL_CONFIG;
|
||||
debug!("args: {:#?}", self);
|
||||
|
||||
// Match on the `command` field of `Args` to execute the corresponding command
|
||||
match &self.command {
|
||||
// Command to send files to the receiver or relay server
|
||||
Some(Commands::Send { relay, files }) => {
|
||||
// Create a string representation of the relay address
|
||||
let relay_string: String = relay.as_deref().unwrap_or(&cfg.app_origin).to_string();
|
||||
// Create Arc wrappers for the relay address and file paths
|
||||
let relay_arc = Arc::new(relay_string);
|
||||
let files_arc = Arc::new(files.to_vec());
|
||||
// Generate a random name
|
||||
let rand_name = generate_random_name();
|
||||
// Start the sender with the generated name, relay address, and file paths
|
||||
sender::start_sender(rand_name, relay_arc, files_arc).await;
|
||||
}
|
||||
// Command to receive files from the sender with the matching password
|
||||
Some(Commands::Receive {
|
||||
relay,
|
||||
overwrite: _,
|
||||
name,
|
||||
}) => {
|
||||
// Print the received transfer name
|
||||
println!("Receive for {name:?}");
|
||||
// Start the receiver with the current directory, relay address, and transfer name
|
||||
let _ = receiver::start_receiver(
|
||||
".".to_string(),
|
||||
relay.as_deref().unwrap_or(&cfg.app_origin),
|
||||
|
|
@ -86,19 +129,23 @@ impl Args {
|
|||
)
|
||||
.await;
|
||||
}
|
||||
// Command to start a relay server
|
||||
Some(Commands::Serve {
|
||||
port,
|
||||
listen_address,
|
||||
}) => {
|
||||
println!("Serve with address '{listen_address:?}' and '{port:?}'");
|
||||
// Create a string representation of the listen address
|
||||
let address: String = listen_address
|
||||
.as_deref()
|
||||
.unwrap_or(&cfg.app_host)
|
||||
.to_string();
|
||||
// Create an integer representation of the port
|
||||
let port_value = port.unwrap_or(cfg.app_port.parse::<i32>().unwrap_or(0));
|
||||
let port: i32 = port_value;
|
||||
// Start the relay server with the port and listen address
|
||||
relay::server::start_ws(&port, &address).await;
|
||||
}
|
||||
// No command provided
|
||||
None => {}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,25 +1,70 @@
|
|||
use lazy_static::lazy_static;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Represents the configuration settings for the Caesar application.
|
||||
///
|
||||
/// This struct is used to store the configuration settings for the application,
|
||||
/// such as the environment, host, port, origin, and logging level.
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct CaesarConfig {
|
||||
/// The environment in which the application is running.
|
||||
///
|
||||
/// Possible values are "production", "staging", or "development".
|
||||
pub app_environment: String,
|
||||
|
||||
/// The host on which the application is running.
|
||||
///
|
||||
/// This is typically an IP address or a hostname.
|
||||
pub app_host: String,
|
||||
|
||||
/// The port on which the application is listening.
|
||||
///
|
||||
/// This is typically a string representation of a port number.
|
||||
pub app_port: String,
|
||||
|
||||
/// The origin of the application.
|
||||
///
|
||||
/// This is typically a URL that specifies the protocol, hostname, and port.
|
||||
pub app_origin: String,
|
||||
|
||||
/// The relay endpoint of the application.
|
||||
///
|
||||
/// This is typically a combination of a hostname and port.
|
||||
pub app_relay: String,
|
||||
|
||||
/// The logging level for the application.
|
||||
///
|
||||
/// This is typically a string representation of a logging level, such as "info",
|
||||
/// "debug", or "error".
|
||||
pub rust_log: String,
|
||||
}
|
||||
|
||||
|
||||
/// The default configuration values for the Caesar application.
|
||||
///
|
||||
/// These values are used when loading the configuration file fails.
|
||||
/// The default configuration is suitable for running the application in a production environment.
|
||||
impl Default for CaesarConfig {
|
||||
/// Returns a new `CaesarConfig` instance with default values.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A new `CaesarConfig` instance with the following default values:
|
||||
///
|
||||
/// - `app_environment`: "production"
|
||||
/// - `app_host`: "0.0.0.0"
|
||||
/// - `app_port`: "8000"
|
||||
/// - `app_origin`: "wss://caesar-transfer-iu.shuttleapp.rs"
|
||||
/// - `app_relay`: "0.0.0.0:8000"
|
||||
/// - `rust_log`: "info"
|
||||
fn default() -> Self {
|
||||
CaesarConfig {
|
||||
app_environment: "production".to_string(),
|
||||
app_host: "0.0.0.0".to_string(),
|
||||
app_port: "8000".to_string(),
|
||||
app_origin: "wss://caesar-transfer-iu.shuttleapp.rs".to_string(),
|
||||
app_relay: "0.0.0.0:8000".to_string(),
|
||||
rust_log: "info".to_string(),
|
||||
app_environment: "production".to_string(), // The environment in which the application is running.
|
||||
app_host: "0.0.0.0".to_string(), // The host on which the application is running.
|
||||
app_port: "8000".to_string(), // The port on which the application is listening.
|
||||
app_origin: "wss://caesar-transfer-iu.shuttleapp.rs".to_string(), // The origin of the application.
|
||||
app_relay: "0.0.0.0:8000".to_string(), // The relay endpoint of the application.
|
||||
rust_log: "info".to_string(), // The logging level for the application.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,15 +6,29 @@ use tracing_subscriber::filter::EnvFilter;
|
|||
mod cli;
|
||||
mod config;
|
||||
|
||||
/// Entry point of the application.
|
||||
///
|
||||
/// This function is called when the application is started. It initializes the environment,
|
||||
/// parses the command line arguments, and runs the application.
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
// Load environment variables from the `.env` file.
|
||||
dotenv().ok();
|
||||
|
||||
// Initialize the logging subscriber.
|
||||
// It configures the logging level based on the `RUST_LOG` environment variable.
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
// Parse the command line arguments.
|
||||
let args = Args::new();
|
||||
|
||||
// Run the application.
|
||||
// If an error occurs, log the error message.
|
||||
if let Err(e) = args.run().await {
|
||||
error!("{e}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue