about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRen Kararou <[email protected]>2024-07-13 00:05:42 -0500
committerRen Kararou <[email protected]>2024-07-13 00:05:42 -0500
commit2355137d412f48379d3fc81d6b727a786e0b765d (patch)
treef9af0aaf00cda62e8109b7f808b881c67da76a67
parent80b2dd31531f24b070a83f2d56670249b58c6f4b (diff)
downloadk2spice-2355137d412f48379d3fc81d6b727a786e0b765d.tar.gz
k2spice-2355137d412f48379d3fc81d6b727a786e0b765d.tar.bz2
k2spice-2355137d412f48379d3fc81d6b727a786e0b765d.zip
add common lib, custom macros, courtesy of Skylar Bleed
-rw-r--r--Cargo.toml2
-rw-r--r--usr/src/lib/com/Cargo.toml12
-rw-r--r--usr/src/lib/com/src/lib.rs62
-rw-r--r--usr/src/mei/printf/src/printf.rs2
-rw-r--r--usr/src/mei/pwd/Cargo.toml1
-rw-r--r--usr/src/mei/pwd/src/pwd.rs18
6 files changed, 87 insertions, 10 deletions
diff --git a/Cargo.toml b/Cargo.toml
index f728548..6bb1f20 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
     "usr/src/mei/printf",
     "usr/src/mei/false",
     "usr/src/mei/hostname",
+	"usr/src/lib/com",
 ]
 default-members = [
     "usr/src/mei/true",
@@ -33,4 +34,3 @@ readme = "README.md"
 nix = "0.27"
 clap = { version = "4.4.8", features = ["derive"] }
 serde = { version = "1.0.192", features = ["derive"] }
-
diff --git a/usr/src/lib/com/Cargo.toml b/usr/src/lib/com/Cargo.toml
new file mode 100644
index 0000000..47b334d
--- /dev/null
+++ b/usr/src/lib/com/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "com"
+version.workspace = true
+edition.workspace = true
+license.workspace = true
+authors.workspace = true
+description.workspace = true
+documentation.workspace = true
+repository.workspace = true
+readme.workspace = true
+
+[dependencies]
diff --git a/usr/src/lib/com/src/lib.rs b/usr/src/lib/com/src/lib.rs
new file mode 100644
index 0000000..b0097c9
--- /dev/null
+++ b/usr/src/lib/com/src/lib.rs
@@ -0,0 +1,62 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2023 Skylar Alexandra Bleed. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/** like a prelude: use com::macros::*; */
+pub mod macros {
+    pub use crate::{ok_or, some_or, fatal};
+}
+
+/** print an error and abort */
+#[macro_export]
+macro_rules! fatal {
+    ($($t:tt)*) => {{
+        eprintln!("fatal error: {}", format!($($t)*));
+        std::process::exit(-1);
+    }};
+}
+
+/** expect an Ok value or abort */
+#[macro_export]
+macro_rules! ok_or {
+    ($x:expr, $($t:tt)*) => {{
+        match $x {
+            Ok(x) => x,
+            Err(e) => fatal!("{e}: {}", format!($($t)*)),
+        }
+    }};
+}
+
+/** expect a Some value or abort */
+#[macro_export]
+macro_rules! some_or {
+    ($x:expr, $($t:tt)*) => {{
+        match $x {
+            Some(x) => x,
+            None => fatal!($($t)*),
+        }
+    }};
+}
diff --git a/usr/src/mei/printf/src/printf.rs b/usr/src/mei/printf/src/printf.rs
index 4d30c64..4180112 100644
--- a/usr/src/mei/printf/src/printf.rs
+++ b/usr/src/mei/printf/src/printf.rs
@@ -123,7 +123,7 @@ fn fmt(fmtstr: String, args: Vec<String>) -> String {
                 i = xe;
             }
         } else {
-            formatted += &fmtstr[i..i+1];
+            formatted += &fmtstr[i..i + 1];
         }
         i += 1;
     }
diff --git a/usr/src/mei/pwd/Cargo.toml b/usr/src/mei/pwd/Cargo.toml
index 6671d1c..255fac0 100644
--- a/usr/src/mei/pwd/Cargo.toml
+++ b/usr/src/mei/pwd/Cargo.toml
@@ -16,3 +16,4 @@ path = "src/pwd.rs"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+com = { path = "../../lib/com" }
diff --git a/usr/src/mei/pwd/src/pwd.rs b/usr/src/mei/pwd/src/pwd.rs
index b0cc40d..06816a7 100644
--- a/usr/src/mei/pwd/src/pwd.rs
+++ b/usr/src/mei/pwd/src/pwd.rs
@@ -22,18 +22,20 @@
 
 /*
  * Copyright 2023 Ren Kararou  All rights reserved.
+ * Portions copyright Skylar Alexandra Bleed 2024. All rights reserved.
  * Use is subject to license terms.
  */
 
 use std::env::current_dir;
-use std::process::exit;
+use com::macros::*;
 
 fn main() {
-    if let Ok(buf) = current_dir() {
-        println!("{}", buf.to_str().expect("pwd: cannot determine current directoy!"));
-        exit(0);
-    } else {
-        eprintln!("pwd: cannot determine current directory!");
-        exit(2);
-    }
+    /* TODO: make expect accept different error codes? */
+
+    /* buf */
+    let b = ok_or!(current_dir(), "pwd: cannot determine current directory!");
+    /* path */
+    let p = some_or!(b.to_str(), "pwd: cannot get path from buf!");
+
+    println!("{p}");
 }