use crate::{
data::{Did, Jwt, Link, MemoIpld},
error::NoosphereError,
};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushBody {
pub sphere: Did,
pub local_base: Option<Link<MemoIpld>>,
pub local_tip: Link<MemoIpld>,
pub counterpart_tip: Option<Link<MemoIpld>>,
pub name_record: Option<Jwt>,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PushResponse {
Accepted {
new_tip: Link<MemoIpld>,
},
NoChange,
}
#[allow(missing_docs)]
#[derive(Serialize, Deserialize, Error, Debug)]
pub enum PushError {
#[error("Stream of blocks up to the gateway was interrupted")]
BrokenUpstream,
#[error("Stream of blocks down from the gateway was interrupted")]
BrokenDownstream,
#[error("First block in upstream was missing or unexpected type")]
UnexpectedBody,
#[error("Pushed history conflicts with canonical history")]
Conflict,
#[error("Missing some implied history")]
MissingHistory,
#[error("Replica is up to date")]
UpToDate,
#[error("Internal error: {0:?}")]
Internal(Option<String>),
}
impl From<&PushError> for StatusCode {
fn from(value: &PushError) -> Self {
match value {
PushError::BrokenUpstream => StatusCode::BAD_GATEWAY,
PushError::BrokenDownstream => StatusCode::BAD_GATEWAY,
PushError::UnexpectedBody => StatusCode::UNPROCESSABLE_ENTITY,
PushError::Conflict => StatusCode::CONFLICT,
PushError::MissingHistory => StatusCode::FAILED_DEPENDENCY,
PushError::UpToDate => StatusCode::NOT_MODIFIED,
PushError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl From<NoosphereError> for PushError {
fn from(error: NoosphereError) -> Self {
error.into()
}
}
impl From<anyhow::Error> for PushError {
fn from(value: anyhow::Error) -> Self {
PushError::Internal(Some(format!("{value}")))
}
}