p2p_chat/app/
mod.rs

1//! This module contains the primary logic for the application.
2//!
3//! It is responsible for parsing command-line arguments, setting up the
4//! application environment, and launching either a client or a mailbox node.
5pub mod args;
6mod client;
7mod mailbox;
8mod setup;
9
10pub use args::AppArgs;
11
12use anyhow::Result;
13
14/// Launches the application.
15///
16/// This function parses command-line arguments and then calls `launch_with_args`.
17///
18/// # Errors
19///
20/// This function will return an error if the application fails to launch.
21pub async fn launch() -> Result<()> {
22    launch_with_args(AppArgs::from_cli()).await
23}
24
25/// Launches the application with the given arguments.
26///
27/// This function prepares the application environment and then runs either a
28/// client or a mailbox node, depending on the provided arguments.
29///
30/// # Arguments
31///
32/// * `args` - The command-line arguments to use.
33///
34/// # Errors
35///
36/// This function will return an error if the application fails to launch.
37pub async fn launch_with_args(args: AppArgs) -> Result<()> {
38    let setup::PreparedApp {
39        args,
40        port,
41        web_port,
42        identity,
43        db,
44        encryption,
45    } = setup::prepare(args)?;
46
47    if args.mailbox {
48        mailbox::run(identity, db, encryption, port).await
49    } else {
50        client::run(identity, db, encryption, port, web_port).await
51    }
52}