Protocols and operators mostly

This commit is contained in:
2026-01-21 22:22:58 +01:00
parent 75b05a2965
commit f38193edcc
33 changed files with 578 additions and 147 deletions

View File

@@ -9,36 +9,34 @@ use orchid_api_traits::{Encode, Request};
use orchid_base::error::{OrcRes, mk_errv};
use orchid_base::format::{FmtCtx, FmtUnit};
use orchid_base::interner::{IStr, es, is};
use orchid_base::name::Sym;
use orchid_base::sym;
use orchid_extension::atom::{AtomMethod, Atomic, MethodSetBuilder, Supports, TAtom};
use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant};
use orchid_extension::conv::TryFromExpr;
use orchid_extension::expr::Expr;
use orchid_extension::gen_expr::sym_ref;
use crate::std::protocol::types::{GetImplMethod, GetTagIdMethod};
use crate::std::string::to_string::ToStringMethod;
#[derive(Copy, Clone, Debug, Coding)]
pub struct StringGetVal;
impl Request for StringGetVal {
pub struct StringGetValMethod;
impl Request for StringGetValMethod {
type Response = Rc<String>;
}
impl AtomMethod for StringGetVal {
impl AtomMethod for StringGetValMethod {
const NAME: &str = "std::string_get_val";
}
impl Supports<StringGetVal> for StrAtom {
async fn handle(&self, _: StringGetVal) -> <StringGetVal as Request>::Response { self.0.clone() }
}
impl Supports<ToStringMethod> for StrAtom {
async fn handle(&self, _: ToStringMethod) -> <ToStringMethod as Request>::Response {
self.0.as_str().to_string()
}
}
#[derive(Clone)]
pub struct StrAtom(Rc<String>);
impl Atomic for StrAtom {
type Variant = OwnedVariant;
type Data = ();
fn reg_reqs() -> MethodSetBuilder<Self> { MethodSetBuilder::new().handle::<StringGetVal>() }
fn reg_reqs() -> MethodSetBuilder<Self> {
MethodSetBuilder::new().handle::<StringGetValMethod>().handle::<ToStringMethod>()
}
}
impl StrAtom {
pub fn new(str: Rc<String>) -> Self { Self(str) }
@@ -60,12 +58,44 @@ impl OwnedAtom for StrAtom {
Self::new(Rc::new(ctx.read::<String>().await))
}
}
impl Supports<StringGetValMethod> for StrAtom {
async fn handle(&self, _: StringGetValMethod) -> <StringGetValMethod as Request>::Response {
self.0.clone()
}
}
impl Supports<ToStringMethod> for StrAtom {
async fn handle(&self, _: ToStringMethod) -> <ToStringMethod as Request>::Response {
self.0.as_str().to_string()
}
}
impl Supports<GetTagIdMethod> for StrAtom {
async fn handle(&self, _: GetTagIdMethod) -> <GetTagIdMethod as Request>::Response {
Sym::literal("std::string::StrAtom").await.to_api()
}
}
impl Supports<GetImplMethod> for StrAtom {
async fn handle(&self, req: GetImplMethod) -> <GetImplMethod as Request>::Response {
let name = Sym::from_api(req.0).await;
let val = if name == sym!(std::ops::add) {
sym_ref(sym!(std::string::concat))
} else {
return None;
};
Some(val.create().await.serialize().await)
}
}
#[derive(Debug, Clone)]
pub struct IntStrAtom(pub(crate) IStr);
impl Atomic for IntStrAtom {
type Variant = OwnedVariant;
type Data = orchid_api::TStr;
fn reg_reqs() -> MethodSetBuilder<Self> {
MethodSetBuilder::new()
.handle::<GetTagIdMethod>()
.handle::<GetImplMethod>()
.handle::<ToStringMethod>()
}
}
impl From<IStr> for IntStrAtom {
fn from(value: IStr) -> Self { Self(value) }
@@ -94,6 +124,22 @@ impl Supports<ToStringMethod> for IntStrAtom {
self.0.to_string()
}
}
impl Supports<GetTagIdMethod> for IntStrAtom {
async fn handle(&self, _: GetTagIdMethod) -> <GetTagIdMethod as Request>::Response {
Sym::literal("std::string::IntStrAtom").await.to_api()
}
}
impl Supports<GetImplMethod> for IntStrAtom {
async fn handle(&self, req: GetImplMethod) -> <GetImplMethod as Request>::Response {
let name = Sym::from_api(req.0).await;
let val = if name == sym!(std::ops::add) {
sym_ref(sym!(std::string::concat))
} else {
return None;
};
Some(val.create().await.serialize().await)
}
}
#[derive(Clone)]
pub struct OrcString {
@@ -109,7 +155,7 @@ impl OrcString {
pub async fn get_string(&self) -> Rc<String> {
match &self.kind {
OrcStringKind::Int(tok) => es(**tok).await.rc(),
OrcStringKind::Val(atom) => atom.request(StringGetVal).await,
OrcStringKind::Val(atom) => atom.request(StringGetValMethod).await,
}
}
}