rustybook 0.2.1

An ergonomic Facebook client in Rust
Documentation
#![doc = include_str!("../README.md")]

#[cfg(all(feature = "cache", not(feature = "messenger")))]
compile_error!("feature `cache` requires feature `messenger`");

mod builder;
pub mod error;
mod extractor;
mod session;

use std::sync::Arc;

use rustybook_http::client::Client;
#[cfg(feature = "messenger")]
use rustybook_messenger::client::MessengerClient;

use crate::builder::RustybookBuilder;
use crate::error::*;

#[cfg(feature = "messenger")]
pub use rustybook_messenger::client::models::{
    Message,
    Presence,
    Typing,
    User,
};
#[cfg(feature = "messenger")]
pub use rustybook_messenger::client::{
    context::Context,
    events::{
        ErrorEvent,
        EventHandler,
    },
};

pub(crate) struct Inner {
    http: Client,
    #[cfg(feature = "messenger")]
    messenger: MessengerClient,
    #[cfg(feature = "messenger")]
    handler: Option<Arc<dyn EventHandler>>,
}

/// Rustybook is a client for interacting with Facebook services.
#[derive(Clone)]
pub struct Rustybook {
    inner: Arc<Inner>,
}

impl Rustybook {
    /// Creates a new Rustybook client with default settings.
    pub fn new() -> Result<Rustybook, Error> {
        RustybookBuilder::new().build()
    }

    /// Returns a builder for configuring the client.
    pub fn builder() -> RustybookBuilder {
        RustybookBuilder::new()
    }

    /// Starts the messenger client
    #[cfg(feature = "messenger")]
    pub async fn start(&self) -> Result<(), Error> {
        if let Some(handler) = self.inner.handler.as_ref() {
            self.inner
                .messenger
                .start_with_handler(handler.clone())
                .await?;
        } else {
            self.inner.messenger.start().await?;
        }
        Ok(())
    }
}