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
// Copyright (c) 2017 Fabian Schuiki

//! This module implements the on-disk AST storage. Items can be serialized to
//! disk and deserialized again ata later point.

use std;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{Error, ErrorKind};
use super::ast;
use bincode::SizeLimit;
use bincode::rustc_serialize::{encode_into, decode_from};


pub fn store_items(path: &str, _: &str, ast: ast::Root) -> std::io::Result<()> {
	let mut file = OpenOptions::new().append(true).create(true).open(path)?;
	file.write(&[42])?;
	encode_into(&ast, &mut file, SizeLimit::Infinite)
		.map_err(|e| Error::new(ErrorKind::Other, e))
}


pub fn load_items(path: &str) -> std::io::Result<Vec<ast::Root>> {
	let mut file = OpenOptions::new().read(true).open(path)?;
	let mut v = Vec::new();
	loop {
		let mut tagbuf = [0];
		if let Err(e) = file.read_exact(&mut tagbuf) {
			if e.kind() == std::io::ErrorKind::UnexpectedEof {
				break;
			} else {
				return Err(e);
			}
		} else {
			assert_eq!(tagbuf[0], 42);
		}
		match decode_from(&mut file, SizeLimit::Infinite) {
			Ok(x) => v.push(x),
			Err(e) => return Err(Error::new(ErrorKind::InvalidInput, e)),
		}
	}
	Ok(v)
}