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::interner::{es, is}; use orchid_base::name::{NameLike, Sym}; use orchid_extension::atom::{Atomic, Supports, TAtom}; use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; 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 for SymAtom { async fn handle(&self, _: ToStringMethod) -> ::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::(CreateSymAtom(sym.to_api())).await, )) } pub async fn gen_sym_lib() -> Vec { prefix("std::refl::sym", [ fun(true, "from_str", async move |str: TAtom| { match Sym::parse(&es(*str).await).await { Ok(sym) => Ok(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| { HomoTpl(own(&sym).await.0.segs().map(IntStrAtom).collect()) }), ]) }