summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-04-18 16:57:59 +0200
committerYorhel <git@yorhel.nl>2019-04-18 16:57:59 +0200
commit56c77e94185ed35bdbc474a0377eba0ac8117ba9 (patch)
tree6f191ba150dd4628fe7ab07b7b0c05fcf23d617c
parentcb642ff75eb1fdfa5ec0d944ef4a148eed54f761 (diff)
Add EventList builder
-rw-r--r--examples/cli.rs2
-rw-r--r--src/lib.rs2
-rw-r--r--src/sock.rs55
3 files changed, 46 insertions, 13 deletions
diff --git a/examples/cli.rs b/examples/cli.rs
index 2995bac..81820ad 100644
--- a/examples/cli.rs
+++ b/examples/cli.rs
@@ -15,7 +15,7 @@ fn main() {
}
}
});
- s.setevents(&["NOTICE", "INFO", "WARN", "ERR"]).unwrap();
+ s.setevents( torctl::EventList::default().notice() ).unwrap();
s.setconf(&[("SocksPort", Some("10245"))]).unwrap();
dbg!(s.getconf(&["ContactInfo", "DataDirectory", "HiddenServiceOptions"]).unwrap());
diff --git a/src/lib.rs b/src/lib.rs
index 11b2125..2f61fb4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,5 +3,5 @@ mod process;
mod sock;
pub use err::Error;
-pub use sock::{Sock,Auth};
+pub use sock::{Sock,Auth,Event,EventList};
pub use process::{genpass,hashpass,spawn};
diff --git a/src/sock.rs b/src/sock.rs
index 1724509..c4c8e8f 100644
--- a/src/sock.rs
+++ b/src/sock.rs
@@ -123,6 +123,7 @@ pub enum Auth {
}
+/// An asynchronous event received from Tor. These are returned by `read_event()`.
#[derive(Debug)]
pub enum Event {
// Log messages
@@ -134,6 +135,47 @@ pub enum Event {
}
+/// A Builder type for the argument to `setevents()`. Use `EventList::default()` to initialize a
+/// default (empty) event list.
+///
+/// ```
+/// # use torctl::EventList;
+/// let log_warn_and_error = EventList::default().warn();
+/// ```
+#[derive(Debug,Default,Clone,Copy)]
+pub struct EventList {
+ debug: bool,
+ info: bool,
+ notice: bool,
+ warn: bool,
+ err: bool,
+}
+
+impl EventList {
+ /// Enable all log events.
+ pub fn debug (mut self) -> Self { self.debug = true; self.info = true; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *INFO* and higher.
+ pub fn info (mut self) -> Self { self.debug = false; self.info = true; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *NOTICE* and higher.
+ pub fn notice(mut self) -> Self { self.debug = false; self.info = false; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *WARN* and higher.
+ pub fn warn (mut self) -> Self { self.debug = false; self.info = false; self.notice = false; self.warn = true; self.err = true; self }
+ /// Only enable logging for log level *ERR*.
+ pub fn err (mut self) -> Self { self.debug = false; self.info = false; self.notice = false; self.warn = false; self.err = true; self }
+
+ fn cmd(&self) -> String {
+ let mut s = "SETEVENTS".to_string();
+ if self.debug { s.push_str(" DEBUG" ) }
+ if self.info { s.push_str(" INFO" ) }
+ if self.notice { s.push_str(" NOTICE") }
+ if self.warn { s.push_str(" WARN" ) }
+ if self.err { s.push_str(" ERR" ) }
+ s.push_str("\r\n");
+ s
+ }
+}
+
+
// A double-quoted string where only \ and " are escaped.
// Encoding: QuotedString("hello").to_string()
@@ -459,17 +501,8 @@ impl Sock {
Ok(res)
}
- // TODO: Create an enum for supported event types, rather than this string thing. We don't
- // support reading all types of events anyway.
- pub fn setevents<'a,T: IntoIterator<Item = &'a &'a str>>(&self, events: T) -> Result<()> {
- let mut msg = "SETEVENTS".to_string();
- for e in events {
- is_keyword(e)?;
- msg.push(' ');
- msg.push_str(e);
- }
- msg.push_str("\r\n");
- self.cmd(msg).map(|_|())
+ pub fn setevents(&self, events: EventList) -> Result<()> {
+ self.cmd(events.cmd()).map(|_|())
}
/// Read an event from the socket. This method blocks until an event has been received.