use alloc::borrow::Cow;
use alloc::string::String;
use serde::{Deserialize, Serialize};
pub use blob::{
BlobHash, BlobId, BlobLinkRequest, BlobMoveRequest, BlobPath, BlobResolveRequest, BlobResponse,
BlobStoreRequest, BlobStoreResponse, BlobUnlinkRequest, ChunkRange, PathResolveRequest,
PathsListRequest, PathsListResponse, ResolveResponse,
};
pub use kv::{DeleteRequest, GetRequest, GetResponse, PrefixRequest, PrefixResponse, PutRequest};
pub use sem::{SelectRequest, SelectResponse, UpdateRequest};
pub use tb::{
Cursor, TbAppendRequest, TbCountRequest, TbCountResponse, TbDeleteRequest, TbGetRequest,
TbGetResponse, TbInsertRequest, TbInsertResponse, TbListRequest, TbListResponse, TbOrderBy,
};
pub use ts::{
FieldValue, FindRequest, FindResponse, Measurement, PublishRequest, Sample, TsOrderBy,
};
mod blob;
mod kv;
mod sem;
mod tb;
mod ts;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Scope {
namespace: Namespace,
database: Option<Cow<'static, str>>,
schema: Option<Cow<'static, str>>,
}
impl Default for Scope {
fn default() -> Self {
Self::private()
}
}
impl Scope {
pub const fn private() -> Self {
Self::private_in(None)
}
pub const fn public(namespace: &'static str) -> Self {
Self::public_in(namespace, None, None)
}
pub const fn private_in(schema: Option<&'static str>) -> Self {
Self {
namespace: Namespace::Private,
database: None,
schema: borrowed(schema),
}
}
pub const fn public_in(
namespace: &'static str,
database: Option<&'static str>,
schema: Option<&'static str>,
) -> Self {
Self {
namespace: Namespace::Public(Cow::Borrowed(namespace)),
database: borrowed(database),
schema: borrowed(schema),
}
}
#[must_use]
pub fn private_owned(schema: Option<String>) -> Self {
Self {
namespace: Namespace::Private,
database: None,
schema: schema.map(Cow::Owned),
}
}
#[must_use]
pub fn public_owned(
namespace: String,
database: Option<String>,
schema: Option<String>,
) -> Self {
Self {
namespace: Namespace::Public(Cow::Owned(namespace)),
database: database.map(Cow::Owned),
schema: schema.map(Cow::Owned),
}
}
#[must_use]
pub fn into_inner(
self,
) -> (
Namespace,
Option<Cow<'static, str>>,
Option<Cow<'static, str>>,
) {
(self.namespace, self.database, self.schema)
}
}
const fn borrowed(s: Option<&'static str>) -> Option<Cow<'static, str>> {
match s {
Some(s) => Some(Cow::Borrowed(s)),
None => None,
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Namespace {
Private,
Public(Cow<'static, str>),
}