Skip to content

Commit

Permalink
rope: optionalize serde dep
Browse files Browse the repository at this point in the history
  • Loading branch information
Cogitri committed Jun 28, 2019
1 parent b19ec53 commit 84bfe30
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
4 changes: 3 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/core-lib/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ regex = "1.0"
memchr = "2.0.1"

xi-trace = { path = "../trace", version = "0.1.0" }
xi-rope = { path = "../rope", version = "0.3" }
xi-rope = { path = "../rope", version = "0.3", features = ["serde"] }
xi-unicode = { path = "../unicode", version = "0.1.0" }
xi-rpc = { path = "../rpc", version = "0.2.0" }

Expand Down
6 changes: 4 additions & 2 deletions rust/rope/Cargo.toml
Expand Up @@ -10,11 +10,13 @@ edition = '2018'
[dependencies]
bytecount = "0.5"
memchr = "2.0"
serde = "1.0"
serde_derive = "1.0"
serde = { version="1.0", optional=true, features=["derive"] }
unicode-segmentation = "1.2.1"
regex = "1.0"

[dev-dependencies]
serde_test = "^1.0"
serde_json = "1.0"

[features]
default = []
28 changes: 18 additions & 10 deletions rust/rope/src/delta.rs
Expand Up @@ -697,7 +697,6 @@ mod tests {
use crate::interval::Interval;
use crate::rope::{Rope, RopeInfo};
use crate::test_helpers::find_deletions;
use serde_json;

const TEST_STR: &'static str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

Expand Down Expand Up @@ -821,15 +820,6 @@ mod tests {
assert_eq!("2367", delta.apply_to_string("0123456789"));
}

#[test]
fn delta_serde() {
let d = Delta::simple_edit(Interval::new(10, 12), Rope::from("+"), TEST_STR.len());
let ser = serde_json::to_value(d.clone()).expect("serialize failed");
eprintln!("{:?}", &ser);
let de: Delta<RopeInfo> = serde_json::from_value(ser).expect("deserialize failed");
assert_eq!(d.apply_to_string(TEST_STR), de.apply_to_string(TEST_STR));
}

#[test]
fn is_simple_delete() {
let d = Delta::simple_edit(10..12, Rope::from("+"), TEST_STR.len());
Expand Down Expand Up @@ -878,3 +868,21 @@ mod tests {
assert_eq!(Some(Rope::from("+")).as_ref(), d.as_simple_insert());
}
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use crate::rope::{Rope, RopeInfo};
use crate::{Delta, Interval};
use serde_json;

const TEST_STR: &'static str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

#[test]
fn delta_serde() {
let d = Delta::simple_edit(Interval::new(10, 12), Rope::from("+"), TEST_STR.len());
let ser = serde_json::to_value(d.clone()).expect("serialize failed");
eprintln!("{:?}", &ser);
let de: Delta<RopeInfo> = serde_json::from_value(ser).expect("deserialize failed");
assert_eq!(d.apply_to_string(TEST_STR), de.apply_to_string(TEST_STR));
}
}
17 changes: 11 additions & 6 deletions rust/rope/src/engine.rs
Expand Up @@ -39,13 +39,14 @@ use crate::multiset::{CountMatcher, Subset};
use crate::rope::{Rope, RopeInfo};

/// Represents the current state of a document and all of its history
#[derive(Serialize, Deserialize, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Engine {
/// The session ID used to create new `RevId`s for edits made on this device
#[serde(default = "default_session", skip_serializing)]
#[cfg_attr(feature = "serde", serde(default = "default_session", skip_serializing))]
session: SessionId,
/// The incrementing revision number counter for this session used for `RevId`s
#[serde(default = "initial_revision_counter", skip_serializing)]
#[cfg_attr(feature = "serde", serde(default = "initial_revision_counter", skip_serializing))]
rev_id_counter: u32,
/// The current contents of the document as would be displayed on screen
text: Rope,
Expand Down Expand Up @@ -75,7 +76,8 @@ pub struct Engine {

// The advantage of using a session ID over random numbers is that it can be
// easily delta-compressed later.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RevId {
// 96 bits has a 10^(-12) chance of collision with 400 million sessions and 10^(-6) with 100 billion.
// `session1==session2==0` is reserved for initialization which is the same on all sessions.
Expand All @@ -88,7 +90,8 @@ pub struct RevId {
num: u32,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct Revision {
/// This uniquely represents the identity of this revision and it stays
/// the same even if it is rebased or merged between devices.
Expand Down Expand Up @@ -126,7 +129,8 @@ struct FullPriority {

use self::Contents::*;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
enum Contents {
Edit {
/// Used to order concurrent inserts, for example auto-indentation
Expand Down Expand Up @@ -159,6 +163,7 @@ fn default_session() -> (u64, u32) {
}

/// Revision 0 is always an Undo of the empty set of groups
#[cfg(feature = "serde")]
fn initial_revision_counter() -> u32 {
1
}
Expand Down
7 changes: 5 additions & 2 deletions rust/rope/src/lib.rs
Expand Up @@ -27,10 +27,12 @@
extern crate bytecount;
extern crate memchr;
extern crate regex;
extern crate serde;
extern crate unicode_segmentation;

#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_derive;
extern crate serde;

#[cfg(test)]
extern crate serde_json;
#[cfg(test)]
Expand All @@ -45,6 +47,7 @@ pub mod find;
pub mod interval;
pub mod multiset;
pub mod rope;
#[cfg(feature = "serde")]
mod serde_impls;
pub mod spans;
#[cfg(test)]
Expand Down
6 changes: 4 additions & 2 deletions rust/rope/src/multiset.rs
Expand Up @@ -22,7 +22,8 @@ use crate::tree::{Node, NodeInfo, TreeBuilder};
use std::fmt;
use std::slice;

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct Segment {
len: usize,
count: usize,
Expand All @@ -34,7 +35,8 @@ struct Segment {
/// included in the set.
///
/// Internally, this is stored as a list of "segments" with a length and a count.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Subset {
/// Invariant, maintained by `SubsetBuilder`: all `Segment`s have non-zero
/// length, and no `Segment` has the same count as the one before it.
Expand Down
24 changes: 15 additions & 9 deletions rust/rope/src/rope.rs
Expand Up @@ -829,7 +829,6 @@ impl<'a> Iterator for Lines<'a> {
#[cfg(test)]
mod tests {
use super::*;
use serde_test::{assert_tokens, Token};

#[test]
fn replace_small() {
Expand Down Expand Up @@ -1019,14 +1018,6 @@ mod tests {
assert_eq!(None, a.next_grapheme_offset(s1.len() * 3 + 4));
}

#[test]
fn test_ser_de() {
let rope = Rope::from("a\u{00A1}\u{4E00}\u{1F4A9}");
assert_tokens(&rope, &[Token::Str("a\u{00A1}\u{4E00}\u{1F4A9}")]);
assert_tokens(&rope, &[Token::String("a\u{00A1}\u{4E00}\u{1F4A9}")]);
assert_tokens(&rope, &[Token::BorrowedStr("a\u{00A1}\u{4E00}\u{1F4A9}")]);
}

#[test]
fn line_of_offset_small() {
let a = Rope::from("a\nb\nc");
Expand Down Expand Up @@ -1207,6 +1198,13 @@ mod tests {
assert!(long_text.len() > 1024);
assert_eq!(cow, Cow::Borrowed(&long_text[..500]));
}
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use super::*;
use crate::Rope;
use serde_test::{assert_tokens, Token};

#[test]
fn serialize_and_deserialize() {
Expand All @@ -1222,4 +1220,12 @@ mod tests {
serde_json::from_str::<Rope>(json.as_str()).expect("error deserializing");
assert_eq!(rope, deserialized_rope);
}

#[test]
fn test_ser_de() {
let rope = Rope::from("a\u{00A1}\u{4E00}\u{1F4A9}");
assert_tokens(&rope, &[Token::Str("a\u{00A1}\u{4E00}\u{1F4A9}")]);
assert_tokens(&rope, &[Token::String("a\u{00A1}\u{4E00}\u{1F4A9}")]);
assert_tokens(&rope, &[Token::BorrowedStr("a\u{00A1}\u{4E00}\u{1F4A9}")]);
}
}

0 comments on commit 84bfe30

Please sign in to comment.