couchbase 1.0.1

The official Couchbase Rust SDK
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
 *
 *  * Copyright (c) 2025 Couchbase, Inc.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *    http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

//! Specifications for sub-document mutation operations.
//!
//! Use [`MutateInSpec`] to build individual mutation specs, then pass a `Vec<MutateInSpec>` to
//! [`Collection::mutate_in`](crate::collection::Collection).
//!
//! # Example
//!
//! ```rust,no_run
//! use couchbase::subdoc::mutate_in_specs::MutateInSpec;
//!
//! # async fn example(collection: couchbase::collection::Collection) -> couchbase::error::Result<()> {
//! let specs = vec![
//!     MutateInSpec::upsert("name", "Alice", None)?,
//!     MutateInSpec::array_append("tags", &["rust", "sdk"], None)?,
//!     MutateInSpec::increment("login_count", 1, None)?,
//!     MutateInSpec::remove("temp_field", None),
//! ];
//!
//! let result = collection.mutate_in("doc-id", &specs, None).await?;
//! println!("New CAS: {}", result.cas());
//! # Ok(())
//! # }
//! ```

use crate::error;
use crate::error::Error;
use crate::subdoc::macros::MUTATE_IN_MACROS;
use couchbase_core::memdx::subdoc::MutateInOpType::{
    ArrayAddUnique, ArrayInsert, ArrayPushFirst, ArrayPushLast, Counter, Delete, DeleteDoc,
    DictAdd, DictSet, Replace, SetDoc,
};
use couchbase_core::memdx::subdoc::{MutateInOp, SubdocOp, SubdocOpFlag};
use serde::Serialize;

/// A sub-document mutation specification, used with
/// [`Collection::mutate_in`](crate::collection::Collection).
///
/// Create specs using the static constructors such as [`insert`](MutateInSpec::insert),
/// [`upsert`](MutateInSpec::upsert), [`replace`](MutateInSpec::replace),
/// [`remove`](MutateInSpec::remove), [`array_append`](MutateInSpec::array_append),
/// [`array_prepend`](MutateInSpec::array_prepend), [`array_insert`](MutateInSpec::array_insert),
/// [`array_add_unique`](MutateInSpec::array_add_unique),
/// [`increment`](MutateInSpec::increment), and [`decrement`](MutateInSpec::decrement).
///
/// # Example
///
/// ```rust
/// use couchbase::subdoc::mutate_in_specs::MutateInSpec;
///
/// let specs = vec![
///     MutateInSpec::upsert("name", "Alice", None).unwrap(),
///     MutateInSpec::remove("temp", None),
/// ];
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct MutateInSpec {
    /// The type of mutation operation.
    pub op: MutateInOpType,
    /// The JSON path to mutate.
    pub path: String,
    /// The serialized value to write (empty for remove operations).
    pub value: Vec<u8>,
    /// Whether to create intermediate path elements if they don't exist.
    pub create_path: bool,
    /// Whether this operation targets an extended attribute (xattr).
    pub is_xattr: bool,
    /// Whether server-side macro expansion should be applied.
    pub expand_macros: bool,
}

impl SubdocOp for MutateInSpec {
    fn is_xattr_op(&self) -> bool {
        self.is_xattr
    }
}

/// The type of a sub-document mutation operation.
#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum MutateInOpType {
    /// Insert a value at the path (fails if the path already exists).
    Insert,
    /// Set the value at the path (creates or overwrites).
    Upsert,
    /// Replace the value at the path (fails if the path does not exist).
    Replace,
    /// Remove the value at the path.
    Remove,
    /// Append value(s) to an array at the path.
    ArrayAppend,
    /// Prepend value(s) to an array at the path.
    ArrayPrepend,
    /// Insert a value at a specific array index.
    ArrayInsert,
    /// Add a value to an array only if it doesn't already exist.
    ArrayAddUnique,
    /// Increment or decrement a numeric value at the path.
    Counter,
}

/// Options for a [`MutateInSpec::insert`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct InsertSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl InsertSpecOptions {
    /// Creates a new `InsertSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::upsert`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct UpsertSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl UpsertSpecOptions {
    /// Creates a new `UpsertSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::replace`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ReplaceSpecOptions {
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl ReplaceSpecOptions {
    /// Creates a new `ReplaceSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }
}

/// Options for a [`MutateInSpec::remove`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct RemoveSpecOptions {
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl RemoveSpecOptions {
    /// Creates a new `RemoveSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }
}

/// Options for a [`MutateInSpec::array_append`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ArrayAppendSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl ArrayAppendSpecOptions {
    /// Creates a new `ArrayAppendSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::array_prepend`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ArrayPrependSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl ArrayPrependSpecOptions {
    /// Creates a new `ArrayPrependSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::array_insert`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ArrayInsertSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl ArrayInsertSpecOptions {
    /// Creates a new `ArrayInsertSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::array_add_unique`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ArrayAddUniqueSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl ArrayAddUniqueSpecOptions {
    /// Creates a new `ArrayAddUniqueSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::increment`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct IncrementSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl IncrementSpecOptions {
    /// Creates a new `IncrementSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

/// Options for a [`MutateInSpec::decrement`] operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct DecrementSpecOptions {
    /// If `true`, create intermediate path elements if they don't exist.
    pub create_path: Option<bool>,
    /// If `true`, target an extended attribute (xattr) instead of the document body.
    pub is_xattr: Option<bool>,
}

impl DecrementSpecOptions {
    /// Creates a new `DecrementSpecOptions` with default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets whether this operation targets an extended attribute (xattr).
    pub fn xattr(mut self, is_xattr: bool) -> Self {
        self.is_xattr = Some(is_xattr);
        self
    }

    /// Sets whether to create intermediate path elements.
    pub fn create_path(mut self, create_path: bool) -> Self {
        self.create_path = Some(create_path);
        self
    }
}

impl MutateInSpec {
    /// Creates an `insert` mutation spec that inserts a value at the given path.
    ///
    /// Fails if the path already exists. The value is serialized to JSON via `serde`.
    pub fn insert<V: Serialize>(
        path: impl Into<String>,
        value: V,
        opts: impl Into<Option<InsertSpecOptions>>,
    ) -> error::Result<Self> {
        Ok(Self::insert_raw(
            path,
            serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?,
            opts,
        ))
    }

    /// Creates an `insert` mutation spec with pre-encoded raw bytes.
    pub fn insert_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<InsertSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();
        let is_macro = Self::check_is_macro(&value);

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(is_macro),
            expand_macros: is_macro,
            op: MutateInOpType::Insert,
            path: path.into(),
            value,
        }
    }

    /// Creates an `upsert` mutation spec that sets the value at the given path,
    /// creating or overwriting it.
    pub fn upsert<V: Serialize>(
        path: impl Into<String>,
        value: V,
        opts: impl Into<Option<UpsertSpecOptions>>,
    ) -> error::Result<Self> {
        Ok(Self::upsert_raw(
            path,
            serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?,
            opts,
        ))
    }

    /// Creates an `upsert` mutation spec with pre-encoded raw bytes.
    pub fn upsert_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<UpsertSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();
        let is_macro = Self::check_is_macro(&value);

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(is_macro),
            expand_macros: is_macro,
            op: MutateInOpType::Upsert,
            path: path.into(),
            value,
        }
    }

    /// Creates a `replace` mutation spec that replaces the value at the given path.
    ///
    /// Fails if the path does not exist.
    pub fn replace<V: Serialize>(
        path: impl Into<String>,
        value: V,
        opts: impl Into<Option<ReplaceSpecOptions>>,
    ) -> error::Result<Self> {
        Ok(Self::replace_raw(
            path,
            serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?,
            opts,
        ))
    }

    /// Creates a `replace` mutation spec with pre-encoded raw bytes.
    pub fn replace_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<ReplaceSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();
        let is_macro = Self::check_is_macro(&value);

        Self {
            create_path: false,
            is_xattr: opts.is_xattr.unwrap_or(is_macro),
            expand_macros: is_macro,
            op: MutateInOpType::Replace,
            path: path.into(),
            value,
        }
    }

    /// Creates a `remove` mutation spec that removes the value at the given path.
    pub fn remove(path: impl Into<String>, opts: impl Into<Option<RemoveSpecOptions>>) -> Self {
        let opts = opts.into().unwrap_or_default();

        Self {
            create_path: false,
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::Remove,
            path: path.into(),
            value: Vec::new(),
        }
    }

    /// Appends one or more values to the end of an array at the given path.
    pub fn array_append<V: Serialize>(
        path: impl Into<String>,
        value: &[V],
        opts: impl Into<Option<ArrayAppendSpecOptions>>,
    ) -> error::Result<Self> {
        let mut value = serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?;
        if !value.is_empty() {
            value.remove(0);
            if !value.is_empty() {
                value.remove(value.len() - 1);
            }
        }

        Ok(Self::array_append_raw(path, value, opts))
    }

    /// Appends raw bytes to the end of an array at the given path.
    pub fn array_append_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<ArrayAppendSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::ArrayAppend,
            path: path.into(),
            value,
        }
    }

    /// Prepends one or more values to the beginning of an array at the given path.
    pub fn array_prepend<V: Serialize>(
        path: impl Into<String>,
        value: &[V],
        opts: impl Into<Option<ArrayPrependSpecOptions>>,
    ) -> error::Result<Self> {
        let mut value = serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?;
        if !value.is_empty() {
            value.remove(0);
            if !value.is_empty() {
                value.remove(value.len() - 1);
            }
        }

        Ok(Self::array_prepend_raw(path, value, opts))
    }

    /// Prepends raw bytes to the beginning of an array at the given path.
    pub fn array_prepend_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<ArrayPrependSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::ArrayPrepend,
            path: path.into(),
            value,
        }
    }

    /// Inserts one or more values at a specific position in an array.
    ///
    /// The path must include the array index, e.g. `"tags[2]"`.
    pub fn array_insert<V: Serialize>(
        path: impl Into<String>,
        value: &[V],
        opts: impl Into<Option<ArrayInsertSpecOptions>>,
    ) -> error::Result<Self> {
        let mut value = serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?;
        if !value.is_empty() {
            value.remove(0);
            if !value.is_empty() {
                value.remove(value.len() - 1);
            }
        }

        Ok(Self::array_insert_raw(path, value, opts))
    }

    /// Inserts raw bytes at a specific position in an array.
    pub fn array_insert_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<ArrayInsertSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::ArrayInsert,
            path: path.into(),
            value,
        }
    }

    /// Adds a unique value to an array at the given path (no-op if it already exists).
    pub fn array_add_unique<V: Serialize>(
        path: impl Into<String>,
        value: V,
        opts: impl Into<Option<ArrayAddUniqueSpecOptions>>,
    ) -> error::Result<Self> {
        Ok(Self::array_add_unique_raw(
            path,
            serde_json::to_vec(&value).map_err(Error::encoding_failure_from_serde)?,
            opts,
        ))
    }

    /// Adds raw bytes as a unique value to an array at the given path.
    pub fn array_add_unique_raw(
        path: impl Into<String>,
        value: Vec<u8>,
        opts: impl Into<Option<ArrayAddUniqueSpecOptions>>,
    ) -> Self {
        let opts = opts.into().unwrap_or_default();
        let is_macro = Self::check_is_macro(&value);

        Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(is_macro),
            expand_macros: is_macro,
            op: MutateInOpType::ArrayAddUnique,
            path: path.into(),
            value,
        }
    }

    /// Increments a numeric value at the given path by the specified positive delta.
    ///
    /// Returns an error if `delta` is negative.
    pub fn increment(
        path: impl Into<String>,
        delta: i64,
        opts: impl Into<Option<IncrementSpecOptions>>,
    ) -> error::Result<Self> {
        if delta < 0 {
            return Err(Error::invalid_argument(
                "delta",
                "only positive delta allowed in subdoc increment",
            ));
        }

        let value = serde_json::to_vec(&delta).map_err(Error::encoding_failure_from_serde)?;
        let opts = opts.into().unwrap_or_default();

        Ok(Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::Counter,
            path: path.into(),
            value,
        })
    }

    /// Decrements a numeric value at the given path by the specified positive delta.
    ///
    /// Returns an error if `delta` is negative.
    pub fn decrement(
        path: impl Into<String>,
        delta: i64,
        opts: impl Into<Option<DecrementSpecOptions>>,
    ) -> error::Result<Self> {
        if delta < 0 {
            return Err(Error::invalid_argument(
                "delta",
                "only positive delta allowed in subdoc increment",
            ));
        }

        let delta = -delta;
        let value = serde_json::to_vec(&delta).map_err(Error::encoding_failure_from_serde)?;
        let opts = opts.into().unwrap_or_default();

        Ok(Self {
            create_path: opts.create_path.unwrap_or(false),
            is_xattr: opts.is_xattr.unwrap_or(false),
            expand_macros: false,
            op: MutateInOpType::Counter,
            path: path.into(),
            value,
        })
    }

    fn check_is_macro(value: &[u8]) -> bool {
        MUTATE_IN_MACROS.contains(&value)
    }
}

impl<'a> TryFrom<&'a MutateInSpec> for MutateInOp<'a> {
    type Error = Error;

    fn try_from(value: &'a MutateInSpec) -> Result<Self, Self::Error> {
        let op_type = match value.op {
            MutateInOpType::Insert => {
                if value.path.is_empty() {
                    return Err(Error::invalid_argument(
                        "path",
                        "path cannot be empty for insert operation",
                    ));
                }

                DictAdd
            }
            MutateInOpType::Upsert => {
                if value.path.is_empty() {
                    return Err(Error::invalid_argument(
                        "path",
                        "path cannot be empty for upsert operation",
                    ));
                }

                DictSet
            }
            MutateInOpType::Replace => {
                if value.path.is_empty() {
                    SetDoc
                } else {
                    Replace
                }
            }
            MutateInOpType::Remove => {
                if value.path.is_empty() {
                    DeleteDoc
                } else {
                    Delete
                }
            }
            MutateInOpType::ArrayAppend => ArrayPushLast,
            MutateInOpType::ArrayPrepend => ArrayPushFirst,
            MutateInOpType::ArrayInsert => ArrayInsert,
            MutateInOpType::ArrayAddUnique => ArrayAddUnique,
            MutateInOpType::Counter => Counter,
        };

        let mut op_flags = SubdocOpFlag::empty();

        if value.is_xattr {
            op_flags |= SubdocOpFlag::XATTR_PATH;
        }
        if value.create_path {
            op_flags |= SubdocOpFlag::MKDIR_P;
        }
        if value.expand_macros {
            op_flags |= SubdocOpFlag::EXPAND_MACROS;
        }

        Ok(MutateInOp::new(op_type, value.path.as_bytes(), &value.value).flags(op_flags))
    }
}