diff --git a/src/error.rs b/src/error.rs index 9ed05db8..d0f35dfd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,5 @@ +//! Provides an `Error` for use in this crate. + use std::io; use std::net::TcpStream; @@ -10,6 +12,7 @@ use xml::writer::Error as EmitterError; use minidom::Error as MinidomError; +/// An error which wraps a bunch of errors from different crates and the stdlib. #[derive(Debug)] pub enum Error { XmlError(XmlError), diff --git a/src/event.rs b/src/event.rs index 7098c4e3..c580a2b3 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,25 +1,56 @@ +//! Provides an abstract event type which can be downcasted into a more specific one. +//! +//! # Examples +//! +//! ``` +//! use xmpp::event::{Event, AbstractEvent}; +//! +//! #[derive(Debug, PartialEq, Eq)] +//! struct EventA; +//! +//! impl Event for EventA {} +//! +//! #[derive(Debug, PartialEq, Eq)] +//! struct EventB; +//! +//! impl Event for EventB {} +//! +//! let event_a = AbstractEvent::new(EventA); +//! +//! assert_eq!(event_a.is::(), true); +//! assert_eq!(event_a.is::(), false); +//! +//! assert_eq!(event_a.downcast::(), Some(&EventA)); +//! assert_eq!(event_a.downcast::(), None); +//! ``` + use std::fmt::Debug; use std::any::Any; +/// An abstract event. pub struct AbstractEvent { inner: Box, } impl AbstractEvent { + /// Creates an abstract event from a concrete event. pub fn new(event: E) -> AbstractEvent { AbstractEvent { inner: Box::new(event), } } + /// Downcasts this abstract event into a concrete event. pub fn downcast(&self) -> Option<&E> { self.inner.downcast_ref::() } + /// Checks whether this abstract event is a specific concrete event. pub fn is(&self) -> bool { self.inner.is::() } } +/// A marker trait which all events must implement. pub trait Event: Any + Debug {} diff --git a/src/jid.rs b/src/jid.rs index bcbeecdb..b277aa3e 100644 --- a/src/jid.rs +++ b/src/jid.rs @@ -1,3 +1,5 @@ +//! Provides a type for Jabber IDs. + use std::fmt; use std::convert::Into; diff --git a/src/plugin.rs b/src/plugin.rs index 3ae94ceb..21c01148 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,3 +1,5 @@ +//! Provides the plugin infrastructure. + use event::{Event, AbstractEvent}; use std::any::Any;