1#![no_std]
3#![forbid(unsafe_code)]
4#![warn(missing_docs)]
5
6extern crate alloc;
7
8use alloc::string::{String, ToString};
9use alloc::{boxed::Box, rc::Rc, vec::Vec};
10use core::cmp::Ordering;
11use core::fmt::{self, Debug};
12use jaq_core::box_iter::{box_once, BoxIter};
13use jaq_core::{load, ops, path, Exn, Native, RunPtr};
14use jaq_std::{run, unary, v, Filter};
15
16#[cfg(feature = "hifijson")]
17use hifijson::{LexAlloc, Token};
18
19#[derive(Clone, Debug, Default)]
30pub enum Val {
31 #[default]
32 Null,
34 Bool(bool),
36 Int(isize),
38 Float(f64),
40 Num(Rc<String>),
42 Str(Rc<String>),
44 Arr(Rc<Vec<Val>>),
46 Obj(Rc<Map<Rc<String>, Val>>),
48}
49
50#[derive(Clone, Debug, PartialEq, Eq)]
52enum Type {
53 Int,
55 Float,
57 Num,
59 Str,
61 Arr,
63 Iter,
65 Range,
67}
68
69impl Type {
70 fn as_str(&self) -> &'static str {
71 match self {
72 Self::Int => "integer",
73 Self::Float => "floating-point number",
74 Self::Num => "number",
75 Self::Str => "string",
76 Self::Arr => "array",
77 Self::Iter => "iterable (array or object)",
78 Self::Range => "rangeable (array or string)",
79 }
80 }
81}
82
83type Map<K, V> = indexmap::IndexMap<K, V, foldhash::fast::RandomState>;
85
86pub type Error = jaq_core::Error<Val>;
88pub type ValR = jaq_core::ValR<Val>;
90pub type ValX<'a> = jaq_core::ValX<'a, Val>;
92
93fn rc_unwrap_or_clone<T: Clone>(a: Rc<T>) -> T {
97 Rc::try_unwrap(a).unwrap_or_else(|a| (*a).clone())
98}
99
100impl jaq_core::ValT for Val {
101 fn from_num(n: &str) -> ValR {
102 Ok(Val::Num(Rc::new(n.to_string())))
103 }
104
105 fn from_map<I: IntoIterator<Item = (Self, Self)>>(iter: I) -> ValR {
106 let iter = iter.into_iter().map(|(k, v)| Ok((k.into_str()?, v)));
107 Ok(Self::obj(iter.collect::<Result<_, _>>()?))
108 }
109
110 fn values(self) -> Box<dyn Iterator<Item = ValR>> {
111 match self {
112 Self::Arr(a) => Box::new(rc_unwrap_or_clone(a).into_iter().map(Ok)),
113 Self::Obj(o) => Box::new(rc_unwrap_or_clone(o).into_iter().map(|(_k, v)| Ok(v))),
114 _ => box_once(Err(Error::typ(self, Type::Iter.as_str()))),
115 }
116 }
117
118 fn index(self, index: &Self) -> ValR {
119 match (self, index) {
120 (Val::Arr(a), Val::Int(i)) => {
121 Ok(abs_index(*i, a.len()).map_or(Val::Null, |i| a[i].clone()))
122 }
123 (Val::Obj(o), Val::Str(s)) => Ok(o.get(s).cloned().unwrap_or(Val::Null)),
124 (s @ (Val::Arr(_) | Val::Obj(_)), _) => Err(Error::index(s, index.clone())),
125 (s, _) => Err(Error::typ(s, Type::Iter.as_str())),
126 }
127 }
128
129 fn range(self, range: jaq_core::val::Range<&Self>) -> ValR {
130 let (from, upto) = (range.start, range.end);
131 match self {
132 Val::Arr(a) => {
133 let len = a.len();
134 let from = from.as_ref().map(|i| i.as_int()).transpose();
135 let upto = upto.as_ref().map(|i| i.as_int()).transpose();
136 from.and_then(|from| Ok((from, upto?))).map(|(from, upto)| {
137 let from = abs_bound(from, len, 0);
138 let upto = abs_bound(upto, len, len);
139 let (skip, take) = skip_take(from, upto);
140 a.iter().skip(skip).take(take).cloned().collect()
141 })
142 }
143 Val::Str(s) => {
144 let len = s.chars().count();
145 let from = from.as_ref().map(|i| i.as_int()).transpose();
146 let upto = upto.as_ref().map(|i| i.as_int()).transpose();
147 from.and_then(|from| Ok((from, upto?))).map(|(from, upto)| {
148 let from = abs_bound(from, len, 0);
149 let upto = abs_bound(upto, len, len);
150 let (skip, take) = skip_take(from, upto);
151 Val::from(s.chars().skip(skip).take(take).collect::<String>())
152 })
153 }
154 _ => Err(Error::typ(self, Type::Range.as_str())),
155 }
156 }
157
158 fn map_values<'a, I: Iterator<Item = ValX<'a>>>(
159 self,
160 opt: path::Opt,
161 f: impl Fn(Self) -> I,
162 ) -> ValX<'a> {
163 match self {
164 Self::Arr(a) => {
165 let iter = rc_unwrap_or_clone(a).into_iter().flat_map(f);
166 Ok(iter.collect::<Result<_, _>>()?)
167 }
168 Self::Obj(o) => {
169 let iter = rc_unwrap_or_clone(o).into_iter();
170 let iter = iter.filter_map(|(k, v)| f(v).next().map(|v| Ok((k, v?))));
171 Ok(Self::obj(iter.collect::<Result<_, Exn<_>>>()?))
172 }
173 v => opt.fail(v, |v| Exn::from(Error::typ(v, Type::Iter.as_str()))),
174 }
175 }
176
177 fn map_index<'a, I: Iterator<Item = ValX<'a>>>(
178 mut self,
179 index: &Self,
180 opt: path::Opt,
181 f: impl Fn(Self) -> I,
182 ) -> ValX<'a> {
183 match self {
184 Val::Obj(ref mut o) => {
185 use indexmap::map::Entry::{Occupied, Vacant};
186 let o = Rc::make_mut(o);
187 let i = match index {
188 Val::Str(s) => s,
189 i => return opt.fail(self, |v| Exn::from(Error::index(v, i.clone()))),
190 };
191 match o.entry(Rc::clone(i)) {
192 Occupied(mut e) => {
193 let v = core::mem::take(e.get_mut());
194 match f(v).next().transpose()? {
195 Some(y) => e.insert(y),
196 None => e.swap_remove(),
199 };
200 }
201 Vacant(e) => {
202 if let Some(y) = f(Val::Null).next().transpose()? {
203 e.insert(y);
204 }
205 }
206 }
207 Ok(self)
208 }
209 Val::Arr(ref mut a) => {
210 let a = Rc::make_mut(a);
211 let abs_or = |i| {
212 abs_index(i, a.len()).ok_or(Error::str(format_args!("index {i} out of bounds")))
213 };
214 let i = match index.as_int().and_then(abs_or) {
215 Ok(i) => i,
216 Err(e) => return opt.fail(self, |_| Exn::from(e)),
217 };
218
219 let x = core::mem::take(&mut a[i]);
220 if let Some(y) = f(x).next().transpose()? {
221 a[i] = y;
222 } else {
223 a.remove(i);
224 }
225 Ok(self)
226 }
227 _ => opt.fail(self, |v| Exn::from(Error::typ(v, Type::Iter.as_str()))),
228 }
229 }
230
231 fn map_range<'a, I: Iterator<Item = ValX<'a>>>(
232 mut self,
233 range: jaq_core::val::Range<&Self>,
234 opt: path::Opt,
235 f: impl Fn(Self) -> I,
236 ) -> ValX<'a> {
237 if let Val::Arr(ref mut a) = self {
238 let a = Rc::make_mut(a);
239 let from = range.start.as_ref().map(|i| i.as_int()).transpose();
240 let upto = range.end.as_ref().map(|i| i.as_int()).transpose();
241 let (from, upto) = match from.and_then(|from| Ok((from, upto?))) {
242 Ok(from_upto) => from_upto,
243 Err(e) => return opt.fail(self, |_| Exn::from(e)),
244 };
245 let len = a.len();
246 let from = abs_bound(from, len, 0);
247 let upto = abs_bound(upto, len, len);
248 let (skip, take) = skip_take(from, upto);
249 let arr = a.iter().skip(skip).take(take).cloned().collect();
250 let y = f(arr).map(|y| y?.into_arr().map_err(Exn::from)).next();
251 let y = y.transpose()?.unwrap_or_default();
252 a.splice(skip..skip + take, (*y).clone());
253 Ok(self)
254 } else {
255 opt.fail(self, |v| Exn::from(Error::typ(v, Type::Arr.as_str())))
256 }
257 }
258
259 fn as_bool(&self) -> bool {
261 !matches!(self, Self::Null | Self::Bool(false))
262 }
263
264 fn as_str(&self) -> Option<&str> {
266 if let Self::Str(s) = self {
267 Some(s)
268 } else {
269 None
270 }
271 }
272}
273
274impl jaq_std::ValT for Val {
275 fn into_seq<S: FromIterator<Self>>(self) -> Result<S, Self> {
276 match self {
277 Self::Arr(a) => match Rc::try_unwrap(a) {
278 Ok(a) => Ok(a.into_iter().collect()),
279 Err(a) => Ok(a.iter().cloned().collect()),
280 },
281 _ => Err(self),
282 }
283 }
284
285 fn as_isize(&self) -> Option<isize> {
286 match self {
287 Self::Int(i) => Some(*i),
288 _ => None,
289 }
290 }
291
292 fn as_f64(&self) -> Result<f64, Error> {
293 Self::as_float(self)
294 }
295}
296
297pub fn defs() -> impl Iterator<Item = load::parse::Def<&'static str>> {
299 load::parse(include_str!("defs.jq"), |p| p.defs())
300 .unwrap()
301 .into_iter()
302}
303
304impl Val {
305 fn length(&self) -> ValR {
310 match self {
311 Val::Null => Ok(Val::Int(0)),
312 Val::Bool(_) => Err(Error::str(format_args!("{self} has no length"))),
313 Val::Int(i) => Ok(Val::Int(i.abs())),
314 Val::Num(n) => Val::from_dec_str(n).length(),
315 Val::Float(f) => Ok(Val::Float(f.abs())),
316 Val::Str(s) => Ok(Val::Int(s.chars().count() as isize)),
317 Val::Arr(a) => Ok(Val::Int(a.len() as isize)),
318 Val::Obj(o) => Ok(Val::Int(o.len() as isize)),
319 }
320 }
321
322 fn indices<'a>(&'a self, y: &'a Val) -> Result<Box<dyn Iterator<Item = usize> + 'a>, Error> {
324 match (self, y) {
325 (Val::Str(_), Val::Str(y)) if y.is_empty() => Ok(Box::new(core::iter::empty())),
326 (Val::Arr(_), Val::Arr(y)) if y.is_empty() => Ok(Box::new(core::iter::empty())),
327 (Val::Str(x), Val::Str(y)) => {
328 let iw = str_windows(x, y.chars().count()).enumerate();
329 Ok(Box::new(iw.filter_map(|(i, w)| (w == **y).then_some(i))))
330 }
331 (Val::Arr(x), Val::Arr(y)) => {
332 let iw = x.windows(y.len()).enumerate();
333 Ok(Box::new(iw.filter_map(|(i, w)| (w == **y).then_some(i))))
334 }
335 (Val::Arr(x), y) => {
336 let ix = x.iter().enumerate();
337 Ok(Box::new(ix.filter_map(move |(i, x)| (x == y).then_some(i))))
338 }
339 (x, y) => Err(Error::index(x.clone(), y.clone())),
340 }
341 }
342}
343
344fn str_windows(line: &str, n: usize) -> impl Iterator<Item = &str> {
348 line.char_indices()
349 .zip(line.char_indices().skip(n).chain(Some((line.len(), ' '))))
350 .map(move |((i, _), (j, _))| &line[i..j])
351}
352
353#[cfg(feature = "parse")]
355pub fn funs() -> impl Iterator<Item = Filter<Native<Val>>> {
356 base_funs().chain([run(parse_fun())])
357}
358
359pub fn base_funs() -> impl Iterator<Item = Filter<Native<Val>>> {
361 base().into_vec().into_iter().map(run)
362}
363
364fn box_once_err<'a>(r: ValR) -> BoxIter<'a, ValX<'a>> {
365 box_once(r.map_err(Exn::from))
366}
367
368fn base() -> Box<[Filter<RunPtr<Val>>]> {
369 Box::new([
370 ("tojson", v(0), |_, cv| {
371 box_once(Ok(cv.1.to_string().into()))
372 }),
373 ("length", v(0), |_, cv| box_once_err(cv.1.length())),
374 ("path_values", v(0), |_, cv| {
375 let pair = |(p, v)| Ok([p, v].into_iter().collect());
376 Box::new(cv.1.path_values(Vec::new()).skip(1).map(pair))
377 }),
378 ("paths", v(0), |_, cv| {
379 Box::new(cv.1.path_values(Vec::new()).skip(1).map(|(p, _v)| Ok(p)))
380 }),
381 ("keys_unsorted", v(0), |_, cv| {
382 let keys = cv.1.key_values().map(|kvs| kvs.map(|(k, _v)| k).collect());
383 let err = || Error::typ(cv.1.clone(), Type::Iter.as_str());
384 box_once_err(keys.ok_or_else(err))
385 }),
386 ("contains", v(1), |_, cv| {
387 unary(cv, |x, y| Ok(Val::from(x.contains(&y))))
388 }),
389 ("has", v(1), |_, cv| {
390 unary(cv, |v, k| v.has(&k).map(Val::from))
391 }),
392 ("indices", v(1), |_, cv| {
393 let to_int = |i: usize| Val::Int(i.try_into().unwrap());
394 unary(cv, move |x, v| {
395 x.indices(&v).map(|idxs| idxs.map(to_int).collect())
396 })
397 }),
398 ("bsearch", v(1), |_, cv| {
399 let to_idx = |r: Result<_, _>| r.map_or_else(|i| -1 - i as isize, |i| i as isize);
400 unary(cv, move |a, x| {
401 a.as_arr().map(|a| Val::Int(to_idx(a.binary_search(&x))))
402 })
403 }),
404 ])
405}
406
407#[cfg(feature = "parse")]
408fn from_json(s: &str) -> ValR {
410 use hifijson::token::Lex;
411 let mut lexer = hifijson::SliceLexer::new(s.as_bytes());
412 lexer
413 .exactly_one(Val::parse)
414 .map_err(|e| Error::str(format_args!("cannot parse {s} as JSON: {e}")))
415}
416
417#[cfg(feature = "parse")]
418fn parse_fun() -> Filter<RunPtr<Val>> {
419 ("fromjson", v(0), |_, cv| {
420 box_once_err(cv.1.as_str().and_then(|s| from_json(s)))
421 })
422}
423
424fn skip_take(from: usize, until: usize) -> (usize, usize) {
425 (from, until.saturating_sub(from))
426}
427
428fn abs_bound(i: Option<isize>, len: usize, default: usize) -> usize {
431 i.map_or(default, |i| core::cmp::min(wrap(i, len).unwrap_or(0), len))
432}
433
434fn abs_index(i: isize, len: usize) -> Option<usize> {
436 wrap(i, len).filter(|i| *i < len)
437}
438
439fn wrap(i: isize, len: usize) -> Option<usize> {
440 if i >= 0 {
441 Some(i as usize)
442 } else if len < -i as usize {
443 None
444 } else {
445 Some(len - (-i as usize))
446 }
447}
448
449#[test]
450fn wrap_test() {
451 let len = 4;
452 assert_eq!(wrap(0, len), Some(0));
453 assert_eq!(wrap(8, len), Some(8));
454 assert_eq!(wrap(-1, len), Some(3));
455 assert_eq!(wrap(-4, len), Some(0));
456 assert_eq!(wrap(-8, len), None);
457}
458
459impl Val {
460 pub fn obj(m: Map<Rc<String>, Self>) -> Self {
462 Self::Obj(m.into())
463 }
464
465 fn as_int(&self) -> Result<isize, Error> {
467 match self {
468 Self::Int(i) => Ok(*i),
469 _ => Err(Error::typ(self.clone(), Type::Int.as_str())),
470 }
471 }
472
473 fn as_float(&self) -> Result<f64, Error> {
476 match self {
477 Self::Int(n) => Ok(*n as f64),
478 Self::Float(n) => Ok(*n),
479 Self::Num(n) => n
480 .parse()
481 .or(Err(Error::typ(self.clone(), Type::Float.as_str()))),
482 _ => Err(Error::typ(self.clone(), Type::Float.as_str())),
483 }
484 }
485
486 fn into_str(self) -> Result<Rc<String>, Error> {
488 match self {
489 Self::Str(s) => Ok(s),
490 _ => Err(Error::typ(self, Type::Str.as_str())),
491 }
492 }
493
494 #[cfg(feature = "parse")]
495 fn as_str(&self) -> Result<&Rc<String>, Error> {
497 match self {
498 Self::Str(s) => Ok(s),
499 _ => Err(Error::typ(self.clone(), Type::Str.as_str())),
500 }
501 }
502
503 fn into_arr(self) -> Result<Rc<Vec<Self>>, Error> {
505 match self {
506 Self::Arr(a) => Ok(a),
507 _ => Err(Error::typ(self, Type::Arr.as_str())),
508 }
509 }
510
511 fn as_arr(&self) -> Result<&Rc<Vec<Self>>, Error> {
512 match self {
513 Self::Arr(a) => Ok(a),
514 _ => Err(Error::typ(self.clone(), Type::Arr.as_str())),
515 }
516 }
517
518 fn from_dec_str(n: &str) -> Self {
520 n.parse().map_or(Self::Null, Self::Float)
521 }
522
523 fn has(&self, key: &Self) -> Result<bool, Error> {
527 match (self, key) {
528 (Self::Arr(a), Self::Int(i)) if *i >= 0 => Ok((*i as usize) < a.len()),
529 (Self::Obj(o), Self::Str(s)) => Ok(o.contains_key(&**s)),
530 _ => Err(Error::index(self.clone(), key.clone())),
531 }
532 }
533
534 fn key_values(&self) -> Option<BoxIter<(Val, &Val)>> {
538 let arr_idx = |(i, x)| (Self::Int(i as isize), x);
539 Some(match self {
540 Self::Arr(a) => Box::new(a.iter().enumerate().map(arr_idx)),
541 Self::Obj(o) => Box::new(o.iter().map(|(k, v)| (Self::Str(Rc::clone(k)), v))),
542 _ => return None,
543 })
544 }
545
546 fn path_values<'a>(self, path: Vec<Val>) -> BoxIter<'a, (Val, Val)> {
548 let head = (path.iter().cloned().collect(), self.clone());
549 let f = move |k| path.iter().cloned().chain([k]).collect();
550 let kvs = self.key_values().into_iter().flatten();
551 let kvs: Vec<_> = kvs.map(|(k, v)| (k, v.clone())).collect();
552 let tail = kvs.into_iter().flat_map(move |(k, v)| v.path_values(f(k)));
553 Box::new(core::iter::once(head).chain(tail))
554 }
555
556 fn contains(&self, other: &Self) -> bool {
563 match (self, other) {
564 (Self::Str(l), Self::Str(r)) => l.contains(&**r),
565 (Self::Arr(l), Self::Arr(r)) => r.iter().all(|r| l.iter().any(|l| l.contains(r))),
566 (Self::Obj(l), Self::Obj(r)) => r
567 .iter()
568 .all(|(k, r)| l.get(k).map_or(false, |l| l.contains(r))),
569 _ => self == other,
570 }
571 }
572
573 #[cfg(feature = "hifijson")]
579 pub fn parse(token: Token, lexer: &mut impl LexAlloc) -> Result<Self, hifijson::Error> {
580 use hifijson::{token, Error};
581 match token {
582 Token::Null => Ok(Self::Null),
583 Token::True => Ok(Self::Bool(true)),
584 Token::False => Ok(Self::Bool(false)),
585 Token::DigitOrMinus => {
586 let (num, parts) = lexer.num_string()?;
587 if parts.dot.is_none() && parts.exp.is_none() {
589 if let Ok(i) = num.parse() {
591 return Ok(Self::Int(i));
592 }
593 }
594 Ok(Self::Num(Rc::new(num.to_string())))
595 }
596 Token::Quote => Ok(Self::from(lexer.str_string()?.to_string())),
597 Token::LSquare => Ok(Self::Arr({
598 let mut arr = Vec::new();
599 lexer.seq(Token::RSquare, |token, lexer| {
600 arr.push(Self::parse(token, lexer)?);
601 Ok::<_, hifijson::Error>(())
602 })?;
603 arr.into()
604 })),
605 Token::LCurly => Ok(Self::obj({
606 let mut obj = Map::default();
607 lexer.seq(Token::RCurly, |token, lexer| {
608 let key =
609 lexer.str_colon(token, |lexer| lexer.str_string().map_err(Error::Str))?;
610
611 let token = lexer.ws_token().ok_or(token::Expect::Value)?;
612 let value = Self::parse(token, lexer)?;
613 obj.insert(Rc::new(key.to_string()), value);
614 Ok::<_, Error>(())
615 })?;
616 obj
617 })),
618 _ => Err(token::Expect::Value)?,
619 }
620 }
621}
622
623#[cfg(feature = "serde_json")]
624impl From<serde_json::Value> for Val {
625 fn from(v: serde_json::Value) -> Self {
626 use serde_json::Value::*;
627 match v {
628 Null => Self::Null,
629 Bool(b) => Self::Bool(b),
630 Number(n) => n
631 .to_string()
632 .parse()
633 .map_or_else(|_| Self::Num(Rc::new(n.to_string())), Self::Int),
634 String(s) => Self::from(s),
635 Array(a) => a.into_iter().map(Self::from).collect(),
636 Object(o) => Self::obj(o.into_iter().map(|(k, v)| (Rc::new(k), v.into())).collect()),
637 }
638 }
639}
640
641#[cfg(feature = "serde_json")]
642impl From<Val> for serde_json::Value {
643 fn from(v: Val) -> Self {
644 use core::str::FromStr;
645 use serde_json::Value::*;
646 match v {
647 Val::Null => Null,
648 Val::Bool(b) => Bool(b),
649 Val::Int(i) => Number(i.into()),
650 Val::Float(f) => serde_json::Number::from_f64(f).map_or(Null, Number),
651 Val::Num(n) => Number(serde_json::Number::from_str(&n).unwrap()),
652 Val::Str(s) => String((*s).clone()),
653 Val::Arr(a) => Array(a.iter().map(|x| x.clone().into()).collect()),
654 Val::Obj(o) => Object(
655 o.iter()
656 .map(|(k, v)| ((**k).clone(), v.clone().into()))
657 .collect(),
658 ),
659 }
660 }
661}
662
663impl From<bool> for Val {
664 fn from(b: bool) -> Self {
665 Self::Bool(b)
666 }
667}
668
669impl From<isize> for Val {
670 fn from(i: isize) -> Self {
671 Self::Int(i)
672 }
673}
674
675impl From<f64> for Val {
676 fn from(f: f64) -> Self {
677 Self::Float(f)
678 }
679}
680
681impl From<String> for Val {
682 fn from(s: String) -> Self {
683 Self::Str(Rc::new(s))
684 }
685}
686
687impl FromIterator<Self> for Val {
688 fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
689 Self::Arr(Rc::new(iter.into_iter().collect()))
690 }
691}
692
693impl core::ops::Add for Val {
694 type Output = ValR;
695 fn add(self, rhs: Self) -> Self::Output {
696 use Val::*;
697 match (self, rhs) {
698 (Null, x) | (x, Null) => Ok(x),
700 (Int(x), Int(y)) => Ok(Int(x + y)),
701 (Int(i), Float(f)) | (Float(f), Int(i)) => Ok(Float(f + i as f64)),
702 (Float(x), Float(y)) => Ok(Float(x + y)),
703 (Num(n), r) => Self::from_dec_str(&n) + r,
704 (l, Num(n)) => l + Self::from_dec_str(&n),
705 (Str(mut l), Str(r)) => {
706 Rc::make_mut(&mut l).push_str(&r);
707 Ok(Str(l))
708 }
709 (Arr(mut l), Arr(r)) => {
710 Rc::make_mut(&mut l).extend(r.iter().cloned());
712 Ok(Arr(l))
713 }
714 (Obj(mut l), Obj(r)) => {
715 Rc::make_mut(&mut l).extend(r.iter().map(|(k, v)| (k.clone(), v.clone())));
716 Ok(Obj(l))
717 }
718 (l, r) => Err(Error::math(l, ops::Math::Add, r)),
719 }
720 }
721}
722
723impl core::ops::Sub for Val {
724 type Output = ValR;
725 fn sub(self, rhs: Self) -> Self::Output {
726 use Val::*;
727 match (self, rhs) {
728 (Int(x), Int(y)) => Ok(Int(x - y)),
729 (Float(f), Int(i)) => Ok(Float(f - i as f64)),
730 (Int(i), Float(f)) => Ok(Float(i as f64 - f)),
731 (Float(x), Float(y)) => Ok(Float(x - y)),
732 (Num(n), r) => Self::from_dec_str(&n) - r,
733 (l, Num(n)) => l - Self::from_dec_str(&n),
734 (Arr(mut l), Arr(r)) => {
735 let r = r.iter().collect::<alloc::collections::BTreeSet<_>>();
736 Rc::make_mut(&mut l).retain(|x| !r.contains(x));
737 Ok(Arr(l))
738 }
739 (l, r) => Err(Error::math(l, ops::Math::Sub, r)),
740 }
741 }
742}
743
744fn obj_merge(l: &mut Rc<Map<Rc<String>, Val>>, r: Rc<Map<Rc<String>, Val>>) {
745 let l = Rc::make_mut(l);
746 let r = rc_unwrap_or_clone(r).into_iter();
747 r.for_each(|(k, v)| match (l.get_mut(&k), v) {
748 (Some(Val::Obj(l)), Val::Obj(r)) => obj_merge(l, r),
749 (Some(l), r) => *l = r,
750 (None, r) => {
751 l.insert(k, r);
752 }
753 });
754}
755
756impl core::ops::Mul for Val {
757 type Output = ValR;
758 fn mul(self, rhs: Self) -> Self::Output {
759 use Val::*;
760 match (self, rhs) {
761 (Int(x), Int(y)) => Ok(Int(x * y)),
762 (Float(f), Int(i)) | (Int(i), Float(f)) => Ok(Float(f * i as f64)),
763 (Float(x), Float(y)) => Ok(Float(x * y)),
764 (Str(s), Int(i)) | (Int(i), Str(s)) if i > 0 => Ok(Self::from(s.repeat(i as usize))),
765 (Str(_), Int(_)) | (Int(_), Str(_)) => Ok(Null),
768 (Num(n), r) => Self::from_dec_str(&n) * r,
769 (l, Num(n)) => l * Self::from_dec_str(&n),
770 (Obj(mut l), Obj(r)) => {
771 obj_merge(&mut l, r);
772 Ok(Obj(l))
773 }
774 (l, r) => Err(Error::math(l, ops::Math::Mul, r)),
775 }
776 }
777}
778
779fn split<'a>(s: &'a str, sep: &'a str) -> Box<dyn Iterator<Item = String> + 'a> {
781 if s.is_empty() {
782 Box::new(core::iter::empty())
783 } else if sep.is_empty() {
784 Box::new(s.chars().map(|s| s.to_string()))
788 } else {
789 Box::new(s.split(sep).map(|s| s.to_string()))
790 }
791}
792
793impl core::ops::Div for Val {
794 type Output = ValR;
795 fn div(self, rhs: Self) -> Self::Output {
796 use Val::{Float, Int, Num, Str};
797 match (self, rhs) {
798 (Int(x), Int(y)) => Ok(Float(x as f64 / y as f64)),
799 (Float(f), Int(i)) => Ok(Float(f / i as f64)),
800 (Int(i), Float(f)) => Ok(Float(i as f64 / f)),
801 (Float(x), Float(y)) => Ok(Float(x / y)),
802 (Num(n), r) => Self::from_dec_str(&n) / r,
803 (l, Num(n)) => l / Self::from_dec_str(&n),
804 (Str(x), Str(y)) => Ok(split(&x, &y).map(Val::from).collect()),
805 (l, r) => Err(Error::math(l, ops::Math::Div, r)),
806 }
807 }
808}
809
810impl core::ops::Rem for Val {
811 type Output = ValR;
812 fn rem(self, rhs: Self) -> Self::Output {
813 use Val::{Float, Int, Num};
814 match (self, rhs) {
815 (Int(x), Int(y)) if y != 0 => Ok(Int(x % y)),
816 (Float(f), Int(i)) => Ok(Float(f % i as f64)),
817 (Int(i), Float(f)) => Ok(Float(i as f64 % f)),
818 (Float(x), Float(y)) => Ok(Float(x % y)),
819 (Num(n), r) => Self::from_dec_str(&n) % r,
820 (l, Num(n)) => l % Self::from_dec_str(&n),
821 (l, r) => Err(Error::math(l, ops::Math::Rem, r)),
822 }
823 }
824}
825
826impl core::ops::Neg for Val {
827 type Output = ValR;
828 fn neg(self) -> Self::Output {
829 use Val::*;
830 match self {
831 Int(x) => Ok(Int(-x)),
832 Float(x) => Ok(Float(-x)),
833 Num(n) => -Self::from_dec_str(&n),
834 x => Err(Error::typ(x, Type::Num.as_str())),
835 }
836 }
837}
838
839impl PartialEq for Val {
840 fn eq(&self, other: &Self) -> bool {
841 match (self, other) {
842 (Self::Null, Self::Null) => true,
843 (Self::Bool(x), Self::Bool(y)) => x == y,
844 (Self::Int(x), Self::Int(y)) => x == y,
845 (Self::Int(i), Self::Float(f)) | (Self::Float(f), Self::Int(i)) => {
846 float_eq(*i as f64, *f)
847 }
848 (Self::Float(x), Self::Float(y)) => float_eq(*x, *y),
849 (Self::Num(x), Self::Num(y)) if Rc::ptr_eq(x, y) => true,
850 (Self::Num(n), y) => &Self::from_dec_str(n) == y,
851 (x, Self::Num(n)) => x == &Self::from_dec_str(n),
852 (Self::Str(x), Self::Str(y)) => x == y,
853 (Self::Arr(x), Self::Arr(y)) => x == y,
854 (Self::Obj(x), Self::Obj(y)) => x == y,
855 _ => false,
856 }
857 }
858}
859
860impl Eq for Val {}
861
862impl PartialOrd for Val {
863 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
864 Some(self.cmp(other))
865 }
866}
867
868impl Ord for Val {
869 fn cmp(&self, other: &Self) -> Ordering {
870 use Ordering::{Equal, Greater, Less};
871 match (self, other) {
872 (Self::Null, Self::Null) => Equal,
873 (Self::Bool(x), Self::Bool(y)) => x.cmp(y),
874 (Self::Int(x), Self::Int(y)) => x.cmp(y),
875 (Self::Int(i), Self::Float(f)) => float_cmp(*i as f64, *f),
876 (Self::Float(f), Self::Int(i)) => float_cmp(*f, *i as f64),
877 (Self::Float(x), Self::Float(y)) => float_cmp(*x, *y),
878 (Self::Num(x), Self::Num(y)) if Rc::ptr_eq(x, y) => Equal,
879 (Self::Num(n), y) => Self::from_dec_str(n).cmp(y),
880 (x, Self::Num(n)) => x.cmp(&Self::from_dec_str(n)),
881 (Self::Str(x), Self::Str(y)) => x.cmp(y),
882 (Self::Arr(x), Self::Arr(y)) => x.cmp(y),
883 (Self::Obj(x), Self::Obj(y)) => match (x.len(), y.len()) {
884 (0, 0) => Equal,
885 (0, _) => Less,
886 (_, 0) => Greater,
887 _ => {
888 let mut l: Vec<_> = x.iter().collect();
889 let mut r: Vec<_> = y.iter().collect();
890 l.sort_by_key(|(k, _v)| *k);
891 r.sort_by_key(|(k, _v)| *k);
892 let kl = l.iter().map(|(k, _v)| k);
894 let kr = r.iter().map(|(k, _v)| k);
895 let vl = l.iter().map(|(_k, v)| v);
896 let vr = r.iter().map(|(_k, v)| v);
897 kl.cmp(kr).then_with(|| vl.cmp(vr))
898 }
899 },
900
901 (Self::Null, _) => Less,
903 (_, Self::Null) => Greater,
904 (Self::Bool(_), _) => Less,
906 (_, Self::Bool(_)) => Greater,
907 (Self::Int(_) | Self::Float(_), _) => Less,
909 (_, Self::Int(_) | Self::Float(_)) => Greater,
910 (Self::Str(_), _) => Less,
912 (_, Self::Str(_)) => Greater,
913 (Self::Arr(_), _) => Less,
914 (_, Self::Arr(_)) => Greater,
915 }
916 }
917}
918
919fn float_eq(left: f64, right: f64) -> bool {
920 float_cmp(left, right) == Ordering::Equal
921}
922
923fn float_cmp(left: f64, right: f64) -> Ordering {
924 if left == 0. && right == 0. {
925 Ordering::Equal
927 } else if left.is_nan() {
928 Ordering::Less
933 } else if right.is_nan() {
934 Ordering::Greater
935 } else {
936 f64::total_cmp(&left, &right)
937 }
938}
939
940pub fn fmt_str(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
942 write!(f, "\"")?;
943 for s in s.split_inclusive(|c| c < ' ' || c == '\\' || c == '"') {
944 let mut chars = s.chars();
946 let last = chars.next_back();
947 let init = chars.as_str();
948
949 match last {
950 Some(last @ ('\t' | '\n' | '\r' | '\\' | '"')) => {
951 write!(f, "{init}{}", last.escape_default())
952 }
953 Some(last) if last < ' ' => write!(f, "{init}\\u{:04x}", last as u8),
954 _ => write!(f, "{s}"),
955 }?;
956 }
957 write!(f, "\"")
958}
959
960impl fmt::Display for Val {
961 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
962 match self {
963 Self::Null => write!(f, "null"),
964 Self::Bool(b) => write!(f, "{b}"),
965 Self::Int(i) => write!(f, "{i}"),
966 Self::Float(x) if x.is_finite() => write!(f, "{x:?}"),
967 Self::Float(_) => write!(f, "null"),
968 Self::Num(n) => write!(f, "{n}"),
969 Self::Str(s) => fmt_str(f, s),
970 Self::Arr(a) => {
971 write!(f, "[")?;
972 let mut iter = a.iter();
973 if let Some(first) = iter.next() {
974 write!(f, "{first}")?;
975 };
976 iter.try_for_each(|x| write!(f, ",{x}"))?;
977 write!(f, "]")
978 }
979 Self::Obj(o) => {
980 write!(f, "{{")?;
981 let mut iter = o.iter().map(|(k, v)| (Val::Str(k.clone()), v));
982 if let Some((k, v)) = iter.next() {
983 write!(f, "{k}:{v}")?;
984 }
985 iter.try_for_each(|(k, v)| write!(f, ",{k}:{v}"))?;
986 write!(f, "}}")
987 }
988 }
989 }
990}