esexpr 0.2.5

ESExpr serialization format and related utilities.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use alloc::borrow::{Cow, ToOwned};
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::fmt::{Debug, Formatter};

use half::f16;
use num_bigint::{BigInt, BigUint};

use crate::cowstr::CowStr;
use crate::{DecodeError, ESExprCodec, ESExprEncodedEq, ESExprTag, ESExprTagSet};

/// Representation of an `ESExpr` value.
/// Must be a constructor, bool, int, string, float32, float64, {int,uint}{8,16,32,64} or null.
#[derive(Debug, Clone)]
pub enum ESExpr<'a> {
	/// A constructor expression.
	/// Can contain positional and keyword arguments.
	Constructor(ESExprConstructor<'a>),

	/// A bool value.
	Bool(bool),

	/// An integer value.
	Int(Cow<'a, BigInt>),

	/// A string value.
	Str(CowStr<'a>),

	/// A float16 value.
	Float16(f16),

	/// A float32 value.
	Float32(f32),

	/// A float64 value.
	Float64(f64),

	/// An array of 8-bit values
	Array8(Cow<'a, [u8]>),

	/// An array of 16-bit values
	Array16(Cow<'a, [u16]>),

	/// An array of 32-bit values
	Array32(Cow<'a, [u32]>),

	/// An array of 64-bit values
	Array64(Cow<'a, [u64]>),

	/// An array of 128-bit values
	Array128(Cow<'a, [u128]>),

	/// A Null value.
	Null(Cow<'a, BigUint>),
}

impl<'a> ESExpr<'a> {
	/// Create a new constructor expression
	pub fn constructor<'b, S: Into<CowStr<'b>>, A: Into<ConstructorArgs<'a>>, K: Into<KeywordArgs<'a>>>(
		name: S,
		args: A,
		kwargs: K,
	) -> Self
	where
		'b: 'a,
	{
		ESExpr::Constructor(ESExprConstructor {
			name: name.into(),
			args: args.into(),
			kwargs: kwargs.into(),
		})
	}

	/// Get the tag of an expression.
	#[must_use]
	pub fn tag<'b>(&'b self) -> ESExprTag<'b> {
		match self {
			ESExpr::Constructor(ESExprConstructor { name, .. }) => ESExprTag::Constructor(name.as_borrowed()),
			ESExpr::Bool(_) => ESExprTag::Bool,
			ESExpr::Int(_) => ESExprTag::Int,
			ESExpr::Str(_) => ESExprTag::Str,
			ESExpr::Float16(_) => ESExprTag::Float16,
			ESExpr::Float32(_) => ESExprTag::Float32,
			ESExpr::Float64(_) => ESExprTag::Float64,
			ESExpr::Array8(_) => ESExprTag::Array8,
			ESExpr::Array16(_) => ESExprTag::Array16,
			ESExpr::Array32(_) => ESExprTag::Array32,
			ESExpr::Array64(_) => ESExprTag::Array64,
			ESExpr::Array128(_) => ESExprTag::Array128,
			ESExpr::Null(_) => ESExprTag::Null,
		}
	}

	/// Performs a deep clone of the value.
	#[must_use]
	pub fn as_owned(&self) -> ESExpr<'static> {
		match self {
			ESExpr::Constructor(constructor) => ESExpr::Constructor(constructor.as_owned()),
			&ESExpr::Bool(b) => ESExpr::Bool(b),
			ESExpr::Int(i) => ESExpr::Int(Cow::Owned(i.as_ref().clone())),
			ESExpr::Str(s) => ESExpr::Str(s.as_owned_cowstr()),
			&ESExpr::Float16(f) => ESExpr::Float16(f),
			&ESExpr::Float32(f) => ESExpr::Float32(f),
			&ESExpr::Float64(f) => ESExpr::Float64(f),
			ESExpr::Array8(b) => ESExpr::Array8(Cow::Owned(b.as_ref().to_owned())),
			ESExpr::Array16(b) => ESExpr::Array16(Cow::Owned(b.as_ref().to_owned())),
			ESExpr::Array32(b) => ESExpr::Array32(Cow::Owned(b.as_ref().to_owned())),
			ESExpr::Array64(b) => ESExpr::Array64(Cow::Owned(b.as_ref().to_owned())),
			ESExpr::Array128(b) => ESExpr::Array128(Cow::Owned(b.as_ref().to_owned())),
			ESExpr::Null(level) => ESExpr::Null(Cow::Owned(level.as_ref().clone())),
		}
	}

	/// Ensures that any borrowed values are converted to owned values.
	#[must_use]
	pub fn into_owned(self) -> ESExpr<'static> {
		match self {
			ESExpr::Constructor(constructor) => ESExpr::Constructor(constructor.into_owned()),
			ESExpr::Bool(b) => ESExpr::Bool(b),
			ESExpr::Int(i) => ESExpr::Int(Cow::Owned(i.into_owned())),
			ESExpr::Str(s) => ESExpr::Str(s.into_owned_cowstr()),
			ESExpr::Float16(f) => ESExpr::Float16(f),
			ESExpr::Float32(f) => ESExpr::Float32(f),
			ESExpr::Float64(f) => ESExpr::Float64(f),
			ESExpr::Array8(b) => ESExpr::Array8(Cow::Owned(b.into_owned())),
			ESExpr::Array16(b) => ESExpr::Array16(Cow::Owned(b.into_owned())),
			ESExpr::Array32(b) => ESExpr::Array32(Cow::Owned(b.into_owned())),
			ESExpr::Array64(b) => ESExpr::Array64(Cow::Owned(b.into_owned())),
			ESExpr::Array128(b) => ESExpr::Array128(Cow::Owned(b.into_owned())),
			ESExpr::Null(level) => ESExpr::Null(Cow::Owned(level.into_owned())),
		}
	}

	/// Creates an `ESExpr` value from a reference without making a deep copy.
	#[must_use]
	pub fn as_borrowed<'b>(&'b self) -> ESExpr<'b>
	where
		'a: 'b,
	{
		match self {
			ESExpr::Constructor(constructor) => ESExpr::Constructor(constructor.as_borrowed()),
			&ESExpr::Bool(b) => ESExpr::Bool(b),
			ESExpr::Int(i) => ESExpr::Int(Cow::Borrowed(i.as_ref())),
			ESExpr::Str(s) => ESExpr::Str(s.as_borrowed()),
			&ESExpr::Float16(f) => ESExpr::Float16(f),
			&ESExpr::Float32(f) => ESExpr::Float32(f),
			&ESExpr::Float64(f) => ESExpr::Float64(f),
			ESExpr::Array8(b) => ESExpr::Array8(Cow::Borrowed(b.as_ref())),
			ESExpr::Array16(b) => ESExpr::Array16(Cow::Borrowed(b.as_ref())),
			ESExpr::Array32(b) => ESExpr::Array32(Cow::Borrowed(b.as_ref())),
			ESExpr::Array64(b) => ESExpr::Array64(Cow::Borrowed(b.as_ref())),
			ESExpr::Array128(b) => ESExpr::Array128(Cow::Borrowed(b.as_ref())),
			ESExpr::Null(level) => ESExpr::Null(Cow::Borrowed(level.as_ref())),
		}
	}
}

impl<'a, 'b> PartialEq<ESExpr<'b>> for ESExpr<'a> {
	fn eq(&self, other: &ESExpr<'b>) -> bool {
		match self {
			ESExpr::Constructor(ESExprConstructor {
				name: name1,
				args: args1,
				kwargs: kwargs1,
			}) => {
				let ESExpr::Constructor(ESExprConstructor {
					name: name2,
					args: args2,
					kwargs: kwargs2,
				}) = other
				else {
					return false;
				};

				name1 == name2 &&
					args1 == args2 && kwargs1
					.iter()
					.zip(kwargs2.iter())
					.all(|((k1, v1), (k2, v2))| k1 == k2 && v1 == v2)
			},
			&ESExpr::Bool(b1) => matches!(other, &ESExpr::Bool(b2) if b1 == b2),
			ESExpr::Int(i1) => matches!(other, ESExpr::Int(i2) if i1 == i2),
			ESExpr::Str(s1) => matches!(other, ESExpr::Str(s2) if s1 == s2),
			&ESExpr::Float16(f1) => matches!(other, &ESExpr::Float16(f2) if f1.to_bits() == f2.to_bits()),
			&ESExpr::Float32(f1) => matches!(other, &ESExpr::Float32(f2) if f1.to_bits() == f2.to_bits()),
			&ESExpr::Float64(f1) => matches!(other, &ESExpr::Float64(f2) if f1.to_bits() == f2.to_bits()),
			ESExpr::Array8(a1) => matches!(other, ESExpr::Array8(a2) if a1 == a2),
			ESExpr::Array16(a1) => matches!(other, ESExpr::Array16(a2) if a1 == a2),
			ESExpr::Array32(a1) => matches!(other, ESExpr::Array32(a2) if a1 == a2),
			ESExpr::Array64(a1) => matches!(other, ESExpr::Array64(a2) if a1 == a2),
			ESExpr::Array128(a1) => matches!(other, ESExpr::Array128(a2) if a1 == a2),
			ESExpr::Null(l1) => matches!(other, ESExpr::Null(l2) if l1 == l2),
		}
	}
}

impl<'a> Eq for ESExpr<'a> {}

impl<'a> ESExprEncodedEq for ESExpr<'a> {
	fn is_encoded_eq(&self, other: &Self) -> bool {
		self == other
	}
}


impl<'a> ESExprCodec<'a> for ESExpr<'a> {
	const TAGS: ESExprTagSet = ESExprTagSet::All;

	fn encode_esexpr(&'a self) -> ESExpr<'a> {
		self.as_borrowed()
	}

	fn decode_esexpr(expr: ESExpr<'a>) -> Result<Self, DecodeError> {
		Ok(expr)
	}
}

/// A wrapper for a `ESExpr<'static>`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ESExprStatic(ESExpr<'static>);

impl ESExprStatic {
	/// Create an `ESExprStatic`
	pub fn new(e: ESExpr<'static>) -> Self {
		ESExprStatic(e)
	}
	
	/// Get the underlying `ESExpr`.
	pub fn into_inner(self) -> ESExpr<'static> {
		self.0
	}
}

impl ESExprEncodedEq for ESExprStatic {
	fn is_encoded_eq(&self, other: &Self) -> bool {
		self == other
	}
}

impl<'a> ESExprCodec<'a> for ESExprStatic {
	const TAGS: ESExprTagSet = ESExprTagSet::All;

	fn encode_esexpr(&'a self) -> ESExpr<'a> {
		self.0.as_borrowed()
	}

	fn decode_esexpr(expr: ESExpr<'a>) -> Result<Self, DecodeError> {
		Ok(ESExprStatic(expr.into_owned()))
	}
}

/// A `ESExpr` constructor expression
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ESExprConstructor<'a> {
	/// The name of the constructor.
	pub name: CowStr<'a>,

	/// The constructor's positional arguments.
	pub args: ConstructorArgs<'a>,

	/// The constructor's keyword arguments.
	pub kwargs: KeywordArgs<'a>,
}

impl<'a> ESExprConstructor<'a> {
	fn into_owned(self) -> ESExprConstructor<'static> {
		ESExprConstructor {
			name: self.name.as_owned_cowstr(),
			args: self.args.into_owned(),
			kwargs: self.kwargs.into_owned(),
		}
	}

	fn as_owned(&self) -> ESExprConstructor<'static> {
		ESExprConstructor {
			name: self.name.as_owned_cowstr(),
			args: self.args.as_owned(),
			kwargs: self.kwargs.as_owned(),
		}
	}

	fn as_borrowed<'b>(&'b self) -> ESExprConstructor<'b> {
		ESExprConstructor {
			name: self.name.as_borrowed(),
			args: self.args.as_borrowed(),
			kwargs: self.kwargs.as_borrowed(),
		}
	}
}

/// Positional arguments of an `ESExprConstructor`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstructorArgs<'a> {
	args: ConstructorArgsInner<'a>,
}

impl<'a> ConstructorArgs<'a> {
	/// Iterates over constructor arguments.
	pub fn iter(&'a self) -> ConstructorArgsIntoIter<'a> {
		self.as_borrowed().into_iter()
	}

	/// The number of constructor arguments.
	#[must_use]
	pub fn len(&self) -> usize {
		self.args.as_slice().len()
	}

	/// Gets whether there are any arguments.
	#[must_use]
	pub fn is_empty(&self) -> bool {
		self.args.as_slice().is_empty()
	}

	fn into_owned(self) -> ConstructorArgs<'static> {
		ConstructorArgs {
			args: ConstructorArgsInner::Owned(self.into_iter().map(ESExpr::into_owned).collect()),
		}
	}

	fn as_owned(&self) -> ConstructorArgs<'static> {
		ConstructorArgs {
			args: ConstructorArgsInner::Owned(self.into_iter().map(ESExpr::into_owned).collect()),
		}
	}

	fn as_borrowed<'b>(&'b self) -> ConstructorArgs<'b> {
		ConstructorArgs {
			args: match &self.args {
				ConstructorArgsInner::Owned(args) => ConstructorArgsInner::Borrowed(args),
				ConstructorArgsInner::Borrowed(args) => ConstructorArgsInner::Borrowed(args),
			},
		}
	}
}

impl<'a> From<Vec<ESExpr<'a>>> for ConstructorArgs<'a> {
	fn from(args: Vec<ESExpr<'a>>) -> Self {
		ConstructorArgs {
			args: ConstructorArgsInner::Owned(args),
		}
	}
}

impl<'a> From<&'a [ESExpr<'a>]> for ConstructorArgs<'a> {
	fn from(args: &'a [ESExpr<'a>]) -> Self {
		ConstructorArgs {
			args: ConstructorArgsInner::Borrowed(args),
		}
	}
}

impl<'a, const N: usize> From<[ESExpr<'a>; N]> for ConstructorArgs<'a> {
	fn from(args: [ESExpr<'a>; N]) -> Self {
		ConstructorArgs {
			args: ConstructorArgsInner::Owned(args.to_vec()),
		}
	}
}

impl<'a> From<ConstructorArgs<'a>> for Vec<ESExpr<'a>> {
	fn from(args: ConstructorArgs<'a>) -> Self {
		match args.args {
			ConstructorArgsInner::Owned(args) => args,
			ConstructorArgsInner::Borrowed(args) => args.iter().map(ESExpr::as_borrowed).collect(),
		}
	}
}

impl<'a> IntoIterator for ConstructorArgs<'a> {
	type Item = ESExpr<'a>;
	type IntoIter = ConstructorArgsIntoIter<'a>;

	fn into_iter(self) -> Self::IntoIter {
		ConstructorArgsIntoIter {
			inner_iter: match self.args {
				ConstructorArgsInner::Owned(args) => ConstructorArgsInnerIntoIter::Owned(args.into_iter()),
				ConstructorArgsInner::Borrowed(args) => ConstructorArgsInnerIntoIter::Borrowed(args.iter()),
			},
		}
	}
}

impl<'a> IntoIterator for &'a ConstructorArgs<'a> {
	type Item = ESExpr<'a>;
	type IntoIter = ConstructorArgsIntoIter<'a>;

	fn into_iter(self) -> Self::IntoIter {
		self.as_borrowed().into_iter()
	}
}

/// `ESExpr` Constructor arguments
/// Used instead of `Cow` because of lifetime variance.
#[derive(Clone)]
enum ConstructorArgsInner<'a> {
	/// Constructor arguments owned by this constructor.
	Owned(Vec<ESExpr<'a>>),

	/// Constructor arguments borrowed from another constructor.
	Borrowed(&'a [ESExpr<'a>]),
}

impl<'a> ConstructorArgsInner<'a> {
	fn as_slice(&self) -> &[ESExpr<'a>] {
		match self {
			ConstructorArgsInner::Owned(args) => args,
			ConstructorArgsInner::Borrowed(args) => args,
		}
	}
}

impl<'a> Debug for ConstructorArgsInner<'a> {
	fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
		self.as_slice().fmt(f)
	}
}

impl<'a> PartialEq for ConstructorArgsInner<'a> {
	fn eq(&self, other: &Self) -> bool {
		self.as_slice() == other.as_slice()
	}
}

impl<'a> Eq for ConstructorArgsInner<'a> {}

#[must_use]
pub struct ConstructorArgsIntoIter<'a> {
	inner_iter: ConstructorArgsInnerIntoIter<'a>,
}

impl<'a> Iterator for ConstructorArgsIntoIter<'a> {
	type Item = ESExpr<'a>;

	fn next(&mut self) -> Option<Self::Item> {
		self.inner_iter.next()
	}
}

enum ConstructorArgsInnerIntoIter<'a> {
	Owned(alloc::vec::IntoIter<ESExpr<'a>>),
	Borrowed(alloc::slice::Iter<'a, ESExpr<'a>>),
}

impl<'a> Iterator for ConstructorArgsInnerIntoIter<'a> {
	type Item = ESExpr<'a>;

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			ConstructorArgsInnerIntoIter::Owned(iter) => iter.next(),
			ConstructorArgsInnerIntoIter::Borrowed(iter) => iter.next().map(ESExpr::as_borrowed),
		}
	}
}

/// `ESExpr` constructor keyword arguments
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeywordArgs<'a> {
	kwargs: KeywordArgsInner<'a>,
}

impl<'a> KeywordArgs<'a> {
	/// Iterate keyword arguments
	pub fn iter(&'a self) -> KeywordArgsIntoIter<'a> {
		self.into_iter()
	}

	/// The number of keyword arguments.
	#[must_use]
	pub fn len(&self) -> usize {
		self.kwargs.as_map().len()
	}

	/// Checks if there are any keyword arguments
	#[must_use]
	pub fn is_empty(&self) -> bool {
		self.kwargs.as_map().is_empty()
	}

	fn into_owned(self) -> KeywordArgs<'static> {
		KeywordArgs {
			kwargs: KeywordArgsInner::Owned(
				self.into_iter()
					.map(|(k, v)| (k.into_owned_cowstr(), v.into_owned()))
					.collect(),
			),
		}
	}

	fn as_owned(&self) -> KeywordArgs<'static> {
		KeywordArgs {
			kwargs: KeywordArgsInner::Owned(
				self.iter()
					.map(|(k, v)| (k.into_owned_cowstr(), v.into_owned()))
					.collect(),
			),
		}
	}

	fn as_borrowed<'b>(&'b self) -> KeywordArgs<'b> {
		KeywordArgs {
			kwargs: match &self.kwargs {
				KeywordArgsInner::Owned(kwargs) => KeywordArgsInner::Borrowed(kwargs),
				KeywordArgsInner::Borrowed(kwargs) => KeywordArgsInner::Borrowed(kwargs),
			},
		}
	}
}

impl<'a> From<BTreeMap<CowStr<'a>, ESExpr<'a>>> for KeywordArgs<'a> {
	fn from(kwargs: BTreeMap<CowStr<'a>, ESExpr<'a>>) -> Self {
		KeywordArgs {
			kwargs: KeywordArgsInner::Owned(kwargs),
		}
	}
}

impl<'a> From<&'a BTreeMap<CowStr<'a>, ESExpr<'a>>> for KeywordArgs<'a> {
	fn from(kwargs: &'a BTreeMap<CowStr<'a>, ESExpr<'a>>) -> Self {
		KeywordArgs {
			kwargs: KeywordArgsInner::Borrowed(kwargs),
		}
	}
}

impl<'a, const N: usize> From<[(CowStr<'a>, ESExpr<'a>); N]> for KeywordArgs<'a> {
	fn from(value: [(CowStr<'a>, ESExpr<'a>); N]) -> Self {
		KeywordArgs {
			kwargs: KeywordArgsInner::Owned(BTreeMap::from(value)),
		}
	}
}

impl<'a> From<KeywordArgs<'a>> for BTreeMap<CowStr<'a>, ESExpr<'a>> {
	fn from(kwargs: KeywordArgs<'a>) -> Self {
		match kwargs.kwargs {
			KeywordArgsInner::Owned(kwargs) => kwargs,
			KeywordArgsInner::Borrowed(kwargs) => {
				kwargs.iter().map(|(k, v)| (k.as_owned_cowstr(), v.clone())).collect()
			},
		}
	}
}

impl<'a> IntoIterator for KeywordArgs<'a> {
	type Item = (CowStr<'a>, ESExpr<'a>);
	type IntoIter = KeywordArgsIntoIter<'a>;

	fn into_iter(self) -> Self::IntoIter {
		KeywordArgsIntoIter {
			inner_iter: match self.kwargs {
				KeywordArgsInner::Owned(kwargs) => KeywordArgsInnerIntoIter::Owned(kwargs.into_iter()),
				KeywordArgsInner::Borrowed(kwargs) => KeywordArgsInnerIntoIter::Borrowed(kwargs.iter()),
			},
		}
	}
}

impl<'a> IntoIterator for &'a KeywordArgs<'a> {
	type Item = (CowStr<'a>, ESExpr<'a>);
	type IntoIter = KeywordArgsIntoIter<'a>;

	fn into_iter(self) -> Self::IntoIter {
		self.as_borrowed().into_iter()
	}
}

#[derive(Clone)]
pub enum KeywordArgsInner<'a> {
	Owned(BTreeMap<CowStr<'a>, ESExpr<'a>>),
	Borrowed(&'a BTreeMap<CowStr<'a>, ESExpr<'a>>),
}

impl<'a> KeywordArgsInner<'a> {
	fn as_map(&self) -> &BTreeMap<CowStr<'a>, ESExpr<'a>> {
		match self {
			KeywordArgsInner::Owned(kwargs) => kwargs,
			KeywordArgsInner::Borrowed(kwargs) => kwargs,
		}
	}
}

impl<'a> Debug for KeywordArgsInner<'a> {
	fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
		self.as_map().fmt(f)
	}
}

impl<'a> PartialEq for KeywordArgsInner<'a> {
	fn eq(&self, other: &Self) -> bool {
		self.as_map() == other.as_map()
	}
}

impl<'a> Eq for KeywordArgsInner<'a> {}

#[must_use]
pub struct KeywordArgsIntoIter<'a> {
	inner_iter: KeywordArgsInnerIntoIter<'a>,
}

impl<'a> Iterator for KeywordArgsIntoIter<'a> {
	type Item = (CowStr<'a>, ESExpr<'a>);

	fn next(&mut self) -> Option<Self::Item> {
		self.inner_iter.next()
	}
}

enum KeywordArgsInnerIntoIter<'a> {
	Owned(alloc::collections::btree_map::IntoIter<CowStr<'a>, ESExpr<'a>>),
	Borrowed(alloc::collections::btree_map::Iter<'a, CowStr<'a>, ESExpr<'a>>),
}

impl<'a> Iterator for KeywordArgsInnerIntoIter<'a> {
	type Item = (CowStr<'a>, ESExpr<'a>);

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			KeywordArgsInnerIntoIter::Owned(iter) => iter.next(),
			KeywordArgsInnerIntoIter::Borrowed(iter) => {
				iter.next().map(|(k, v)| (k.as_borrowed(), ESExpr::as_borrowed(v)))
			},
		}
	}
}