Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Jan 28, 2023
1 parent 884a131 commit 012afed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Binary file removed helix-core/.DS_Store
Binary file not shown.
11 changes: 4 additions & 7 deletions helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::parse::*;
use crate::{Assoc, ChangeSet, Range, Rope, Selection, Transaction};
use once_cell::sync::Lazy;
use regex::Regex;
use std::fs::File;
use std::io::{Read, Write};
use std::num::NonZeroUsize;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -68,25 +67,23 @@ struct Revision {
timestamp: Instant,
}

const HEADER_TAG: &str = "Helix Undofile";
const CURRENT_VERSION: u8 = 1;
const HEADER_TAG: &str = "Helix Undofile 0.1\n";

pub fn serialize_history<W: Write>(writer: &mut W, history: &History) -> std::io::Result<()> {
write_string(writer, HEADER_TAG)?;
write_byte(writer, CURRENT_VERSION)?;
write_usize(writer, history.current)?;
write_vec(writer, &history.revisions, serialize_revision)?;
Ok(())
}

pub fn deserialize_history<R: Read>(reader: &mut R) -> std::io::Result<History> {
if HEADER_TAG != read_string(reader)? {
let header = read_string(reader)?;
if HEADER_TAG != header {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"missing undofile header",
format!("missing undofile header"),
))
} else {
let _version = read_byte(reader)?;
let timestamp = Instant::now();
let current = read_usize(reader)?;
let revisions = read_vec(reader, |reader| deserialize_revision(reader, timestamp))?;
Expand Down
8 changes: 5 additions & 3 deletions helix-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ pub fn read_usize<R: Read>(reader: &mut R) -> Result<usize> {

pub fn read_string<R: Read>(reader: &mut R) -> Result<String> {
let len = read_usize(reader)?;
let mut buf = String::with_capacity(len);
reader.read_to_string(&mut buf)?;
Ok(buf)
let mut buf = vec![0; len];
reader.read_exact(&mut buf)?;

let res = String::from_utf8(buf).map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
Ok(res)
}

pub fn read_vec<R: Read, T>(reader: &mut R, f: impl Fn(&mut R) -> Result<T>) -> Result<Vec<T>> {
Expand Down

0 comments on commit 012afed

Please sign in to comment.