#object #serialization #traits #reading #format #serde #type

objio

This crate provides simple traits for reading and writing objects

3 releases

0.1.2 Sep 12, 2024
0.1.1 Sep 11, 2024
0.1.0 Sep 11, 2024

#1232 in Encoding

48 downloads per month
Used in 2 crates

MIT license

15KB
202 lines

Rust crate objio

This crate provides simple traits for reading and writing objects.

The traits ObjectReader and ObjectWriter are not intended as a generalized serialization framework like serde, they are provided to simply read/write specific object types in specific formats.

Example

TBD.

Changes

Version 0.1.2

  • Documentation: added documentation to all traits and a detailed example at the module level.

Version 0.1.1

  • Refactor: updated error type processing.
    • Removed custom Error type.
    • Changed trait Error types to have a constraint requiring From<<std::io::Error>>.

Version 0.1.0

  • Initial release.

lib.rs:

This crate provides simple traits for reading and writing objects.

The traits ObjectReader and ObjectWriter are not intended as a generalized serialization framework like serde, they are provided to simply read/write specific object types in specific formats. These traits were refactored from the rdftk_io crate that provided a number of parsers and generators for different RDF representations.

As a number of implementations require options to configure parsers and generators the trait HasOptions can be implemented to provide this in a common manner.

Example Writer

  1. The type TestObject is the type we wich to be able to write, it has a single string field.
  2. The type TestError is required as it can be created from an IO error instance.
  3. The type TestOptions are the options that configure our generator, currently only defining the amount of indentation before writing TestObject instances.
  4. The type TestWriter` is where the magic starts, ...
  5. It contains a field for the options above.
  6. It implements the trait HasOptions using the macro impl_has_options!.
  7. It implements the trait ObjectWriter.
  8. We the construct an example instance of the test object and an instance of the writer with options.
  9. Finally, we write the example object and compare it to expected results.
use objio::{impl_has_options, HasOptions, ObjectWriter};
use std::io::Write;

#[derive(Debug, Default)]
struct TestObject { // our writeable type
value: String,
}

#[derive(Debug, Default)]
struct TestError {} // implements From<std::io::Error>

#[derive(Debug, Default)]
struct TestOptions {
indent: usize,
}

#[derive(Debug, Default)]
struct TestWriter {
options: TestOptions,
}

impl_has_options!(TestWriter, TestOptions);

impl ObjectWriter<TestObject> for TestWriter {
type Error = TestError;

fn write<W>(&self, w: &mut W, object: &TestObject) -> Result<(), Self::Error>
where
W: Write,
{
let indent = self.options.indent;
let value = &object.value;
w.write_all(format!("{:indent$}{value}", "").as_bytes())?;
Ok(())
}
}

let example = TestObject::from("Hello");
let writer = TestWriter::default().with_options(TestOptions { indent: 2 });

assert_eq!(
writer.write_to_string(&example).unwrap(),
"  Hello".to_string()
);

No runtime deps