aboutsummaryrefslogtreecommitdiff
path: root/src/util/migrate.rs
blob: 199c68f64d2e74595a267e4640b7dd2da5b88157 (plain) (blame)
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
use serde::{Deserialize, Serialize};

pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static {
	/// A sequence of bytes to add at the beginning of the serialized
	/// string, to identify that the data is of this version.
	const MARKER: &'static [u8] = b"";

	/// The previous version of this data type, from which items of this version
	/// can be migrated. Set `type Previous = NoPrevious` to indicate that this datatype
	/// is the initial schema and cannot be migrated.
	type Previous: Migrate;

	/// This function must be filled in by implementors to migrate from a previons iteration
	/// of the data format.
	fn migrate(previous: Self::Previous) -> Self;

	fn decode(bytes: &[u8]) -> Option<Self> {
		if bytes.len() >= Self::MARKER.len() && &bytes[..Self::MARKER.len()] == Self::MARKER {
			if let Ok(value) =
				rmp_serde::decode::from_read_ref::<_, Self>(&bytes[Self::MARKER.len()..])
			{
				return Some(value);
			}
		}

		Self::Previous::decode(bytes).map(Self::migrate)
	}

	fn encode(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
		let mut wr = Vec::with_capacity(128);
		wr.extend_from_slice(Self::MARKER);
		let mut se = rmp_serde::Serializer::new(&mut wr)
			.with_struct_map()
			.with_string_variants();
		self.serialize(&mut se)?;
		Ok(wr)
	}
}

pub trait InitialFormat: Serialize + for<'de> Deserialize<'de> + 'static {
	/// A sequence of bytes to add at the beginning of the serialized
	/// string, to identify that the data is of this version.
	const MARKER: &'static [u8] = b"";
}

// ----

impl<T: InitialFormat> Migrate for T {
	const MARKER: &'static [u8] = <T as InitialFormat>::MARKER;

	type Previous = NoPrevious;

	fn migrate(_previous: Self::Previous) -> Self {
		unreachable!();
	}
}

#[derive(Serialize, Deserialize)]
pub struct NoPrevious;

impl Migrate for NoPrevious {
	type Previous = NoPrevious;

	fn migrate(_previous: Self::Previous) -> Self {
		unreachable!();
	}

	fn decode(_bytes: &[u8]) -> Option<Self> {
		None
	}

	fn encode(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
		unreachable!()
	}
}