|
| 1 | +use crate::environment::Environment; |
| 2 | +use crate::error_message; |
| 3 | + use crate::ifn::IFn; |
| 4 | +use crate::type_tag::TypeTag; |
| 5 | +use crate::protocol::ProtocolCastable; |
| 6 | +use crate::protocols; |
| 7 | +use crate::traits::IMeta; |
| 8 | +use crate::value::{ToValue, Value}; |
| 9 | +use std::rc::Rc; |
| 10 | + |
| 11 | +/// Returns meta for symbol |
| 12 | +/// Todo: currently uses form (meta 'clojure.string/join) |
| 13 | +/// should use #'var-form |
| 14 | +/// TODO: macro: true/false |
| 15 | +/// TODO: argslists for functions |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct MetaFn { |
| 18 | + enclosing_environment: Rc<Environment>, |
| 19 | +} |
| 20 | +impl MetaFn { |
| 21 | + pub fn new(enclosing_environment: Rc<Environment>) -> MetaFn { |
| 22 | + MetaFn { |
| 23 | + enclosing_environment, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | +impl ToValue for MetaFn { |
| 28 | + fn to_value(&self) -> Value { |
| 29 | + Value::IFn(Rc::new(self.clone())) |
| 30 | + } |
| 31 | +} |
| 32 | +impl IFn for MetaFn { |
| 33 | + fn invoke(&self, args: Vec<Rc<Value>>) -> Value { |
| 34 | + if args.len() != 1 { |
| 35 | + return error_message::wrong_arg_count(1, args.len()); |
| 36 | + } |
| 37 | + match args.get(0).unwrap().try_as_protocol::<protocols::IMeta>() { |
| 38 | + Some(imeta) => imeta.meta().to_value(), |
| 39 | + // In order to avoid having the cryptic error messages of Clojure, we're experimenting here |
| 40 | + // already with some other error messages. As we finds ones we like, they will likewise be |
| 41 | + // abstracted out to their own functions -- for now, they're just one offs |
| 42 | + _ => error_message::custom(&format!( |
| 43 | + "In (meta ..), .. must be an instance of IMeta, and {} is of type {}, which is not", |
| 44 | + args.get(0).unwrap(), |
| 45 | + args.get(0).unwrap().type_tag()) |
| 46 | + ) |
| 47 | + //error_message::cast_error(Cast("IMeta"), TypeTag::PersistentListMap) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments