1+ // Copyright 2023 Alibaba Cloud. All rights reserved.
12// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
23// SPDX-License-Identifier: Apache-2.0
34#![ deny( missing_docs) ]
910//! - `Versionize` trait
1011//! - `VersionMap` helper
1112//!
12- //! `VersionMap` maps individual structure/enum versions to a root version
13- //! (app version). This mapping is required both when serializing or
14- //! deserializing structures as it needs to know which version of structure
15- //! to serialize for a given target app version .
13+ //! `VersionMap` maps individual crate name to the crate Semver Version.
14+ //! This mapping is required both when serializing or deserializing structures
15+ //! as it needs to record the crate version and crate name for serializing,
16+ //! and to know which crate version to be used for deserializing .
1617//!
1718//! `Versionize` trait is implemented for the following primitives:
1819//! u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, char, f32, f64,
@@ -32,13 +33,12 @@ pub mod crc;
3233pub mod primitives;
3334pub mod version_map;
3435
35- use std:: any:: TypeId ;
36- use std:: io:: { Read , Write } ;
36+ pub use semver;
3737pub use version_map:: VersionMap ;
38- use versionize_derive:: Versionize ;
38+
39+ use std:: io:: { Read , Write } ;
3940
4041/// Versioned serialization/deserialization error definitions.
41- #[ allow( clippy:: derive_partial_eq_without_eq) ] // FIXME: next major release
4242#[ derive( Debug , PartialEq ) ]
4343pub enum VersionizeError {
4444 /// An IO error occured.
@@ -53,6 +53,16 @@ pub enum VersionizeError {
5353 StringLength ( usize ) ,
5454 /// Vector length exceeded.
5555 VecLength ( usize ) ,
56+ /// HashMap length exceeded.
57+ HashMapLength ( usize ) ,
58+ /// HashSet length exceeded.
59+ HashSetLength ( usize ) ,
60+ /// Unsupported version.
61+ UnsuportVersion ( String ) ,
62+ /// Multiple version
63+ MultipleVersion ( String , String , String ) ,
64+ /// Not found version
65+ NotFound ( String ) ,
5666}
5767
5868impl std:: fmt:: Display for VersionizeError {
@@ -76,6 +86,25 @@ impl std::fmt::Display for VersionizeError {
7686 bad_len,
7787 primitives:: MAX_VEC_SIZE
7888 ) ,
89+ HashMapLength ( bad_len) => write ! (
90+ f,
91+ "HashMap of length exceeded {} > {} bytes" ,
92+ bad_len,
93+ primitives:: MAX_HASH_MAP_LEN
94+ ) ,
95+ HashSetLength ( bad_len) => write ! (
96+ f,
97+ "HashSet of length exceeded {} > {} bytes" ,
98+ bad_len,
99+ primitives:: MAX_HASH_SET_LEN
100+ ) ,
101+ UnsuportVersion ( ver) => write ! ( f, "{} version is NOT supported." , ver) ,
102+ MultipleVersion ( rcrate, a, b) => write ! (
103+ f,
104+ "There are multiple version {}, {} in {} crate." ,
105+ a, b, rcrate,
106+ ) ,
107+ NotFound ( v) => write ! ( f, "Not found {}." , v) ,
79108 }
80109 }
81110}
@@ -103,60 +132,32 @@ pub type VersionizeResult<T> = std::result::Result<T, VersionizeError>;
103132/// #[inline]
104133/// fn serialize<W: std::io::Write>(
105134/// &self,
106- /// writer: &mut W,
107- /// version_map: &VersionMap,
108- /// app_version: u16,
135+ /// writer: W,
136+ /// version_map: &mut VersionMap,
109137/// ) -> VersionizeResult<()> {
110- /// self.0.serialize(writer, version_map, app_version )
138+ /// self.0.serialize(writer, version_map)
111139/// }
112140///
113141/// #[inline]
114142/// fn deserialize<R: std::io::Read>(
115- /// reader: &mut R,
143+ /// reader: R,
116144/// version_map: &VersionMap,
117- /// app_version: u16,
118145/// ) -> VersionizeResult<Self> {
119- /// Ok(MyType(T::deserialize(reader, version_map, app_version)?))
120- /// }
121- ///
122- /// fn version() -> u16 {
123- /// 1
146+ /// Ok(MyType(T::deserialize(reader, version_map)?))
124147/// }
125148/// }
126149/// ```
127150/// [1]: https://docs.rs/versionize_derive/latest/versionize_derive/derive.Versionize.html
128151pub trait Versionize {
129- /// Serializes `self` to `target_verion` using the specficifed `writer` and
152+ /// Serializes `self` using the specficifed `writer` and
130153 /// `version_map`.
131- fn serialize < W : Write > (
132- & self ,
133- writer : & mut W ,
134- version_map : & VersionMap ,
135- target_version : u16 ,
136- ) -> VersionizeResult < ( ) > ;
154+ fn serialize < W : Write > ( & self , writer : W , version_map : & mut VersionMap ) -> VersionizeResult < ( ) > ;
137155
138- /// Returns a new instance of `Self` by deserializing from `source_version`
139- /// using the specficifed `reader` and `version_map`.
140- fn deserialize < R : Read > (
141- reader : & mut R ,
142- version_map : & VersionMap ,
143- source_version : u16 ,
144- ) -> VersionizeResult < Self >
156+ /// Returns a new instance of `Self` by deserializing using the specficifed `reader`
157+ /// and `version_map`.
158+ fn deserialize < R : Read > ( reader : R , version_map : & VersionMap ) -> VersionizeResult < Self >
145159 where
146160 Self : Sized ;
147-
148- /// Returns the `Self` type id.
149- /// The returned ID represents a globally unique identifier for a type.
150- /// It is required by the `VersionMap` implementation.
151- fn type_id ( ) -> std:: any:: TypeId
152- where
153- Self : ' static ,
154- {
155- TypeId :: of :: < Self > ( )
156- }
157-
158- /// Returns latest `Self` version number.
159- fn version ( ) -> u16 ;
160161}
161162
162163#[ cfg( test) ]
0 commit comments