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::new(core::iter::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 ("keys_unsorted", v(0), |_, cv| {
375 box_once_err(cv.1.keys_unsorted().map(|v| Val::Arr(v.into())))
376 }),
377 ("contains", v(1), |_, cv| {
378 unary(cv, |x, y| Ok(Val::from(x.contains(&y))))
379 }),
380 ("has", v(1), |_, cv| {
381 unary(cv, |v, k| v.has(&k).map(Val::from))
382 }),
383 ("indices", v(1), |_, cv| {
384 let to_int = |i: usize| Val::Int(i.try_into().unwrap());
385 unary(cv, move |x, v| {
386 x.indices(&v).map(|idxs| idxs.map(to_int).collect())
387 })
388 }),
389 ])
390}
391
392#[cfg(feature = "parse")]
393fn from_json(s: &str) -> ValR {
395 use hifijson::token::Lex;
396 let mut lexer = hifijson::SliceLexer::new(s.as_bytes());
397 lexer
398 .exactly_one(Val::parse)
399 .map_err(|e| Error::str(format_args!("cannot parse {s} as JSON: {e}")))
400}
401
402#[cfg(feature = "parse")]
403fn parse_fun() -> Filter<RunPtr<Val>> {
404 ("fromjson", v(0), |_, cv| {
405 box_once_err(cv.1.as_str().and_then(|s| from_json(s)))
406 })
407}
408
409fn skip_take(from: usize, until: usize) -> (usize, usize) {
410 (from, until.saturating_sub(from))
411}
412
413fn abs_bound(i: Option<isize>, len: usize, default: usize) -> usize {
416 i.map_or(default, |i| core::cmp::min(wrap(i, len).unwrap_or(0), len))
417}
418
419fn abs_index(i: isize, len: usize) -> Option<usize> {
421 wrap(i, len).filter(|i| *i < len)
422}
423
424fn wrap(i: isize, len: usize) -> Option<usize> {
425 if i >= 0 {
426 Some(i as usize)
427 } else if len < -i as usize {
428 None
429 } else {
430 Some(len - (-i as usize))
431 }
432}
433
434#[test]
435fn wrap_test() {
436 let len = 4;
437 assert_eq!(wrap(0, len), Some(0));
438 assert_eq!(wrap(8, len), Some(8));
439 assert_eq!(wrap(-1, len), Some(3));
440 assert_eq!(wrap(-4, len), Some(0));
441 assert_eq!(wrap(-8, len), None);
442}
443
444impl Val {
445 pub fn obj(m: Map<Rc<String>, Self>) -> Self {
447 Self::Obj(m.into())
448 }
449
450 fn as_int(&self) -> Result<isize, Error> {
452 match self {
453 Self::Int(i) => Ok(*i),
454 _ => Err(Error::typ(self.clone(), Type::Int.as_str())),
455 }
456 }
457
458 fn as_float(&self) -> Result<f64, Error> {
461 match self {
462 Self::Int(n) => Ok(*n as f64),
463 Self::Float(n) => Ok(*n),
464 Self::Num(n) => n
465 .parse()
466 .or(Err(Error::typ(self.clone(), Type::Float.as_str()))),
467 _ => Err(Error::typ(self.clone(), Type::Float.as_str())),
468 }
469 }
470
471 fn into_str(self) -> Result<Rc<String>, Error> {
473 match self {
474 Self::Str(s) => Ok(s),
475 _ => Err(Error::typ(self, Type::Str.as_str())),
476 }
477 }
478
479 #[cfg(feature = "parse")]
480 fn as_str(&self) -> Result<&Rc<String>, Error> {
482 match self {
483 Self::Str(s) => Ok(s),
484 _ => Err(Error::typ(self.clone(), Type::Str.as_str())),
485 }
486 }
487
488 fn into_arr(self) -> Result<Rc<Vec<Self>>, Error> {
490 match self {
491 Self::Arr(a) => Ok(a),
492 _ => Err(Error::typ(self, Type::Arr.as_str())),
493 }
494 }
495
496 fn from_dec_str(n: &str) -> Self {
498 n.parse().map_or(Self::Null, Self::Float)
499 }
500
501 fn has(&self, key: &Self) -> Result<bool, Error> {
505 match (self, key) {
506 (Self::Arr(a), Self::Int(i)) if *i >= 0 => Ok((*i as usize) < a.len()),
507 (Self::Obj(o), Self::Str(s)) => Ok(o.contains_key(&**s)),
508 _ => Err(Error::index(self.clone(), key.clone())),
509 }
510 }
511
512 fn keys_unsorted(&self) -> Result<Vec<Self>, Error> {
516 match self {
517 Self::Arr(a) => Ok((0..a.len() as isize).map(Self::Int).collect()),
518 Self::Obj(o) => Ok(o.keys().map(|k| Self::Str(Rc::clone(k))).collect()),
519 _ => Err(Error::typ(self.clone(), Type::Iter.as_str())),
520 }
521 }
522
523 fn contains(&self, other: &Self) -> bool {
530 match (self, other) {
531 (Self::Str(l), Self::Str(r)) => l.contains(&**r),
532 (Self::Arr(l), Self::Arr(r)) => r.iter().all(|r| l.iter().any(|l| l.contains(r))),
533 (Self::Obj(l), Self::Obj(r)) => r
534 .iter()
535 .all(|(k, r)| l.get(k).map_or(false, |l| l.contains(r))),
536 _ => self == other,
537 }
538 }
539
540 #[cfg(feature = "hifijson")]
546 pub fn parse(token: Token, lexer: &mut impl LexAlloc) -> Result<Self, hifijson::Error> {
547 use hifijson::{token, Error};
548 match token {
549 Token::Null => Ok(Self::Null),
550 Token::True => Ok(Self::Bool(true)),
551 Token::False => Ok(Self::Bool(false)),
552 Token::DigitOrMinus => {
553 let (num, parts) = lexer.num_string()?;
554 if parts.dot.is_none() && parts.exp.is_none() {
556 if let Ok(i) = num.parse() {
558 return Ok(Self::Int(i));
559 }
560 }
561 Ok(Self::Num(Rc::new(num.to_string())))
562 }
563 Token::Quote => Ok(Self::from(lexer.str_string()?.to_string())),
564 Token::LSquare => Ok(Self::Arr({
565 let mut arr = Vec::new();
566 lexer.seq(Token::RSquare, |token, lexer| {
567 arr.push(Self::parse(token, lexer)?);
568 Ok::<_, hifijson::Error>(())
569 })?;
570 arr.into()
571 })),
572 Token::LCurly => Ok(Self::obj({
573 let mut obj = Map::default();
574 lexer.seq(Token::RCurly, |token, lexer| {
575 let key =
576 lexer.str_colon(token, |lexer| lexer.str_string().map_err(Error::Str))?;
577
578 let token = lexer.ws_token().ok_or(token::Expect::Value)?;
579 let value = Self::parse(token, lexer)?;
580 obj.insert(Rc::new(key.to_string()), value);
581 Ok::<_, Error>(())
582 })?;
583 obj
584 })),
585 _ => Err(token::Expect::Value)?,
586 }
587 }
588}
589
590#[cfg(feature = "serde_json")]
591impl From<serde_json::Value> for Val {
592 fn from(v: serde_json::Value) -> Self {
593 use serde_json::Value::*;
594 match v {
595 Null => Self::Null,
596 Bool(b) => Self::Bool(b),
597 Number(n) => n
598 .to_string()
599 .parse()
600 .map_or_else(|_| Self::Num(Rc::new(n.to_string())), Self::Int),
601 String(s) => Self::from(s),
602 Array(a) => a.into_iter().map(Self::from).collect(),
603 Object(o) => Self::obj(o.into_iter().map(|(k, v)| (Rc::new(k), v.into())).collect()),
604 }
605 }
606}
607
608#[cfg(feature = "serde_json")]
609impl From<Val> for serde_json::Value {
610 fn from(v: Val) -> Self {
611 use core::str::FromStr;
612 use serde_json::Value::*;
613 match v {
614 Val::Null => Null,
615 Val::Bool(b) => Bool(b),
616 Val::Int(i) => Number(i.into()),
617 Val::Float(f) => serde_json::Number::from_f64(f).map_or(Null, Number),
618 Val::Num(n) => Number(serde_json::Number::from_str(&n).unwrap()),
619 Val::Str(s) => String((*s).clone()),
620 Val::Arr(a) => Array(a.iter().map(|x| x.clone().into()).collect()),
621 Val::Obj(o) => Object(
622 o.iter()
623 .map(|(k, v)| ((**k).clone(), v.clone().into()))
624 .collect(),
625 ),
626 }
627 }
628}
629
630impl From<bool> for Val {
631 fn from(b: bool) -> Self {
632 Self::Bool(b)
633 }
634}
635
636impl From<isize> for Val {
637 fn from(i: isize) -> Self {
638 Self::Int(i)
639 }
640}
641
642impl From<f64> for Val {
643 fn from(f: f64) -> Self {
644 Self::Float(f)
645 }
646}
647
648impl From<String> for Val {
649 fn from(s: String) -> Self {
650 Self::Str(Rc::new(s))
651 }
652}
653
654impl FromIterator<Self> for Val {
655 fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
656 Self::Arr(Rc::new(iter.into_iter().collect()))
657 }
658}
659
660impl core::ops::Add for Val {
661 type Output = ValR;
662 fn add(self, rhs: Self) -> Self::Output {
663 use Val::*;
664 match (self, rhs) {
665 (Null, x) | (x, Null) => Ok(x),
667 (Int(x), Int(y)) => Ok(Int(x + y)),
668 (Int(i), Float(f)) | (Float(f), Int(i)) => Ok(Float(f + i as f64)),
669 (Float(x), Float(y)) => Ok(Float(x + y)),
670 (Num(n), r) => Self::from_dec_str(&n) + r,
671 (l, Num(n)) => l + Self::from_dec_str(&n),
672 (Str(mut l), Str(r)) => {
673 Rc::make_mut(&mut l).push_str(&r);
674 Ok(Str(l))
675 }
676 (Arr(mut l), Arr(r)) => {
677 Rc::make_mut(&mut l).extend(r.iter().cloned());
679 Ok(Arr(l))
680 }
681 (Obj(mut l), Obj(r)) => {
682 Rc::make_mut(&mut l).extend(r.iter().map(|(k, v)| (k.clone(), v.clone())));
683 Ok(Obj(l))
684 }
685 (l, r) => Err(Error::math(l, ops::Math::Add, r)),
686 }
687 }
688}
689
690impl core::ops::Sub for Val {
691 type Output = ValR;
692 fn sub(self, rhs: Self) -> Self::Output {
693 use Val::*;
694 match (self, rhs) {
695 (Int(x), Int(y)) => Ok(Int(x - y)),
696 (Float(f), Int(i)) => Ok(Float(f - i as f64)),
697 (Int(i), Float(f)) => Ok(Float(i as f64 - f)),
698 (Float(x), Float(y)) => Ok(Float(x - y)),
699 (Num(n), r) => Self::from_dec_str(&n) - r,
700 (l, Num(n)) => l - Self::from_dec_str(&n),
701 (Arr(mut l), Arr(r)) => {
702 let r = r.iter().collect::<alloc::collections::BTreeSet<_>>();
703 Rc::make_mut(&mut l).retain(|x| !r.contains(x));
704 Ok(Arr(l))
705 }
706 (l, r) => Err(Error::math(l, ops::Math::Sub, r)),
707 }
708 }
709}
710
711fn obj_merge(l: &mut Rc<Map<Rc<String>, Val>>, r: Rc<Map<Rc<String>, Val>>) {
712 let l = Rc::make_mut(l);
713 let r = rc_unwrap_or_clone(r).into_iter();
714 r.for_each(|(k, v)| match (l.get_mut(&k), v) {
715 (Some(Val::Obj(l)), Val::Obj(r)) => obj_merge(l, r),
716 (Some(l), r) => *l = r,
717 (None, r) => {
718 l.insert(k, r);
719 }
720 });
721}
722
723impl core::ops::Mul for Val {
724 type Output = ValR;
725 fn mul(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)) | (Int(i), Float(f)) => Ok(Float(f * i as f64)),
730 (Float(x), Float(y)) => Ok(Float(x * y)),
731 (Str(s), Int(i)) | (Int(i), Str(s)) if i > 0 => Ok(Self::from(s.repeat(i as usize))),
732 (Str(_), Int(_)) | (Int(_), Str(_)) => Ok(Null),
735 (Num(n), r) => Self::from_dec_str(&n) * r,
736 (l, Num(n)) => l * Self::from_dec_str(&n),
737 (Obj(mut l), Obj(r)) => {
738 obj_merge(&mut l, r);
739 Ok(Obj(l))
740 }
741 (l, r) => Err(Error::math(l, ops::Math::Mul, r)),
742 }
743 }
744}
745
746fn split<'a>(s: &'a str, sep: &'a str) -> Box<dyn Iterator<Item = String> + 'a> {
748 if s.is_empty() {
749 Box::new(core::iter::empty())
750 } else if sep.is_empty() {
751 Box::new(s.chars().map(|s| s.to_string()))
755 } else {
756 Box::new(s.split(sep).map(|s| s.to_string()))
757 }
758}
759
760impl core::ops::Div for Val {
761 type Output = ValR;
762 fn div(self, rhs: Self) -> Self::Output {
763 use Val::{Float, Int, Num, Str};
764 match (self, rhs) {
765 (Int(x), Int(y)) => Ok(Float(x as f64 / y as f64)),
766 (Float(f), Int(i)) => Ok(Float(f / i as f64)),
767 (Int(i), Float(f)) => Ok(Float(i as f64 / f)),
768 (Float(x), Float(y)) => Ok(Float(x / y)),
769 (Num(n), r) => Self::from_dec_str(&n) / r,
770 (l, Num(n)) => l / Self::from_dec_str(&n),
771 (Str(x), Str(y)) => Ok(split(&x, &y).map(Val::from).collect()),
772 (l, r) => Err(Error::math(l, ops::Math::Div, r)),
773 }
774 }
775}
776
777impl core::ops::Rem for Val {
778 type Output = ValR;
779 fn rem(self, rhs: Self) -> Self::Output {
780 use Val::{Float, Int, Num};
781 match (self, rhs) {
782 (Int(x), Int(y)) if y != 0 => Ok(Int(x % y)),
783 (Float(f), Int(i)) => Ok(Float(f % i as f64)),
784 (Int(i), Float(f)) => Ok(Float(i as f64 % f)),
785 (Float(x), Float(y)) => Ok(Float(x % y)),
786 (Num(n), r) => Self::from_dec_str(&n) % r,
787 (l, Num(n)) => l % Self::from_dec_str(&n),
788 (l, r) => Err(Error::math(l, ops::Math::Rem, r)),
789 }
790 }
791}
792
793impl core::ops::Neg for Val {
794 type Output = ValR;
795 fn neg(self) -> Self::Output {
796 use Val::*;
797 match self {
798 Int(x) => Ok(Int(-x)),
799 Float(x) => Ok(Float(-x)),
800 Num(n) => -Self::from_dec_str(&n),
801 x => Err(Error::typ(x, Type::Num.as_str())),
802 }
803 }
804}
805
806impl PartialEq for Val {
807 fn eq(&self, other: &Self) -> bool {
808 match (self, other) {
809 (Self::Null, Self::Null) => true,
810 (Self::Bool(x), Self::Bool(y)) => x == y,
811 (Self::Int(x), Self::Int(y)) => x == y,
812 (Self::Int(i), Self::Float(f)) | (Self::Float(f), Self::Int(i)) => {
813 float_eq(*i as f64, *f)
814 }
815 (Self::Float(x), Self::Float(y)) => float_eq(*x, *y),
816 (Self::Num(x), Self::Num(y)) if Rc::ptr_eq(x, y) => true,
817 (Self::Num(n), y) => &Self::from_dec_str(n) == y,
818 (x, Self::Num(n)) => x == &Self::from_dec_str(n),
819 (Self::Str(x), Self::Str(y)) => x == y,
820 (Self::Arr(x), Self::Arr(y)) => x == y,
821 (Self::Obj(x), Self::Obj(y)) => x == y,
822 _ => false,
823 }
824 }
825}
826
827impl Eq for Val {}
828
829impl PartialOrd for Val {
830 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
831 Some(self.cmp(other))
832 }
833}
834
835impl Ord for Val {
836 fn cmp(&self, other: &Self) -> Ordering {
837 use Ordering::{Equal, Greater, Less};
838 match (self, other) {
839 (Self::Null, Self::Null) => Equal,
840 (Self::Bool(x), Self::Bool(y)) => x.cmp(y),
841 (Self::Int(x), Self::Int(y)) => x.cmp(y),
842 (Self::Int(i), Self::Float(f)) => float_cmp(*i as f64, *f),
843 (Self::Float(f), Self::Int(i)) => float_cmp(*f, *i as f64),
844 (Self::Float(x), Self::Float(y)) => float_cmp(*x, *y),
845 (Self::Num(x), Self::Num(y)) if Rc::ptr_eq(x, y) => Equal,
846 (Self::Num(n), y) => Self::from_dec_str(n).cmp(y),
847 (x, Self::Num(n)) => x.cmp(&Self::from_dec_str(n)),
848 (Self::Str(x), Self::Str(y)) => x.cmp(y),
849 (Self::Arr(x), Self::Arr(y)) => x.cmp(y),
850 (Self::Obj(x), Self::Obj(y)) => match (x.len(), y.len()) {
851 (0, 0) => Equal,
852 (0, _) => Less,
853 (_, 0) => Greater,
854 _ => {
855 let mut l: Vec<_> = x.iter().collect();
856 let mut r: Vec<_> = y.iter().collect();
857 l.sort_by_key(|(k, _v)| *k);
858 r.sort_by_key(|(k, _v)| *k);
859 let kl = l.iter().map(|(k, _v)| k);
861 let kr = r.iter().map(|(k, _v)| k);
862 let vl = l.iter().map(|(_k, v)| v);
863 let vr = r.iter().map(|(_k, v)| v);
864 kl.cmp(kr).then_with(|| vl.cmp(vr))
865 }
866 },
867
868 (Self::Null, _) => Less,
870 (_, Self::Null) => Greater,
871 (Self::Bool(_), _) => Less,
873 (_, Self::Bool(_)) => Greater,
874 (Self::Int(_) | Self::Float(_), _) => Less,
876 (_, Self::Int(_) | Self::Float(_)) => Greater,
877 (Self::Str(_), _) => Less,
879 (_, Self::Str(_)) => Greater,
880 (Self::Arr(_), _) => Less,
881 (_, Self::Arr(_)) => Greater,
882 }
883 }
884}
885
886fn float_eq(left: f64, right: f64) -> bool {
887 float_cmp(left, right) == Ordering::Equal
888}
889
890fn float_cmp(left: f64, right: f64) -> Ordering {
891 if left == 0. && right == 0. {
892 Ordering::Equal
894 } else if left.is_nan() {
895 Ordering::Less
900 } else if right.is_nan() {
901 Ordering::Greater
902 } else {
903 f64::total_cmp(&left, &right)
904 }
905}
906
907pub fn fmt_str(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
909 write!(f, "\"")?;
910 for s in s.split_inclusive(|c| c < ' ' || c == '\\' || c == '"') {
911 let mut chars = s.chars();
913 let last = chars.next_back();
914 let init = chars.as_str();
915
916 match last {
917 Some(last @ ('\t' | '\n' | '\r' | '\\' | '"')) => {
918 write!(f, "{init}{}", last.escape_default())
919 }
920 Some(last) if last < ' ' => write!(f, "{init}\\u{:04x}", last as u8),
921 _ => write!(f, "{s}"),
922 }?;
923 }
924 write!(f, "\"")
925}
926
927impl fmt::Display for Val {
928 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
929 match self {
930 Self::Null => write!(f, "null"),
931 Self::Bool(b) => write!(f, "{b}"),
932 Self::Int(i) => write!(f, "{i}"),
933 Self::Float(x) if x.is_finite() => write!(f, "{x:?}"),
934 Self::Float(_) => write!(f, "null"),
935 Self::Num(n) => write!(f, "{n}"),
936 Self::Str(s) => fmt_str(f, s),
937 Self::Arr(a) => {
938 write!(f, "[")?;
939 let mut iter = a.iter();
940 if let Some(first) = iter.next() {
941 write!(f, "{first}")?;
942 };
943 iter.try_for_each(|x| write!(f, ",{x}"))?;
944 write!(f, "]")
945 }
946 Self::Obj(o) => {
947 write!(f, "{{")?;
948 let mut iter = o.iter().map(|(k, v)| (Val::Str(k.clone()), v));
949 if let Some((k, v)) = iter.next() {
950 write!(f, "{k}:{v}")?;
951 }
952 iter.try_for_each(|(k, v)| write!(f, ",{k}:{v}"))?;
953 write!(f, "}}")
954 }
955 }
956 }
957}