Skip to content

Commit

Permalink
binary serialization of project file
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Jun 4, 2020
1 parent 58459d2 commit 03644b7
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 43 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions proj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ log4rs = { version = "0.8.1", features = ["rolling_file_appender", "compound_pol
log = "0.4"
failure = "0.1"
failure_derive = "0.1"
bincode = "1.2"

aloevera_util = { path = "../util", version = "0.2.3" }
aloevera_vera = { path = "../vera", version = "0.2.3" }
17 changes: 12 additions & 5 deletions proj/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Error {
}

/// Wallet errors, mostly wrappers around underlying crypto or I/O errors.
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Debug, Fail)]
pub enum ErrorKind {
/// IO Error
#[fail(display = "I/O error: {}", _0)]
Expand All @@ -42,6 +42,9 @@ pub enum ErrorKind {
/// Argument Error
#[fail(display = "Argument Error: {}", _0)]
ArgumentError(String),
/// Errors from the Bincode crate
#[fail(display = "Bincode Error: {}", _0)]
BincodeError(bincode::Error),
/// Other
#[fail(display = "Generic error: {}", _0)]
GenericError(String),
Expand Down Expand Up @@ -74,10 +77,6 @@ impl Display for Error {
}

impl Error {
/// get kind
pub fn kind(&self) -> ErrorKind {
self.inner.get_context().clone()
}
/// get cause string
pub fn cause_string(&self) -> String {
match self.cause() {
Expand Down Expand Up @@ -124,3 +123,11 @@ impl From<ParseIntError> for Error {
}
}
}

impl From<bincode::Error> for Error {
fn from(error: bincode::Error) -> Error {
Error {
inner: Context::new(ErrorKind::BincodeError(error)),
}
}
}
10 changes: 6 additions & 4 deletions proj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ pub use error::{Error, ErrorKind};

pub use project::AloeVeraProject;

/// Just a wrapper around a serializable object
pub trait Jsonable {
/// to json
fn to_json(&self) -> Result<String, Error>;
/// And around the binary version
pub trait Binable {
/// to binary
fn to_bin(&self) -> Result<Vec<u8>, Error>;
/// from binary
fn from_bin(encoded: &Vec<u8>) -> Result<Box<Self>, Error>;
}
18 changes: 10 additions & 8 deletions proj/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//! Top Level Project file definition

use crate::Jsonable;
use crate::Binable;
use crate::{Error, ErrorKind};
use std::collections::BTreeMap;
use vera::{VeraBitmap, VeraImageSet, VeraPalette, VeraSprite, VeraTileMap};
Expand All @@ -35,13 +35,15 @@ pub struct AloeVeraProject<'a> {
pub bitmaps: BTreeMap<String, VeraBitmap<'a>>,
}

impl<'a> Jsonable for AloeVeraProject<'a> {
fn to_json(&self) -> Result<String, Error> {
serde_json::to_string_pretty(&self).map_err(|e| {
let msg = format!("Unable to create JSON: {}", e);
error!("{}", msg);
ErrorKind::JSONError(msg).into()
})
impl<'a> Binable for AloeVeraProject<'a> {
fn to_bin(&self) -> Result<Vec<u8>, Error> {
let encoded = bincode::serialize(&self)?;
Ok(encoded)
}

fn from_bin(encoded: &Vec<u8>) -> Result<Box<Self>, Error> {
let decoded = bincode::deserialize(&encoded[..])?;
Ok(Box::new(decoded))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/bitmap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};

use crate::cmd::common::{self, GlobalArgs};
use vera::VeraBitmap;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn bitmap_init(g_args: &GlobalArgs, args: &BitmapInitArgs) -> Result<(), Err
};
let bitmap = VeraBitmap::init_from_imageset(&args.id, &imageset)?;
proj.bitmaps.insert(args.id.clone(), bitmap);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;
common::output_to_file(&project_file, &proj.to_bin()?, &None)?;

Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/create/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::Error;
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};
use util::fat;

/// Arguments for the initial create project command
Expand All @@ -30,8 +30,7 @@ pub fn create_project(args: &CreateProjectArgs) -> Result<(), Error> {
info!("Creating new project file at: {}", args.output_file);

let proj = AloeVeraProject::new(id);
let json = proj.to_json()?;
crate::cmd::common::output_to_file(&args.output_file, &json.as_bytes(), &None)?;
crate::cmd::common::output_to_file(&args.output_file, &proj.to_bin()?, &None)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/imageset/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::Jsonable;
use proj::Binable;

use crate::cmd::common::{self, GlobalArgs};
use vera::{VeraImageSet, VeraImageSetLoadConfig, VeraPixelDepth};
Expand All @@ -26,7 +26,7 @@ fn insert_imageset(
//info!("Inserting imageset into project: {}", project_file);
let mut proj = crate::cmd::common::load_project(project_file.clone())?;
proj.imagesets.insert(id.into(), imageset.clone());
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes(), &None)?;
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_bin()?, &None)?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/palette/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};

use crate::cmd::common::{self, GlobalArgs};
use vera::{VeraPalette, VeraPaletteLoadConfig};
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn palette_import(g_args: &GlobalArgs, args: &PaletteImportArgs) -> Result<(
let proj_json = common::read_file_string(&project_file)?;
let mut proj = AloeVeraProject::new_from_json(&proj_json)?;
proj.palettes.insert(palette.id.clone(), palette);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;
common::output_to_file(&project_file, &proj.to_bin()?, &None)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/sprite/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};

use crate::cmd::common::{self, GlobalArgs};
use vera::VeraSprite;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn sprite_init(g_args: &GlobalArgs, args: &SpriteInitArgs) -> Result<(), Err
};
let sprite = VeraSprite::init_from_imageset(&args.id, &imageset)?;
proj.sprites.insert(args.id.clone(), sprite);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;
common::output_to_file(&project_file, &proj.to_bin()?, &None)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/tilemap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use proj::Jsonable;
use proj::Binable;

use crate::cmd::common::{self, GlobalArgs};
use crate::{Error, ErrorKind};
Expand All @@ -24,7 +24,7 @@ fn insert_tilemap(
) -> Result<(), Error> {
let mut proj = crate::cmd::common::load_project(project_file.clone())?;
proj.tilemaps.insert(id.into(), tilemap.clone());
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes(), &None)?;
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_bin()?, &None)?;
Ok(())
}

Expand Down
16 changes: 8 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub struct Error {
}

/// Wallet errors, mostly wrappers around underlying crypto or I/O errors.
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Debug, Fail)]
pub enum ErrorKind {
/// Vera Error
#[fail(display = "Vera module error: {}", _0)]
Vera(vera::ErrorKind),
Vera(vera::Error),
/// Project Error
#[fail(display = "Project Error")]
Proj(proj::ErrorKind),
#[fail(display = "Project Error: {}", _0)]
Proj(proj::Error),
/// IO Error
#[fail(display = "I/O error: {}", _0)]
IO(String),
Expand Down Expand Up @@ -78,9 +78,9 @@ impl Display for Error {

impl Error {
/// get kind
pub fn kind(&self) -> ErrorKind {
/*pub fn kind(&self) -> ErrorKind {
self.inner.get_context().clone()
}
}*/
/// get cause string
pub fn cause_string(&self) -> String {
match self.cause() {
Expand All @@ -101,15 +101,15 @@ impl Error {
impl From<proj::Error> for Error {
fn from(error: proj::Error) -> Error {
Error {
inner: Context::new(ErrorKind::Proj(error.kind())),
inner: Context::new(ErrorKind::Proj(error)),
}
}
}

impl From<vera::Error> for Error {
fn from(error: vera::Error) -> Error {
Error {
inner: Context::new(ErrorKind::Vera(error.kind())),
inner: Context::new(ErrorKind::Vera(error)),
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions vera/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Error {
}

/// Wallet errors, mostly wrappers around underlying crypto or I/O errors.
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Debug, Fail)]
pub enum ErrorKind {
/// IO Error
#[fail(display = "I/O error: {}", _0)]
Expand Down Expand Up @@ -174,10 +174,6 @@ impl Display for Error {
}

impl Error {
/// get kind
pub fn kind(&self) -> ErrorKind {
self.inner.get_context().clone()
}
/// get cause string
pub fn cause_string(&self) -> String {
match self.cause() {
Expand Down

0 comments on commit 03644b7

Please sign in to comment.