arcana-base-server 0.6.0

Library to help create your own arcana activation server
Documentation
#![allow(clippy::pedantic)]
tonic::include_proto!("arcana.activation");

/// This trait allows to have generic response method for
/// the multiple response types, which are auto-generated.
/// It creates the response from a log line, created by methods
/// of ResponseHeader.
pub trait HeadedResponse {
    fn eof() -> Self;
    fn from_header(header: ResponseHeader) -> Self;
}

/// This trait allows to process requests equally and dispatch
/// them to correct handlers.
/// In the protocol, header contains the useful information for that.
pub trait HeadedRequest {
    fn header(&self) -> &Option<RequestHeader>;
}

impl ResponseHeader {
    pub(crate) fn eof() -> Self {
        ResponseHeader {
            msg: String::new(),
            response_type: ResponseType::Eof.into(),
        }
    }

    pub fn error(msg: String) -> Self {
        ResponseHeader {
            msg,
            response_type: ResponseType::Error.into(),
        }
    }

    pub fn warn(msg: String) -> Self {
        ResponseHeader {
            msg,
            response_type: ResponseType::Warn.into(),
        }
    }

    pub fn log(msg: String) -> Self {
        ResponseHeader {
            msg,
            response_type: ResponseType::Log.into(),
        }
    }
}

impl HeadedResponse for ApplyResponse {
    fn eof() -> Self {
        ApplyResponse {
            header: Some(ResponseHeader::eof()),
        }
    }

    fn from_header(header: ResponseHeader) -> Self {
        ApplyResponse {
            header: Some(header),
        }
    }
}

impl HeadedResponse for BuildResponse {
    fn eof() -> Self {
        BuildResponse {
            header: Some(ResponseHeader::eof()),
            new_store_path: None,
        }
    }

    fn from_header(header: ResponseHeader) -> Self {
        let new_store_path = if header.msg.starts_with("/nix/store") {
            Some(header.msg.clone())
        } else {
            None
        };

        BuildResponse {
            header: Some(header),
            new_store_path,
        }
    }
}

impl HeadedResponse for CopyClosureResponse {
    fn eof() -> Self {
        CopyClosureResponse {
            header: Some(ResponseHeader::eof()),
        }
    }

    fn from_header(header: ResponseHeader) -> Self {
        CopyClosureResponse {
            header: Some(header),
        }
    }
}

impl HeadedRequest for ApplyRequest {
    fn header(&self) -> &Option<RequestHeader> {
        &self.header
    }
}

impl HeadedRequest for BuildRequest {
    fn header(&self) -> &Option<RequestHeader> {
        &self.header
    }
}

impl HeadedRequest for CopyClosureRequest {
    fn header(&self) -> &Option<RequestHeader> {
        &self.header
    }
}