summaryrefslogtreecommitdiff
path: root/src/configuration.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-08-25 12:36:37 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-08-25 12:36:37 +0200
commite5de70f2c1dcac767bd34a6b90ac1797a7acb433 (patch)
tree25a8c8f339fb5b496b460391ee06a9effeb64855 /src/configuration.rs
parent6b909d95ec07848136a6f337db28318c1cd46c60 (diff)
parentd7bc21d19e168f3a99f54e5ba4866ed49f1187f1 (diff)
Merge branch 'rust'
Diffstat (limited to 'src/configuration.rs')
-rw-r--r--src/configuration.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/configuration.rs b/src/configuration.rs
new file mode 100644
index 0000000..5995ffc
--- /dev/null
+++ b/src/configuration.rs
@@ -0,0 +1,63 @@
+use lexopt::{Parser, prelude::*};
+
+const DEFAULT_ADDRESS: &str = "127.0.0.1:6666";
+const DEFAULT_FREQUENCY: u64 = 150;
+const DEFAULT_MODULATION: u8 = 5;
+
+#[derive(Clone)]
+pub struct Configuration {
+ pub address: String,
+ pub frequency: u64,
+ pub modulation: u8,
+}
+
+impl Configuration {
+ pub fn new() -> Self {
+ let mut address = DEFAULT_ADDRESS.to_string();
+ let mut frequency = DEFAULT_FREQUENCY;
+ let mut modulation = DEFAULT_MODULATION;
+
+ let mut parser = Parser::from_env();
+
+ while let Ok(Some(argument)) = parser.next() {
+ match argument {
+ Short('l') | Long("listen-address") => {
+ if let Ok(value) = parser.value().and_then(|v| v.parse()) {
+ address = value;
+ } else {
+ eprintln!("Warning: Invalid listen address ignored.");
+ }
+ }
+ Short('f') | Long("frequency") => {
+ if let Ok(value) = parser.value().and_then(|v| v.parse()) {
+ frequency = value;
+ } else {
+ eprintln!("Warning: Invalid frequency ignored.");
+ }
+ }
+ Short('m') | Long("modulation") => {
+ if let Ok(value) = parser.value().and_then(|v| v.parse()) {
+ modulation = value;
+ } else {
+ eprintln!("Warning: Invalid modulation ignored.");
+ }
+ }
+ Long("help") => {
+ println!(
+ "Usage: tomato-sauce [-l|--listen-address=LISTEN_ADDRESS] [-f|--frequency=NUMBER] [-m|--modulation=NUMBER]"
+ );
+ std::process::exit(0);
+ }
+ _ => {
+ eprintln!("Warning: Unknown argument ignored");
+ }
+ }
+ }
+
+ Configuration {
+ address,
+ frequency,
+ modulation,
+ }
+ }
+}