68 lines
2.0 KiB
Rust
68 lines
2.0 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use orchid_api::TStrv;
|
|
use orchid_api_derive::{Coding, Hierarchy};
|
|
use orchid_api_traits::Request;
|
|
use orchid_base::error::mk_errv;
|
|
use orchid_base::name::{NameLike, Sym};
|
|
use orchid_extension::atom::{Atomic, Supports, TAtom};
|
|
use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own};
|
|
use orchid_extension::context::i;
|
|
use orchid_extension::expr::{Expr, ExprHandle};
|
|
use orchid_extension::system::dep_req;
|
|
use orchid_extension::tree::{GenMember, fun, prefix};
|
|
|
|
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(&self, _: ToStringMethod) -> <ToStringMethod as Request>::Response {
|
|
self.0.to_string()
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Coding, Hierarchy)]
|
|
#[extends(StdReq)]
|
|
pub struct CreateSymAtom(pub TStrv);
|
|
impl Request for CreateSymAtom {
|
|
type Response = api::ExprTicket;
|
|
}
|
|
|
|
pub async fn sym_expr(sym: Sym) -> Expr {
|
|
Expr::from_handle(ExprHandle::deserialize(
|
|
dep_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(&i().ex(*str).await, &i()).await {
|
|
Ok(sym) => Ok(SymAtom(sym)),
|
|
Err(_) => Err(mk_errv(
|
|
i().i("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(own(&sym).await.0.segs().map(IntStrAtom).collect())
|
|
}),
|
|
])
|
|
}
|