Skip to content

Commit

Permalink
Merge branch 'master' into libdonet-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrdz committed Feb 22, 2024
2 parents 0519543 + 28a71b8 commit a40c1c4
Show file tree
Hide file tree
Showing 25 changed files with 1,468 additions and 221 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@ The Donet repository houses two different Rust projects:
Please read the [introduction to Donet](./docs/01-Introduction.md) for an overview of the project
and how the engine works.

If you have any questions, feel free to join [our community Discord server](https://discord.gg/T6jGjEutfy).
Before starting your own contribution to Donet, please read over the [Contributing Guidelines](./CONTRIBUTING.md). If you are a first
time contributor to the project, please add your name and Git email address to the [CONTRIBUTORS.md](./CONTRIBUTORS.md) markdown file.
You may also use your GitHub username as your name. If your contribution includes modifications to source code, please add your
name and Git email in the [Cargo.toml](./Cargo.toml) file as an author of this project.

To build Donet, run the following under the project directory:
```sh
cargo build --release
```

If you are working on a contribution to either the Donet daemon or libdonet, please run code linting and unit testing before pushing:
```sh
cargo clippy
cargo fmt --all -- --check
cargo test
```
These checks should go over all source files in both `donet/` and `libdonet/` source directories.

If you have any further questions, feel free to join [our community Discord server](https://discord.gg/T6jGjEutfy).

## Documentation
Currently there is not much documentation on Donet, as libdonet is still under development.
Expand Down
96 changes: 58 additions & 38 deletions donet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ pub mod network;
pub mod service_factory;
pub mod utils;

#[derive(Clone, Copy)]
enum FlagArguments {
DCFilePath,
}
static BIN_NAME: &str = "donetd";
static VERSION_STRING: &str = "0.1.0";
static DEFAULT_TOML: &str = "daemon.toml";
static GIT_SHA1: &str = env!("GIT_SHA1");

fn main() -> std::io::Result<()> {
use config::*;
use libdonet::dcfile::DCFileInterface;
Expand All @@ -52,70 +61,81 @@ fn main() -> std::io::Result<()> {
use tokio::runtime::{Builder, Runtime};
use tokio::task::JoinHandle;

static VERSION_STRING: &str = "0.1.0";
static GIT_SHA1: &str = env!("GIT_SHA1");
let mut config_file: &str = "daemon.toml"; // default
let args: Vec<String> = std::env::args().collect();

let mut config_file: &str = DEFAULT_TOML;
let mut want_dc_check: bool = false;
let mut dc_check_file: Option<&String> = None;
let mut consumed_next_arg: bool = false;
let mut dc_check_files: Vec<String> = vec![];
let mut expecting_flag_argument: Option<FlagArguments> = None;

if args.len() > 1 {
for item in args.iter().enumerate() {
let (index, argument): (usize, &String) = item;
if (index == 0) || consumed_next_arg {
consumed_next_arg = false;
continue;
if index == 0 {
continue; // skip invoked binary name
}
if argument.starts_with('-') {
if argument == "-h" || argument == "--help" {
print_help_page(config_file);
print_help_page();
return Ok(());
} else if argument == "-v" || argument == "--version" {
print_version(VERSION_STRING, GIT_SHA1);
return Ok(());
} else if argument == "-c" || argument == "--check-dc" {
// Consume extra argument, catch error if out of arguments.
if let Some(dc_filename) = args.get(index + 1) {
consumed_next_arg = true;
want_dc_check = true;
dc_check_file = Some(dc_filename);
} else {
println!("donet: {}: Missing filename argument.\n", argument);
print_help_page(config_file);
return Ok(());
}
want_dc_check = true;
expecting_flag_argument = Some(FlagArguments::DCFilePath);
continue;
} else {
println!("donet: {}: Invalid argument.\n", argument);
print_help_page(config_file);
println!("{}: {}: Invalid flag.\n", BIN_NAME, argument);
print_help_page();
return Ok(());
}
} else if let Some(expect_flag_arg) = expecting_flag_argument {
match expect_flag_arg {
FlagArguments::DCFilePath => {
dc_check_files.push(argument.to_owned());

// Look ahead to see if we should expect more args.
if let Some(lookahead) = args.get(index + 1) {
if !lookahead.ends_with(".dc") {
expecting_flag_argument = None;
}
continue;
}
expecting_flag_argument = None;
}
}
} else if index == (args.len() - 1) {
// last argument given & doesn't match any of the above,
// last argument given & we're not expecting more arguments,
// so it must be the configuration file path given.
config_file = argument.as_str();
break;
} else {
println!("{}: {}: Invalid argument.\n", BIN_NAME, argument);
print_help_page();
return Ok(());
}
}
if expecting_flag_argument.is_some() {
println!("{}: Expected more arguments.\n", BIN_NAME);
print_help_page();
return Ok(());
}
}
logger::initialize_logger()?;

if want_dc_check {
if let Some(dc_file) = dc_check_file {
let dc_read: DCReadResult = read_dc_files(vec![dc_file.to_owned()]);
info!("libdonet: DC read of {:?}", dc_check_files);
let dc_read: DCReadResult = read_dc_files(dc_check_files.to_owned());

if let Ok(mut dc_file) = dc_read {
info!("No issues found. DC File Hash: {}", dc_file.get_pretty_hash());
return Ok(());
}
error!("Failed to parse DC file: {:?}", dc_read.unwrap_err());
return Err(Error::new(ErrorKind::InvalidInput, "Failed to parse DC file."));
} else {
error!("No DC filename provided. Cannot do DC read.");
return Err(Error::new(ErrorKind::InvalidInput, "No DC file given."));
if let Ok(mut dc_file) = dc_read {
let h: u32 = dc_file.get_hash();
let sh: i32 = h as i32;
let ph: String = dc_file.get_pretty_hash();
info!("No issues found. File hash is {} (signed {}, hex {})", h, sh, ph);
return Ok(());
}
error!("Failed to parse DC file: {:?}", dc_read.unwrap_err());
return Err(Error::new(ErrorKind::InvalidInput, "Failed to parse DC file."));
}

// Read the daemon configuration file
Expand Down Expand Up @@ -214,18 +234,18 @@ fn main() -> std::io::Result<()> {
tokio_runtime.block_on(daemon_main)
}

fn print_help_page(config_path: &str) {
fn print_help_page() {
println!(
"Usage: donet [options] ... [CONFIG_FILE]\n\
"Usage: {} [options] ... [CONFIG_FILE]\n\
\n\
Donet - Distributed Object Network Engine.\n\
This binary will look for a configuration file (.toml)\n\
in the current working directory as \"{}\".\n\
\n\
-h, --help Print the help page.\n\
-v, --version Print Donet binary build version & info.\n\
-c, --check-dc Run the DC parser on the given DC file.\n",
config_path
-c, --check-dc Run the libdonet DC parser on the given DC file.\n",
BIN_NAME, DEFAULT_TOML
);
}

Expand Down
1 change: 1 addition & 0 deletions libdonet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors.workspace = true
homepage.workspace = true
repository.workspace = true
readme = "README.md"
documentation = "https://libdonet.rs"

[lib]
name = "libdonet"
Expand Down
2 changes: 2 additions & 0 deletions libdonet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
# libdonet

The libdonet Rust library provides the necessary utilities and definitions for using the Donet networking protocol. These utilities include a lexer, parser, and high-level representation of the parsed DC file, as well as creating datagrams, iterating through datagrams, and the definition of every message type in the Donet protocol.

The Rust crate documentation is available at [libdonet.rs](https://libdonet.rs).
Loading

0 comments on commit a40c1c4

Please sign in to comment.