From e1bf49c75af4ff75aaae9761cc5a8196c4d6e198 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 22 Jun 2024 22:30:30 -0600 Subject: [PATCH] getopt.rs(3): adds comments & documentation --- src/getopt.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/getopt.rs b/src/getopt.rs index dc88229..ee8cafa 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -40,9 +40,9 @@ pub enum OptError { #[derive(Clone, Debug)] pub struct Opt { - arg: Option, - ind: *mut i32, - opt: Result, + arg: Option, /* option argument */ + ind: *mut i32, /* option index */ + opt: Result, /* option option */ } impl Opt { @@ -53,6 +53,7 @@ impl Opt { default.to_string() } + /* makes matching the output of this method more bearable */ pub fn opt(&self) -> Result<&str, OptError> { self.opt.as_ref().map(|o| o.as_str()).map_err(OptError::clone) } @@ -68,6 +69,7 @@ impl Opt { * options have already been processed. */ pub fn ind(&self) -> usize { unsafe { *self.ind as usize } } + /* this is patently terrible and is only happening because I’m stubborn */ pub fn set_ind(&self, ind: i32) { unsafe { *self.ind = ind; } } } @@ -84,11 +86,11 @@ impl GetOpt for Vec { .map(|x| CString::new(x).unwrap().into_raw()) .collect(); + /* god knows what this does */ let boxed = Box::into_raw(c_strings.into_boxed_slice()); let argv = boxed as *const *mut i8; - /* these operations must be separated out into separate operations so - * the CStrings can live long enough */ + /* operations are separated out so that everything lives long enough */ let opts = CString::new(optstring).unwrap().into_raw(); let len = self.len() as c_int; @@ -110,21 +112,17 @@ impl GetOpt for Vec { * * Otherwise, getopt() shall return -1 when all command line * options are parsed. */ - 58 => { /* numerical ASCII value for ':' */ + 58 => { /* ASCII ':' */ Some(Opt { - /* opt argument */ arg: None, - /* opt index */ ind: std::ptr::addr_of_mut!(optind), /* error containing option */ opt: Err(OptError::MissingArg(optopt.to_string())), }) }, - 63 => { /* numerical ASCII value for '?' */ + 63 => { /* ASCII '?' */ Some(Opt { - /* opt argument */ arg: None, - /* opt index */ ind: std::ptr::addr_of_mut!(optind), /* error containing option */ opt: Err(OptError::UnknownOpt(optopt.to_string())), @@ -155,21 +153,25 @@ impl GetOpt for Vec { } Some(Opt { - arg, /* opt argument */ - ind: std::ptr::addr_of_mut!(optind), /* opt index */ - /* option itself */ + arg, + ind: std::ptr::addr_of_mut!(optind), + /* I didn’t need to cast this before; I rewrote the + * pointer logic and now I do + * + * I don’t know why this is */ opt: Ok((opt as u8 as char).to_string()), }) }, }; - /* delloc argv */ + /* delloc argv (something online said I should do this) */ let _ = Box::from_raw(boxed); return ret; } } } +/* tests (good) */ #[cfg(test)] mod tests { use GetOpt;