-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
56 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "serdeio" | ||
version = "0.2.0" | ||
version = "0.3.0" | ||
edition = "2021" | ||
license = "MIT" | ||
authors = ["Du Shiqiao <[email protected]>"] | ||
|
@@ -13,14 +13,13 @@ categories = ["io-util", "serialization"] | |
|
||
[lib] | ||
name = "serdeio" | ||
doctest = false | ||
|
||
[dependencies] | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
serde_yaml = { version = "0.9", optional = true } | ||
csv = { version = "1", optional = true } | ||
|
||
[dev-dependencies] | ||
anyhow = "1" | ||
|
||
[features] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
use anyhow::{anyhow, Context, Result as AnyResult}; | ||
use serde::{Deserialize, Serialize}; | ||
use serdeio::{read_records_from_file, write_records_to_writer, DataFormat}; | ||
use serdeio::{read_record_from_file, write_records_to_writer, DataFormat}; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct User { | ||
id: u32, | ||
name: String, | ||
items: Vec<u32>, | ||
items: Vec<String>, | ||
} | ||
|
||
fn main() -> AnyResult<()> { | ||
pub fn main() -> AnyResult<()> { | ||
// get input file path from argv | ||
let args: Vec<String> = std::env::args().collect(); | ||
let input_file_path = &args[1]; | ||
|
||
// read to memory | ||
let users: Vec<User> = read_records_from_file(input_file_path) | ||
// read json to memory | ||
let users: Vec<User> = read_record_from_file(input_file_path) | ||
.map_err(|e| anyhow! {e}) | ||
.context("Failed to read records from file")?; | ||
|
||
// write to stdout in YAML format | ||
// write to stdout in json lines format | ||
let writer = std::io::stdout(); | ||
write_records_to_writer(writer, DataFormat::Yaml, &users).unwrap(); | ||
write_records_to_writer(writer, DataFormat::JsonLines, &users).unwrap(); | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
use std::io::{Read, Write}; | ||
|
||
use anyhow::Result as AnyResult; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
use crate::common::Result; | ||
|
||
pub fn read<T: DeserializeOwned>(reader: impl Read) -> Result<T> { | ||
pub fn read<T: DeserializeOwned>(reader: impl Read) -> AnyResult<T> { | ||
serde_json::from_reader(reader).map_err(|e| e.into()) | ||
} | ||
|
||
pub fn write<T: Serialize>(writer: impl Write, record: &T) -> Result<()> { | ||
pub fn write<T: Serialize>(writer: impl Write, record: &T) -> AnyResult<()> { | ||
serde_json::to_writer(writer, record).map_err(|e| e.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
use std::io::{Read, Write}; | ||
|
||
use anyhow::Result as AnyResult; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
use crate::common::Result; | ||
|
||
pub fn read<T: DeserializeOwned>(reader: impl Read) -> Result<T> { | ||
pub fn read<T: DeserializeOwned>(reader: impl Read) -> AnyResult<T> { | ||
serde_yaml::from_reader(reader).map_err(|e| e.into()) | ||
} | ||
|
||
pub fn write<T: Serialize>(writer: impl Write, record: &T) -> Result<()> { | ||
pub fn write<T: Serialize>(writer: impl Write, record: &T) -> AnyResult<()> { | ||
serde_yaml::to_writer(writer, record).map_err(|e| e.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters