forked from Orchid/orchid
69 lines
2.0 KiB
Rust
69 lines
2.0 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use orchid_api_derive::{Coding, Hierarchy};
|
|
use orchid_api_traits::Request;
|
|
use orchid_base::{NameLike, ReqHandleExt, Sym, es, is, mk_errv};
|
|
use orchid_extension::gen_expr::new_atom;
|
|
use orchid_extension::tree::{GenMember, fun, prefix};
|
|
use orchid_extension::{
|
|
Atomic, Expr, ExprHandle, OwnedAtom, OwnedVariant, Supports, TAtom, sys_req,
|
|
};
|
|
|
|
use crate::std::std_system::StdReq;
|
|
use crate::std::string::str_atom::IntStrAtom;
|
|
use crate::std::string::to_string::ToStringMethod;
|
|
use crate::{HomoTpl, StdSystem, api};
|
|
|
|
#[derive(Clone, Coding)]
|
|
pub struct SymAtomData(pub api::TStrv);
|
|
#[derive(Clone)]
|
|
pub struct SymAtom(pub(crate) Sym);
|
|
impl Atomic for SymAtom {
|
|
type Data = SymAtomData;
|
|
type Variant = OwnedVariant;
|
|
}
|
|
impl OwnedAtom for SymAtom {
|
|
type Refs = ();
|
|
async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(SymAtomData(self.0.tok().to_api())) }
|
|
}
|
|
impl Supports<ToStringMethod> for SymAtom {
|
|
async fn handle<'a>(
|
|
&self,
|
|
hand: Box<dyn orchid_base::ReqHandle<'a> + '_>,
|
|
req: ToStringMethod,
|
|
) -> std::io::Result<orchid_base::Receipt<'a>> {
|
|
hand.reply(&req, &self.0.to_string()).await
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Coding, Hierarchy)]
|
|
#[extends(StdReq)]
|
|
pub struct CreateSymAtom(pub api::TStrv);
|
|
impl Request for CreateSymAtom {
|
|
type Response = api::ExprTicket;
|
|
}
|
|
|
|
pub async fn sym_expr(sym: Sym) -> Expr {
|
|
Expr::from_handle(ExprHandle::deserialize(
|
|
sys_req::<StdSystem, _>(CreateSymAtom(sym.to_api())).await,
|
|
))
|
|
}
|
|
|
|
pub async fn gen_sym_lib() -> Vec<GenMember> {
|
|
prefix("std::refl::sym", [
|
|
fun(true, "from_str", async move |str: TAtom<IntStrAtom>| {
|
|
match Sym::parse(&es(*str).await).await {
|
|
Ok(sym) => Ok(new_atom(SymAtom(sym))),
|
|
Err(_) => Err(mk_errv(
|
|
is("Cannot parse sym from empty string").await,
|
|
"Empty string passed to std::refl::sym::from_str",
|
|
[str.pos()],
|
|
)),
|
|
}
|
|
}),
|
|
fun(true, "to_tpl", async move |sym: TAtom<SymAtom>| {
|
|
HomoTpl(sym.own().await.0.segs().map(|seg| new_atom(IntStrAtom(seg))).collect())
|
|
}),
|
|
])
|
|
}
|