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
use crate::data_sources::SelectedDataSource;
use crate::formatting::Formatting;
use crate::notebooks::{Cell, FrontMatter, Label};
use crate::timestamps::TimeRange;
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::*;
use serde::{Deserialize, Serialize};
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")]
pub enum Operation {
    MoveCells(MoveCellsOperation),
    ReplaceCells(ReplaceCellsOperation),
    ReplaceText(ReplaceTextOperation),
    UpdateNotebookTimeRange(UpdateNotebookTimeRangeOperation),
    UpdateNotebookTitle(UpdateNotebookTitleOperation),
    SetSelectedDataSource(SetSelectedDataSourceOperation),
    AddLabel(AddLabelOperation),
    ReplaceLabel(ReplaceLabelOperation),
    RemoveLabel(RemoveLabelOperation),
    UpdateFrontMatter(UpdateFrontMatterOperation),
    ClearFrontMatter(ClearFrontMatterOperation),
}

/// 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 cell IDs, 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 cell IDs, including the ones in the
    /// `old_referencing_cells` field.
    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 cell IDs, 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,
}

#[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>,
}