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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
use super::{TableColumnDefinition, TableColumnId, TableRow, TableRowValue};
use crate::data_sources::SelectedDataSource;
use crate::formatting::Formatting;
use crate::front_matter_schemas::{FrontMatterSchemaEntry, FrontMatterValueSchema};
use crate::notebooks::{
    front_matter::{FrontMatter, FrontMatterValue},
    Cell, Label,
};
use crate::timestamps::TimeRange;
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use typed_builder::TypedBuilder;

/// An operation is the representation for a mutation to be performed to a notebook.
///
/// Operations are intended to be atomic (they should either be performed in their entirety or not
/// at all), while also capturing the intent of the user.
///
/// For more information, please see RFC 8:
///   https://www.notion.so/fiberplane/RFC-8-Notebook-Operations-f9d18676d0d9437d81de30faa219deb4
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(clippy::large_enum_variant)]
pub enum Operation {
    // Cell-level operations.
    MoveCells(MoveCellsOperation),
    ReplaceCells(ReplaceCellsOperation),

    // Text-level operation.
    ReplaceText(ReplaceTextOperation),

    // Time-range operation.
    UpdateNotebookTimeRange(UpdateNotebookTimeRangeOperation),

    /// **Deprecated:** Please use `ReplaceText` with `cell_id == TITLE_CELL_ID` instead.
    UpdateNotebookTitle(UpdateNotebookTitleOperation),

    // Data source selection.
    SetSelectedDataSource(SetSelectedDataSourceOperation),

    // Label operations.
    AddLabel(AddLabelOperation),
    ReplaceLabel(ReplaceLabelOperation),
    RemoveLabel(RemoveLabelOperation),

    // Front matter operations.
    ClearFrontMatter(ClearFrontMatterOperation),
    InsertFrontMatterSchema(InsertFrontMatterSchemaOperation),
    UpdateFrontMatterSchema(UpdateFrontMatterSchemaOperation),
    MoveFrontMatterSchema(MoveFrontMatterSchemaOperation),
    RemoveFrontMatterSchema(RemoveFrontMatterSchemaOperation),
    /// **Deprecated:** Full front matter updates should be avoided, granular update operations are
    /// better for conflict handling.
    UpdateFrontMatter(UpdateFrontMatterOperation),

    // Table cell operations.
    // TODO: We'll probably want Move operations for columns and rows later as well.
    InsertTableColumn(InsertTableColumnOperation),
    RemoveTableColumn(RemoveTableColumnOperation),
    UpdateTableColumnDefinition(UpdateTableColumnDefinitionOperation),
    InsertTableRow(InsertTableRowOperation),
    RemoveTableRow(RemoveTableRowOperation),
}

/// Moves one or more cells.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct MoveCellsOperation {
    /// IDs of all the cells to be moved.
    ///
    /// These must be adjacent and given in the order they appear in the notebook.
    pub cell_ids: Vec<String>,

    /// Index the cells will be moved from. This is the index of the first cell before the move.
    pub from_index: u32,

    /// Index the cells will be moved to. This is the index of the first cell after the move.
    pub to_index: u32,
}

/// Replaces one or more cells at once.
///
/// Note: This operation is relatively coarse and can be (ab)used to perform
/// `ReplaceText` operations as well. In order to preserve intent as much as
/// possible, please use `ReplaceText` where possible.
///
/// Note: This operation may not be used to move cells, other than the necessary
/// corrections in cell indices that account for newly inserted and removed
/// cells. Attempts to move cells to other indices will cause validation to
/// fail.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct ReplaceCellsOperation {
    /// Vector of the new cells, including their new indices.
    ///
    /// Indices of the new cells must be ordered incrementally to form a single,
    /// cohesive range of cells.
    ///
    /// Note that "new" does not imply "newly inserted". If a cell with the same
    /// ID is part of the `old_cells` field, it will merely be updated. Only
    /// cells in the `new_cells` field that are not part of the `old_cells` will
    /// be newly inserted.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub new_cells: Vec<CellWithIndex>,

    /// Vector of the old cells, including their old indices.
    ///
    /// Indices of the old cells must be ordered incrementally to form a single,
    /// cohesive range of cells.
    ///
    /// Note that "old" does not imply "removed". If a cell with the same
    /// ID is part of the `new_cells` field, it will merely be updated. Only
    /// cells in the `old_cells` field that are not part of the `new_cells` will
    /// be removed.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub old_cells: Vec<CellWithIndex>,

    /// Offset at which to split the first of the old cells.
    ///
    /// In this context, splitting means that the text of the cell in the
    /// notebook is split in two at the split offset. The first part is kept,
    /// while the second part (which must match the cell's text in `old_cells`)
    /// is replaced with the text given in the first of the `new_cells`.
    ///
    /// If `None`, no cell is split.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub split_offset: Option<u32>,

    /// Offset from which to merge the remainder of the last old cell.
    ///
    /// In this context, merging means that the text of the new cell is merged
    /// from two parts. The first part comes the last of the `new_cells`, while
    /// the second part is what remains of the cell in the notebook after the
    /// merge offset.
    ///
    /// If `None`, no cells are merged.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub merge_offset: Option<u32>,

    /// Optional cells which are updated as a result of the replacing of other
    /// cells. This is intended to be used for cells that reference the
    /// `new_cells` and which now need to be updated as a result of the
    /// operation being applied to those cells.
    ///
    /// These referencing cells may also be newly inserted if they are not
    /// included in the `old_referencing_cells`.
    ///
    /// Indices of new referencing cells do not need to form a cohesive range,
    /// but they should still be ordered in ascending order.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub new_referencing_cells: Vec<CellWithIndex>,

    /// Optional cells which are updated as a result of the replacing of other
    /// cells. This is intended to be used for cells that reference the
    /// `old_cells` and which now need to be updated as a result of the
    /// operation being applied to those cells.
    ///
    /// These referencing cells may also be removed if they are not included in
    /// the `new_referencing_cells`.
    ///
    /// Indices of old referencing cells do not need to form a cohesive range,
    /// but they should still be ordered in ascending order.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub old_referencing_cells: Vec<CellWithIndex>,
}

impl ReplaceCellsOperation {
    /// Returns all the new cells, including the ones in the
    /// `new_referencing_cells` field.
    ///
    /// Note that new cells doesn't imply newly inserted, since cells that are
    /// merely updated are part of the new cells as well. See
    /// `all_newly_inserted_cells()` if that's what you're looking for.
    pub fn all_new_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.old_cells
            .iter()
            .chain(self.old_referencing_cells.iter())
    }

    /// Returns all the newly inserted cells, including the ones in the
    /// `new_referencing_cells` field.
    pub fn all_newly_inserted_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.newly_inserted_cells()
            .chain(self.newly_inserted_referencing_cells())
    }

    /// Returns all the old cells, including the ones in the
    /// `old_referencing_cells` field.
    ///
    /// Note that old cells doesn't imply removed cells, since cells that are
    /// merely updated are part of the old cells as well. See
    /// `all_old_removed_cells()` if that's what you're looking for.
    pub fn all_old_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.old_cells
            .iter()
            .chain(self.old_referencing_cells.iter())
    }

    /// Returns all the old removed cells, including the ones from the
    /// `old_referencing_cells` field.
    pub fn all_old_removed_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.old_removed_cells()
            .chain(self.old_removed_referencing_cells())
    }

    /// Returns all newly inserted cells, excluding referencing cells.
    pub fn newly_inserted_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.new_cells.iter().filter(move |new_cell| {
            !self
                .old_cells
                .iter()
                .any(|old_cell| old_cell.id() == new_cell.id())
        })
    }

    /// Returns all newly inserted referencing cells.
    pub fn newly_inserted_referencing_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.new_referencing_cells.iter().filter(move |new_cell| {
            !self
                .old_referencing_cells
                .iter()
                .any(|old_cell| old_cell.id() == new_cell.id())
        })
    }

    /// Returns all old cells that will be removed, excluding referencing cells.
    pub fn old_removed_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.old_cells.iter().filter(move |old_cell| {
            !self
                .new_cells
                .iter()
                .any(|new_cell| new_cell.id() == old_cell.id())
        })
    }

    /// Returns all old referencing cells that will be removed.
    pub fn old_removed_referencing_cells(&self) -> impl Iterator<Item = &CellWithIndex> {
        self.old_referencing_cells.iter().filter(move |old_cell| {
            !self
                .new_referencing_cells
                .iter()
                .any(|new_cell| new_cell.id() == old_cell.id())
        })
    }
}

/// Replaces the part of the content in any content type cell or the title of a graph cell.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct ReplaceTextOperation {
    /// ID of the cell whose text we're modifying.
    #[builder(setter(into))]
    pub cell_id: String,

    /// Field to update the text of.
    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub field: Option<String>,

    /// Starting offset where we will be replacing the text.
    ///
    /// Please be aware this offset refers to the position of a Unicode Scalar Value (non-surrogate
    /// codepoint) in the cell text, which may require additional effort to determine correctly.
    pub offset: u32,

    /// The new text value we're inserting.
    #[builder(default, setter(into))]
    pub new_text: String,

    /// Optional formatting that we wish to apply to the new text.
    ///
    /// Offsets in the formatting are relative to the start of the new text.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub new_formatting: Option<Formatting>,

    /// The old text that we're replacing.
    #[builder(default, setter(into))]
    pub old_text: String,

    /// Optional formatting that was applied to the old text. This should be **all** the formatting
    /// annotations that were *inside* the `old_text` before this operation was applied. However,
    /// it is at the operation's discretion whether or not to include annotations that are at the
    /// old text's boundaries.
    ///
    /// Offsets in the formatting are relative to the start of the old text.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub old_formatting: Option<Formatting>,
}

/// Updates the notebook time range.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateNotebookTimeRangeOperation {
    pub old_time_range: TimeRange,
    pub time_range: TimeRange,
}

/// Updates the notebook title.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateNotebookTitleOperation {
    #[builder(default, setter(into))]
    pub old_title: String,

    #[builder(default, setter(into))]
    pub title: String,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct SetSelectedDataSourceOperation {
    #[builder(setter(into))]
    pub provider_type: String,

    #[builder(default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub old_selected_data_source: Option<SelectedDataSource>,

    #[builder(default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub new_selected_data_source: Option<SelectedDataSource>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct CellWithIndex {
    pub cell: Cell,
    pub index: u32,
}

impl CellWithIndex {
    pub fn new(cell: Cell, index: u32) -> CellWithIndex {
        CellWithIndex { cell, index }
    }

    pub fn formatting(&self) -> Option<&Formatting> {
        self.cell.formatting()
    }

    pub fn id(&self) -> &str {
        self.cell.id()
    }

    pub fn text(&self) -> Option<&str> {
        self.cell.text()
    }
}

/// Add an label to an notebook.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct AddLabelOperation {
    /// The new label
    pub label: Label,
}

/// Replace an label in an notebook.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct ReplaceLabelOperation {
    // The previous label
    pub old_label: Label,

    // The new label
    pub new_label: Label,
}

/// Remove an label in an notebook.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct RemoveLabelOperation {
    pub label: Label,
}

/// Replaces front matter in a notebook
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateFrontMatterOperation {
    pub old_front_matter: FrontMatter,
    pub new_front_matter: FrontMatter,
}

/// Removes front matter in a notebook
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct ClearFrontMatterOperation {
    pub front_matter: FrontMatter,
}

/// Adds front matter entries in a notebook
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct InsertFrontMatterSchemaOperation {
    // NOTE: No strip_option here because the strongly typed builder makes
    // it hard to revert operations in fiberplane-ot otherwise
    /// The Front Matter Schema key that is just before the insertion point. This
    /// is solely used for consistency checks when validating the operation.
    #[builder(default, setter(into))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_of_entry_before_insertion_location: Option<String>,

    /// The Front Matter Schema key that is just after the insertion point. This
    /// is solely used for consistency checks when validating the operation.
    #[builder(default, setter(into))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_of_entry_after_insertion_location: Option<String>,

    /// The index to insert the new front matter schema into
    pub to_index: u32,

    /// The new entries to add to the front matter schema, with their new values
    pub insertions: Vec<FrontMatterSchemaRow>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FrontMatterSchemaRow {
    #[builder(setter(into))]
    pub key: String,

    #[builder(setter(into))]
    pub schema: FrontMatterValueSchema,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub value: Option<FrontMatterValue>,
}

impl From<(FrontMatterSchemaEntry, Option<Value>)> for FrontMatterSchemaRow {
    fn from((schema, value): (FrontMatterSchemaEntry, Option<Value>)) -> Self {
        Self {
            key: schema.key,
            schema: schema.schema,
            value: value.map(Into::into),
        }
    }
}

/// Changes the expected schema of a front matter key in a notebook and/or the
/// value attached to a schema
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateFrontMatterSchemaOperation {
    /// The key of the front matter schema to update.
    #[builder(setter(into))]
    pub key: String,

    /// The previous schema used for that front matter key. The old value is used
    /// to make consistency checks, as well as revert the operation.
    #[builder(setter(into))]
    pub old_schema: FrontMatterValueSchema,

    // NOTE: No strip_option here because the strongly typed builder makes
    // it hard to revert operations in fiberplane-ot otherwise
    /// The previous value for that front matter key. It is used for consistency checks,
    /// as well as making reverting operations possible.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub old_value: Option<FrontMatterValue>,

    // NOTE: No strip_option here because the strongly typed builder makes
    // it hard to revert operations in fiberplane-ot otherwise
    /// The new schema to use, if unspecified the operation will leave the schema
    /// untouched (so the operation is only being used to edit the associated value).
    ///
    /// If a new schema is specified, and the data type does _not_ match between the
    /// old and the new one, then the old value will be wiped anyway.
    #[builder(setter(into))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub new_schema: Option<FrontMatterValueSchema>,

    /// The new value to set for the front matter entry.
    ///
    /// If this attribute is `None` or `null` it can mean multiple things depending on
    /// the other attributes:
    /// - if `delete_value` is `false`, this means we want to keep the `old_value`
    ///   + it is impossible to keep the `old_value` if the schemas are incompatible. In that
    ///     case we use the `default_value` of the new schema (or nothing if there’s no default)
    /// - if `delete_value` is `true`, this means we want to wipe the value from the front
    ///   matter in all cases.
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub new_value: Option<FrontMatterValue>,

    /// Switch that controls front matter value edition alongside `new_value`, when
    /// `new_value` is None.
    #[builder(setter(strip_bool))]
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub delete_value: bool,
}

/// Moves front matter entries in a notebook
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct MoveFrontMatterSchemaOperation {
    /// The keys that will be moved in the front matter. They should be a range of
    /// consecutive front matter entries, matching the existing front matter schema
    /// at the index pointed to by `from_index`
    pub keys: Vec<String>,

    /// Index the key will be moved from. This is the index of the first front matter key before the move.
    pub from_index: u32,

    /// Index the key will be moved to. This is the index of the first front matter key after the move.
    pub to_index: u32,
}

/// Removes front matter entries in a notebook
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct RemoveFrontMatterSchemaOperation {
    /// The key of the front matter schema element lying just before the deletion range, i.e. _not_ removed.
    /// This is used to make consistency checks when validating operation.
    #[builder(default, setter(into))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_of_entry_before_deletion_range: Option<String>,

    /// The key of the front matter schema element lying just after the deletion range, i.e. _not_ removed.
    /// This is used to make consistency checks when validating operation.
    #[builder(default, setter(into))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_of_entry_after_deletion_range: Option<String>,

    /// The index to start removing elements from. This is the index of the first element that will be
    /// deleted, and should match the first element of the `deletions` array.
    pub from_index: u32,

    /// Elements that should be deleted, with their last known values
    pub deletions: Vec<FrontMatterSchemaRow>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct CellAppendText {
    #[builder(setter(into))]
    pub content: String,

    #[builder(default)]
    #[serde(default)]
    pub formatting: Formatting,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct CellReplaceText {
    /// Starting offset where we will be replacing the text.
    ///
    /// Please be aware this offset refers to the position of a Unicode Scalar Value (non-surrogate
    /// codepoint) in the cell text, which may require additional effort to determine correctly.
    pub offset: u32,

    /// The new text value we're inserting.
    #[builder(default, setter(into))]
    pub new_text: String,

    /// Optional formatting that we wish to apply to the new text.
    ///
    /// Offsets in the formatting are relative to the start of the new text.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub new_formatting: Option<Formatting>,

    /// The old text that we're replacing.
    #[builder(default, setter(into))]
    pub old_text: String,

    /// Optional formatting that was applied to the old text. This should be **all** the formatting
    /// annotations that were *inside* the `old_text` before this operation was applied. However,
    /// it is at the operation's discretion whether or not to include annotations that are at the
    /// old text's boundaries.
    ///
    /// Offsets in the formatting are relative to the start of the old text.
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub old_formatting: Option<Formatting>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct InsertTableColumnOperation {
    /// ID of the table cell.
    #[builder(setter(into))]
    pub cell_id: String,

    /// Definition for the column.
    pub column_def: TableColumnDefinition,

    /// The index at which to insert the column.
    pub index: u32,

    /// The values to insert in the column.
    ///
    /// The amount of values should match the amount of rows in the table.
    #[builder(setter(into))]
    pub values: Vec<TableRowValue>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct RemoveTableColumnOperation {
    /// ID of the table cell.
    #[builder(setter(into))]
    pub cell_id: String,

    /// Definition of the column being removed.
    pub column_def: TableColumnDefinition,

    /// The index of the column being removed.
    pub index: u32,

    /// The values that are being removed together with the column.
    ///
    /// The amount of values should match the amount of rows in the table.
    #[builder(setter(into))]
    pub values: Vec<TableRowValue>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateTableColumnDefinitionOperation {
    /// ID of the table cell.
    #[builder(setter(into))]
    pub cell_id: String,

    /// ID of the column being updated.
    pub column_id: TableColumnId,

    /// New heading text.
    #[builder(setter(into))]
    pub new_title: String,

    /// Old heading text.
    #[builder(setter(into))]
    pub old_title: String,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct InsertTableRowOperation {
    /// ID of the table cell.
    #[builder(setter(into))]
    pub cell_id: String,

    /// The row being inserted.
    pub row: TableRow,

    /// The index at which to insert the row.
    pub index: u32,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::notebooks::operations")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct RemoveTableRowOperation {
    /// ID of the table cell.
    #[builder(setter(into))]
    pub cell_id: String,

    /// The row being removed.
    pub row: TableRow,

    /// The index of the row being removed.
    pub index: u32,
}