proto.rs

  1//! Handles conversions of `language` items to and from the [`rpc`] protocol.
  2
  3use crate::{diagnostic_set::DiagnosticEntry, CursorShape, Diagnostic};
  4use anyhow::{anyhow, Result};
  5use clock::ReplicaId;
  6use lsp::{DiagnosticSeverity, LanguageServerId};
  7use rpc::proto;
  8use std::{ops::Range, sync::Arc};
  9use text::*;
 10
 11pub use proto::{BufferState, Operation};
 12
 13/// Serializes a [`RopeFingerprint`] to be sent over RPC.
 14pub fn serialize_fingerprint(fingerprint: RopeFingerprint) -> String {
 15    fingerprint.to_hex()
 16}
 17
 18/// Deserializes a `[text::LineEnding]` from the RPC representation.
 19pub fn deserialize_line_ending(message: proto::LineEnding) -> text::LineEnding {
 20    match message {
 21        proto::LineEnding::Unix => text::LineEnding::Unix,
 22        proto::LineEnding::Windows => text::LineEnding::Windows,
 23    }
 24}
 25
 26/// Serializes a [`text::LineEnding`] to be sent over RPC.
 27pub fn serialize_line_ending(message: text::LineEnding) -> proto::LineEnding {
 28    match message {
 29        text::LineEnding::Unix => proto::LineEnding::Unix,
 30        text::LineEnding::Windows => proto::LineEnding::Windows,
 31    }
 32}
 33
 34/// Serializes a [`crate::Operation`] to be sent over RPC.
 35pub fn serialize_operation(operation: &crate::Operation) -> proto::Operation {
 36    proto::Operation {
 37        variant: Some(match operation {
 38            crate::Operation::Buffer(text::Operation::Edit(edit)) => {
 39                proto::operation::Variant::Edit(serialize_edit_operation(edit))
 40            }
 41
 42            crate::Operation::Buffer(text::Operation::Undo(undo)) => {
 43                proto::operation::Variant::Undo(proto::operation::Undo {
 44                    replica_id: undo.timestamp.replica_id as u32,
 45                    lamport_timestamp: undo.timestamp.value,
 46                    version: serialize_version(&undo.version),
 47                    counts: undo
 48                        .counts
 49                        .iter()
 50                        .map(|(edit_id, count)| proto::UndoCount {
 51                            replica_id: edit_id.replica_id as u32,
 52                            lamport_timestamp: edit_id.value,
 53                            count: *count,
 54                        })
 55                        .collect(),
 56                })
 57            }
 58
 59            crate::Operation::UpdateSelections {
 60                selections,
 61                line_mode,
 62                lamport_timestamp,
 63                cursor_shape,
 64            } => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
 65                replica_id: lamport_timestamp.replica_id as u32,
 66                lamport_timestamp: lamport_timestamp.value,
 67                selections: serialize_selections(selections),
 68                line_mode: *line_mode,
 69                cursor_shape: serialize_cursor_shape(cursor_shape) as i32,
 70            }),
 71
 72            crate::Operation::UpdateDiagnostics {
 73                lamport_timestamp,
 74                server_id,
 75                diagnostics,
 76            } => proto::operation::Variant::UpdateDiagnostics(proto::UpdateDiagnostics {
 77                replica_id: lamport_timestamp.replica_id as u32,
 78                lamport_timestamp: lamport_timestamp.value,
 79                server_id: server_id.0 as u64,
 80                diagnostics: serialize_diagnostics(diagnostics.iter()),
 81            }),
 82
 83            crate::Operation::UpdateCompletionTriggers {
 84                triggers,
 85                lamport_timestamp,
 86            } => proto::operation::Variant::UpdateCompletionTriggers(
 87                proto::operation::UpdateCompletionTriggers {
 88                    replica_id: lamport_timestamp.replica_id as u32,
 89                    lamport_timestamp: lamport_timestamp.value,
 90                    triggers: triggers.clone(),
 91                },
 92            ),
 93        }),
 94    }
 95}
 96
 97/// Serializes an [`EditOperation`] to be sent over RPC.
 98pub fn serialize_edit_operation(operation: &EditOperation) -> proto::operation::Edit {
 99    proto::operation::Edit {
100        replica_id: operation.timestamp.replica_id as u32,
101        lamport_timestamp: operation.timestamp.value,
102        version: serialize_version(&operation.version),
103        ranges: operation.ranges.iter().map(serialize_range).collect(),
104        new_text: operation
105            .new_text
106            .iter()
107            .map(|text| text.to_string())
108            .collect(),
109    }
110}
111
112/// Serializes an entry in the undo map to be sent over RPC.
113pub fn serialize_undo_map_entry(
114    (edit_id, counts): (&clock::Lamport, &[(clock::Lamport, u32)]),
115) -> proto::UndoMapEntry {
116    proto::UndoMapEntry {
117        replica_id: edit_id.replica_id as u32,
118        local_timestamp: edit_id.value,
119        counts: counts
120            .iter()
121            .map(|(undo_id, count)| proto::UndoCount {
122                replica_id: undo_id.replica_id as u32,
123                lamport_timestamp: undo_id.value,
124                count: *count,
125            })
126            .collect(),
127    }
128}
129
130/// Splits the given list of operations into chunks.
131pub fn split_operations(
132    mut operations: Vec<proto::Operation>,
133) -> impl Iterator<Item = Vec<proto::Operation>> {
134    #[cfg(any(test, feature = "test-support"))]
135    const CHUNK_SIZE: usize = 5;
136
137    #[cfg(not(any(test, feature = "test-support")))]
138    const CHUNK_SIZE: usize = 100;
139
140    let mut done = false;
141    std::iter::from_fn(move || {
142        if done {
143            return None;
144        }
145
146        let operations = operations
147            .drain(..std::cmp::min(CHUNK_SIZE, operations.len()))
148            .collect::<Vec<_>>();
149        if operations.is_empty() {
150            done = true;
151        }
152        Some(operations)
153    })
154}
155
156/// Serializes selections to be sent over RPC.
157pub fn serialize_selections(selections: &Arc<[Selection<Anchor>]>) -> Vec<proto::Selection> {
158    selections.iter().map(serialize_selection).collect()
159}
160
161/// Serializes a [`Selection`] to be sent over RPC.
162pub fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
163    proto::Selection {
164        id: selection.id as u64,
165        start: Some(proto::EditorAnchor {
166            anchor: Some(serialize_anchor(&selection.start)),
167            excerpt_id: 0,
168        }),
169        end: Some(proto::EditorAnchor {
170            anchor: Some(serialize_anchor(&selection.end)),
171            excerpt_id: 0,
172        }),
173        reversed: selection.reversed,
174    }
175}
176
177/// Serializes a [`CursorShape`] to be sent over RPC.
178pub fn serialize_cursor_shape(cursor_shape: &CursorShape) -> proto::CursorShape {
179    match cursor_shape {
180        CursorShape::Bar => proto::CursorShape::CursorBar,
181        CursorShape::Block => proto::CursorShape::CursorBlock,
182        CursorShape::Underscore => proto::CursorShape::CursorUnderscore,
183        CursorShape::Hollow => proto::CursorShape::CursorHollow,
184    }
185}
186
187/// Deserializes a [`CursorShape`] from the RPC representation.
188pub fn deserialize_cursor_shape(cursor_shape: proto::CursorShape) -> CursorShape {
189    match cursor_shape {
190        proto::CursorShape::CursorBar => CursorShape::Bar,
191        proto::CursorShape::CursorBlock => CursorShape::Block,
192        proto::CursorShape::CursorUnderscore => CursorShape::Underscore,
193        proto::CursorShape::CursorHollow => CursorShape::Hollow,
194    }
195}
196
197/// Serializes a list of diagnostics to be sent over RPC.
198pub fn serialize_diagnostics<'a>(
199    diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
200) -> Vec<proto::Diagnostic> {
201    diagnostics
202        .into_iter()
203        .map(|entry| proto::Diagnostic {
204            source: entry.diagnostic.source.clone(),
205            start: Some(serialize_anchor(&entry.range.start)),
206            end: Some(serialize_anchor(&entry.range.end)),
207            message: entry.diagnostic.message.clone(),
208            severity: match entry.diagnostic.severity {
209                DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
210                DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
211                DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
212                DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
213                _ => proto::diagnostic::Severity::None,
214            } as i32,
215            group_id: entry.diagnostic.group_id as u64,
216            is_primary: entry.diagnostic.is_primary,
217            is_valid: true,
218            code: entry.diagnostic.code.clone(),
219            is_disk_based: entry.diagnostic.is_disk_based,
220            is_unnecessary: entry.diagnostic.is_unnecessary,
221        })
222        .collect()
223}
224
225/// Serializes an [`Anchor`] to be sent over RPC.
226pub fn serialize_anchor(anchor: &Anchor) -> proto::Anchor {
227    proto::Anchor {
228        replica_id: anchor.timestamp.replica_id as u32,
229        timestamp: anchor.timestamp.value,
230        offset: anchor.offset as u64,
231        bias: match anchor.bias {
232            Bias::Left => proto::Bias::Left as i32,
233            Bias::Right => proto::Bias::Right as i32,
234        },
235        buffer_id: anchor.buffer_id.map(Into::into),
236    }
237}
238
239// This behavior is currently copied in the collab database, for snapshotting channel notes
240/// Deserializes an [`crate::Operation`] from the RPC representation.
241pub fn deserialize_operation(message: proto::Operation) -> Result<crate::Operation> {
242    Ok(
243        match message
244            .variant
245            .ok_or_else(|| anyhow!("missing operation variant"))?
246        {
247            proto::operation::Variant::Edit(edit) => {
248                crate::Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
249            }
250            proto::operation::Variant::Undo(undo) => {
251                crate::Operation::Buffer(text::Operation::Undo(UndoOperation {
252                    timestamp: clock::Lamport {
253                        replica_id: undo.replica_id as ReplicaId,
254                        value: undo.lamport_timestamp,
255                    },
256                    version: deserialize_version(&undo.version),
257                    counts: undo
258                        .counts
259                        .into_iter()
260                        .map(|c| {
261                            (
262                                clock::Lamport {
263                                    replica_id: c.replica_id as ReplicaId,
264                                    value: c.lamport_timestamp,
265                                },
266                                c.count,
267                            )
268                        })
269                        .collect(),
270                }))
271            }
272            proto::operation::Variant::UpdateSelections(message) => {
273                let selections = message
274                    .selections
275                    .into_iter()
276                    .filter_map(|selection| {
277                        Some(Selection {
278                            id: selection.id as usize,
279                            start: deserialize_anchor(selection.start?.anchor?)?,
280                            end: deserialize_anchor(selection.end?.anchor?)?,
281                            reversed: selection.reversed,
282                            goal: SelectionGoal::None,
283                        })
284                    })
285                    .collect::<Vec<_>>();
286
287                crate::Operation::UpdateSelections {
288                    lamport_timestamp: clock::Lamport {
289                        replica_id: message.replica_id as ReplicaId,
290                        value: message.lamport_timestamp,
291                    },
292                    selections: Arc::from(selections),
293                    line_mode: message.line_mode,
294                    cursor_shape: deserialize_cursor_shape(
295                        proto::CursorShape::from_i32(message.cursor_shape)
296                            .ok_or_else(|| anyhow!("Missing cursor shape"))?,
297                    ),
298                }
299            }
300            proto::operation::Variant::UpdateDiagnostics(message) => {
301                crate::Operation::UpdateDiagnostics {
302                    lamport_timestamp: clock::Lamport {
303                        replica_id: message.replica_id as ReplicaId,
304                        value: message.lamport_timestamp,
305                    },
306                    server_id: LanguageServerId(message.server_id as usize),
307                    diagnostics: deserialize_diagnostics(message.diagnostics),
308                }
309            }
310            proto::operation::Variant::UpdateCompletionTriggers(message) => {
311                crate::Operation::UpdateCompletionTriggers {
312                    triggers: message.triggers,
313                    lamport_timestamp: clock::Lamport {
314                        replica_id: message.replica_id as ReplicaId,
315                        value: message.lamport_timestamp,
316                    },
317                }
318            }
319        },
320    )
321}
322
323/// Deserializes an [`EditOperation`] from the RPC representation.
324pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
325    EditOperation {
326        timestamp: clock::Lamport {
327            replica_id: edit.replica_id as ReplicaId,
328            value: edit.lamport_timestamp,
329        },
330        version: deserialize_version(&edit.version),
331        ranges: edit.ranges.into_iter().map(deserialize_range).collect(),
332        new_text: edit.new_text.into_iter().map(Arc::from).collect(),
333    }
334}
335
336/// Deserializes an entry in the undo map from the RPC representation.
337pub fn deserialize_undo_map_entry(
338    entry: proto::UndoMapEntry,
339) -> (clock::Lamport, Vec<(clock::Lamport, u32)>) {
340    (
341        clock::Lamport {
342            replica_id: entry.replica_id as u16,
343            value: entry.local_timestamp,
344        },
345        entry
346            .counts
347            .into_iter()
348            .map(|undo_count| {
349                (
350                    clock::Lamport {
351                        replica_id: undo_count.replica_id as u16,
352                        value: undo_count.lamport_timestamp,
353                    },
354                    undo_count.count,
355                )
356            })
357            .collect(),
358    )
359}
360
361/// Deserializes selections from the RPC representation.
362pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
363    Arc::from(
364        selections
365            .into_iter()
366            .filter_map(deserialize_selection)
367            .collect::<Vec<_>>(),
368    )
369}
370
371/// Deserializes a [`Selection`] from the RPC representation.
372pub fn deserialize_selection(selection: proto::Selection) -> Option<Selection<Anchor>> {
373    Some(Selection {
374        id: selection.id as usize,
375        start: deserialize_anchor(selection.start?.anchor?)?,
376        end: deserialize_anchor(selection.end?.anchor?)?,
377        reversed: selection.reversed,
378        goal: SelectionGoal::None,
379    })
380}
381
382/// Deserializes a list of diagnostics from the RPC representation.
383pub fn deserialize_diagnostics(
384    diagnostics: Vec<proto::Diagnostic>,
385) -> Arc<[DiagnosticEntry<Anchor>]> {
386    diagnostics
387        .into_iter()
388        .filter_map(|diagnostic| {
389            Some(DiagnosticEntry {
390                range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
391                diagnostic: Diagnostic {
392                    source: diagnostic.source,
393                    severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
394                        proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
395                        proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
396                        proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
397                        proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
398                        proto::diagnostic::Severity::None => return None,
399                    },
400                    message: diagnostic.message,
401                    group_id: diagnostic.group_id as usize,
402                    code: diagnostic.code,
403                    is_primary: diagnostic.is_primary,
404                    is_disk_based: diagnostic.is_disk_based,
405                    is_unnecessary: diagnostic.is_unnecessary,
406                },
407            })
408        })
409        .collect()
410}
411
412/// Deserializes an [`Anchor`] from the RPC representation.
413pub fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
414    let buffer_id = if let Some(id) = anchor.buffer_id {
415        Some(BufferId::new(id).ok()?)
416    } else {
417        None
418    };
419    Some(Anchor {
420        timestamp: clock::Lamport {
421            replica_id: anchor.replica_id as ReplicaId,
422            value: anchor.timestamp,
423        },
424        offset: anchor.offset as usize,
425        bias: match proto::Bias::from_i32(anchor.bias)? {
426            proto::Bias::Left => Bias::Left,
427            proto::Bias::Right => Bias::Right,
428        },
429        buffer_id,
430    })
431}
432
433/// Returns a `[clock::Lamport`] timestamp for the given [`proto::Operation`].
434pub fn lamport_timestamp_for_operation(operation: &proto::Operation) -> Option<clock::Lamport> {
435    let replica_id;
436    let value;
437    match operation.variant.as_ref()? {
438        proto::operation::Variant::Edit(op) => {
439            replica_id = op.replica_id;
440            value = op.lamport_timestamp;
441        }
442        proto::operation::Variant::Undo(op) => {
443            replica_id = op.replica_id;
444            value = op.lamport_timestamp;
445        }
446        proto::operation::Variant::UpdateDiagnostics(op) => {
447            replica_id = op.replica_id;
448            value = op.lamport_timestamp;
449        }
450        proto::operation::Variant::UpdateSelections(op) => {
451            replica_id = op.replica_id;
452            value = op.lamport_timestamp;
453        }
454        proto::operation::Variant::UpdateCompletionTriggers(op) => {
455            replica_id = op.replica_id;
456            value = op.lamport_timestamp;
457        }
458    }
459
460    Some(clock::Lamport {
461        replica_id: replica_id as ReplicaId,
462        value,
463    })
464}
465
466/// Serializes a [`Transaction`] to be sent over RPC.
467pub fn serialize_transaction(transaction: &Transaction) -> proto::Transaction {
468    proto::Transaction {
469        id: Some(serialize_timestamp(transaction.id)),
470        edit_ids: transaction
471            .edit_ids
472            .iter()
473            .copied()
474            .map(serialize_timestamp)
475            .collect(),
476        start: serialize_version(&transaction.start),
477    }
478}
479
480/// Deserializes a [`Transaction`] from the RPC representation.
481pub fn deserialize_transaction(transaction: proto::Transaction) -> Result<Transaction> {
482    Ok(Transaction {
483        id: deserialize_timestamp(
484            transaction
485                .id
486                .ok_or_else(|| anyhow!("missing transaction id"))?,
487        ),
488        edit_ids: transaction
489            .edit_ids
490            .into_iter()
491            .map(deserialize_timestamp)
492            .collect(),
493        start: deserialize_version(&transaction.start),
494    })
495}
496
497/// Serializes a [`clock::Lamport`] timestamp to be sent over RPC.
498pub fn serialize_timestamp(timestamp: clock::Lamport) -> proto::LamportTimestamp {
499    proto::LamportTimestamp {
500        replica_id: timestamp.replica_id as u32,
501        value: timestamp.value,
502    }
503}
504
505/// Deserializes a [`clock::Lamport`] timestamp from the RPC representation.
506pub fn deserialize_timestamp(timestamp: proto::LamportTimestamp) -> clock::Lamport {
507    clock::Lamport {
508        replica_id: timestamp.replica_id as ReplicaId,
509        value: timestamp.value,
510    }
511}
512
513/// Serializes a range of [`FullOffset`]s to be sent over RPC.
514pub fn serialize_range(range: &Range<FullOffset>) -> proto::Range {
515    proto::Range {
516        start: range.start.0 as u64,
517        end: range.end.0 as u64,
518    }
519}
520
521/// Deserializes a range of [`FullOffset`]s from the RPC representation.
522pub fn deserialize_range(range: proto::Range) -> Range<FullOffset> {
523    FullOffset(range.start as usize)..FullOffset(range.end as usize)
524}
525
526/// Deserializes a clock version from the RPC representation.
527pub fn deserialize_version(message: &[proto::VectorClockEntry]) -> clock::Global {
528    let mut version = clock::Global::new();
529    for entry in message {
530        version.observe(clock::Lamport {
531            replica_id: entry.replica_id as ReplicaId,
532            value: entry.timestamp,
533        });
534    }
535    version
536}
537
538/// Serializes a clock version to be sent over RPC.
539pub fn serialize_version(version: &clock::Global) -> Vec<proto::VectorClockEntry> {
540    version
541        .iter()
542        .map(|entry| proto::VectorClockEntry {
543            replica_id: entry.replica_id as u32,
544            timestamp: entry.value,
545        })
546        .collect()
547}