Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: rename module to have a clearer name
Lars Wirzenius committed 1 year ago
commit 5017e2cc77888775e1922d25ebeb4d960663e9e3
parent 72f09aa9f64d456eb88da9f0b89a0bccc5822fc7
4 files changed +118 -118
modified src/event.rs
@@ -42,8 +42,8 @@ use std::{
};

use crate::{
-
    event_source::{NodeEventError, NodeEventSource},
    logger,
+
    node_event_source::{NodeEventError, NodeEventSource},
};

/// Source of events from the local Radicle node.
deleted src/event_source.rs
@@ -1,116 +0,0 @@
-
//! Read node events from the local node.
-

-
use std::{path::PathBuf, time};
-

-
use radicle::{
-
    node::{Event, Handle},
-
    Profile,
-
};
-

-
use crate::logger;
-

-
/// Source of events from the local Radicle node.
-
///
-
/// The events are filtered. Only events allowed by at least one
-
/// filter are returned. See [`NodeEventSource::allow`] and
-
/// [`EventFilter`].
-
pub struct NodeEventSource {
-
    events: Box<dyn Iterator<Item = Result<Event, radicle::node::Error>>>,
-
}
-

-
impl NodeEventSource {
-
    /// Create a new source of node events, for a given Radicle
-
    /// profile.
-
    pub fn new(profile: &Profile) -> Result<Self, NodeEventError> {
-
        let socket = profile.socket();
-
        if !socket.exists() {
-
            return Err(NodeEventError::NoControlSocket(socket));
-
        }
-
        let node = radicle::Node::new(socket.clone());
-
        match node.subscribe(time::Duration::MAX) {
-
            Ok(events) => Ok(Self {
-
                events: Box::new(events.into_iter()),
-
            }),
-
            Err(err) => {
-
                logger::error("failed to subscribe to node events", &err);
-
                Err(NodeEventError::CannotSubscribe(socket.clone(), err))
-
            }
-
        }
-
    }
-

-
    /// Get the next node event from an event source, without
-
    /// filtering. This will block until there is an event, or until
-
    /// there will be no more events from this source, or there's an
-
    /// error.
-
    pub fn node_event(&mut self) -> Result<Option<Event>, NodeEventError> {
-
        if let Some(event) = self.events.next() {
-
            match event {
-
                Ok(event) => Ok(Some(event)),
-
                Err(radicle::node::Error::Io(err))
-
                    if err.kind() == std::io::ErrorKind::ConnectionReset =>
-
                {
-
                    logger::event_disconnected();
-
                    Err(NodeEventError::BrokenConnection)
-
                }
-
                Err(err) => {
-
                    logger::error("error reading event from node", &err);
-
                    Err(NodeEventError::Node(err))
-
                }
-
            }
-
        } else {
-
            Ok(None)
-
        }
-
    }
-
}
-

-
/// Possible errors from accessing the local Radicle node.
-
#[derive(Debug, thiserror::Error)]
-
pub enum NodeEventError {
-
    /// Regex compilation error.
-
    #[error("programming error in regular expression {0:?}")]
-
    Regex(&'static str, regex::Error),
-

-
    /// Node control socket does not exist.
-
    #[error("node control socket does not exist: {0}")]
-
    NoControlSocket(PathBuf),
-

-
    /// Can't subscribe to node events.
-
    #[error("failed to subscribe to node events on socket {0}")]
-
    CannotSubscribe(PathBuf, #[source] radicle::node::Error),
-

-
    /// Some error from getting an event from the node.
-
    #[error(transparent)]
-
    Node(#[from] radicle::node::Error),
-

-
    /// Connection to the node control socket broke.
-
    #[error("connection to the node control socket broke")]
-
    BrokenConnection,
-

-
    /// Some error from parsing a repository id.
-
    #[error(transparent)]
-
    Id(#[from] radicle::identity::IdError),
-

-
    /// Some error doing input/output.
-
    #[error(transparent)]
-
    Io(#[from] std::io::Error),
-

-
    /// An error reading a filter file.
-
    #[error("failed to read filter file: {0}")]
-
    ReadFilterFile(PathBuf, #[source] std::io::Error),
-

-
    /// An error parsing JSON as filters, when read from a file.
-
    #[error("failed to parser filters file: {0}")]
-
    FiltersJsonFile(PathBuf, #[source] serde_json::Error),
-

-
    /// An error parsing YAML as filters, when read from a file.
-
    #[error("failed to parser filters file: {0}")]
-
    FiltersYamlFile(PathBuf, #[source] serde_yml::Error),
-

-
    /// An error parsing JSON as filters, from an in-memory string.
-
    #[error("failed to parser filters as JSON")]
-
    FiltersJsonString(#[source] serde_json::Error),
-

-
    /// An error parsing a Git object id as string into an Oid.
-
    #[error("failed to parse string as a Git object id: {0:?}")]
-
    ParseOid(String, #[source] radicle::git::raw::Error),
-
}
modified src/lib.rs
@@ -10,9 +10,9 @@ pub mod broker;
pub mod config;
pub mod db;
pub mod event;
-
pub mod event_source;
pub mod logger;
pub mod msg;
+
pub mod node_event_source;
pub mod notif;
pub mod pages;
pub mod queueadd;
added src/node_event_source.rs
@@ -0,0 +1,116 @@
+
//! Read node events from the local node.
+

+
use std::{path::PathBuf, time};
+

+
use radicle::{
+
    node::{Event, Handle},
+
    Profile,
+
};
+

+
use crate::logger;
+

+
/// Source of events from the local Radicle node.
+
///
+
/// The events are filtered. Only events allowed by at least one
+
/// filter are returned. See [`NodeEventSource::allow`] and
+
/// [`EventFilter`].
+
pub struct NodeEventSource {
+
    events: Box<dyn Iterator<Item = Result<Event, radicle::node::Error>>>,
+
}
+

+
impl NodeEventSource {
+
    /// Create a new source of node events, for a given Radicle
+
    /// profile.
+
    pub fn new(profile: &Profile) -> Result<Self, NodeEventError> {
+
        let socket = profile.socket();
+
        if !socket.exists() {
+
            return Err(NodeEventError::NoControlSocket(socket));
+
        }
+
        let node = radicle::Node::new(socket.clone());
+
        match node.subscribe(time::Duration::MAX) {
+
            Ok(events) => Ok(Self {
+
                events: Box::new(events.into_iter()),
+
            }),
+
            Err(err) => {
+
                logger::error("failed to subscribe to node events", &err);
+
                Err(NodeEventError::CannotSubscribe(socket.clone(), err))
+
            }
+
        }
+
    }
+

+
    /// Get the next node event from an event source, without
+
    /// filtering. This will block until there is an event, or until
+
    /// there will be no more events from this source, or there's an
+
    /// error.
+
    pub fn node_event(&mut self) -> Result<Option<Event>, NodeEventError> {
+
        if let Some(event) = self.events.next() {
+
            match event {
+
                Ok(event) => Ok(Some(event)),
+
                Err(radicle::node::Error::Io(err))
+
                    if err.kind() == std::io::ErrorKind::ConnectionReset =>
+
                {
+
                    logger::event_disconnected();
+
                    Err(NodeEventError::BrokenConnection)
+
                }
+
                Err(err) => {
+
                    logger::error("error reading event from node", &err);
+
                    Err(NodeEventError::Node(err))
+
                }
+
            }
+
        } else {
+
            Ok(None)
+
        }
+
    }
+
}
+

+
/// Possible errors from accessing the local Radicle node.
+
#[derive(Debug, thiserror::Error)]
+
pub enum NodeEventError {
+
    /// Regex compilation error.
+
    #[error("programming error in regular expression {0:?}")]
+
    Regex(&'static str, regex::Error),
+

+
    /// Node control socket does not exist.
+
    #[error("node control socket does not exist: {0}")]
+
    NoControlSocket(PathBuf),
+

+
    /// Can't subscribe to node events.
+
    #[error("failed to subscribe to node events on socket {0}")]
+
    CannotSubscribe(PathBuf, #[source] radicle::node::Error),
+

+
    /// Some error from getting an event from the node.
+
    #[error(transparent)]
+
    Node(#[from] radicle::node::Error),
+

+
    /// Connection to the node control socket broke.
+
    #[error("connection to the node control socket broke")]
+
    BrokenConnection,
+

+
    /// Some error from parsing a repository id.
+
    #[error(transparent)]
+
    Id(#[from] radicle::identity::IdError),
+

+
    /// Some error doing input/output.
+
    #[error(transparent)]
+
    Io(#[from] std::io::Error),
+

+
    /// An error reading a filter file.
+
    #[error("failed to read filter file: {0}")]
+
    ReadFilterFile(PathBuf, #[source] std::io::Error),
+

+
    /// An error parsing JSON as filters, when read from a file.
+
    #[error("failed to parser filters file: {0}")]
+
    FiltersJsonFile(PathBuf, #[source] serde_json::Error),
+

+
    /// An error parsing YAML as filters, when read from a file.
+
    #[error("failed to parser filters file: {0}")]
+
    FiltersYamlFile(PathBuf, #[source] serde_yml::Error),
+

+
    /// An error parsing JSON as filters, from an in-memory string.
+
    #[error("failed to parser filters as JSON")]
+
    FiltersJsonString(#[source] serde_json::Error),
+

+
    /// An error parsing a Git object id as string into an Oid.
+
    #[error("failed to parse string as a Git object id: {0:?}")]
+
    ParseOid(String, #[source] radicle::git::raw::Error),
+
}