surrealdb/api/engine/remote/ws/
mod.rs#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod native;
#[cfg(target_arch = "wasm32")]
pub(crate) mod wasm;
use crate::api;
use crate::api::conn::DbResponse;
use crate::api::conn::Method;
use crate::api::err::Error;
use crate::api::Connect;
use crate::api::Result;
use crate::api::Surreal;
use crate::dbs::Notification;
use crate::dbs::Status;
use crate::opt::IntoEndpoint;
use crate::sql::Strand;
use crate::sql::Value;
use serde::Deserialize;
use std::marker::PhantomData;
use std::time::Duration;
pub(crate) const PATH: &str = "rpc";
const PING_INTERVAL: Duration = Duration::from_secs(5);
const PING_METHOD: &str = "ping";
#[derive(Debug)]
pub struct Ws;
#[derive(Debug)]
pub struct Wss;
#[derive(Debug, Clone)]
pub struct Client {
pub(crate) id: i64,
method: Method,
}
impl Surreal<Client> {
pub fn connect<P>(
&self,
address: impl IntoEndpoint<P, Client = Client>,
) -> Connect<Client, ()> {
Connect {
router: self.router.clone(),
address: address.into_endpoint(),
capacity: 0,
client: PhantomData,
response_type: PhantomData,
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct Failure {
pub(crate) code: i64,
pub(crate) message: String,
}
#[derive(Debug, Deserialize)]
pub(crate) enum Data {
Other(Value),
Query(Vec<QueryMethodResponse>),
Live(Notification),
}
type ServerResult = std::result::Result<Data, Failure>;
impl From<Failure> for Error {
fn from(failure: Failure) -> Self {
match failure.code {
-32600 => Self::InvalidRequest(failure.message),
-32602 => Self::InvalidParams(failure.message),
-32603 => Self::InternalError(failure.message),
-32700 => Self::ParseError(failure.message),
_ => Self::Query(failure.message),
}
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct QueryMethodResponse {
#[allow(dead_code)]
time: String,
status: Status,
result: Value,
}
impl DbResponse {
fn from(result: ServerResult) -> Result<Self> {
match result.map_err(Error::from)? {
Data::Other(value) => Ok(DbResponse::Other(value)),
Data::Query(results) => Ok(DbResponse::Query(api::Response(
results
.into_iter()
.map(|response| match response.status {
Status::Ok => Ok(response.result),
Status::Err => match response.result {
Value::Strand(Strand(message)) => Err(Error::Query(message).into()),
message => Err(Error::Query(message.to_string()).into()),
},
})
.enumerate()
.collect(),
))),
Data::Live(..) => unreachable!(),
}
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct Response {
id: Option<Value>,
pub(crate) result: ServerResult,
}