|
|
@ -4,46 +4,46 @@ use std::{ |
|
|
|
path::Path, |
|
|
|
path::Path, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
use crate::{data::ClientData, Result}; |
|
|
|
use crate::{data::AppData, Result}; |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to deserialize a Data struct from a string
|
|
|
|
/// Attempts to deserialize a Data struct from a string
|
|
|
|
pub fn from_str(s: &str) -> Result<ClientData> { |
|
|
|
pub fn from_str(s: &str) -> Result<AppData> { |
|
|
|
Ok(serde_json::from_str(s)?) |
|
|
|
Ok(serde_json::from_str(s)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to deserialize a Data struct from a slice of bytes
|
|
|
|
/// Attempts to deserialize a Data struct from a slice of bytes
|
|
|
|
pub fn from_slice(s: &[u8]) -> Result<ClientData> { |
|
|
|
pub fn from_slice(s: &[u8]) -> Result<AppData> { |
|
|
|
Ok(serde_json::from_slice(s)?) |
|
|
|
Ok(serde_json::from_slice(s)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to deserialize a Data struct from something that implements
|
|
|
|
/// Attempts to deserialize a Data struct from something that implements
|
|
|
|
/// the std::io::Read trait
|
|
|
|
/// the std::io::Read trait
|
|
|
|
pub fn from_reader<R: Read>(mut r: R) -> Result<ClientData> { |
|
|
|
pub fn from_reader<R: Read>(mut r: R) -> Result<AppData> { |
|
|
|
let mut buffer = Vec::new(); |
|
|
|
let mut buffer = Vec::new(); |
|
|
|
r.read_to_end(&mut buffer)?; |
|
|
|
r.read_to_end(&mut buffer)?; |
|
|
|
from_slice(&buffer) |
|
|
|
from_slice(&buffer) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to deserialize a Data struct from a file
|
|
|
|
/// Attempts to deserialize a Data struct from a file
|
|
|
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<ClientData> { |
|
|
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<AppData> { |
|
|
|
let path = path.as_ref(); |
|
|
|
let path = path.as_ref(); |
|
|
|
let file = File::open(path)?; |
|
|
|
let file = File::open(path)?; |
|
|
|
Ok(from_reader(file)?) |
|
|
|
Ok(from_reader(file)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to serialize a Data struct to a String
|
|
|
|
/// Attempts to serialize a Data struct to a String
|
|
|
|
pub fn to_string(data: &ClientData) -> Result<String> { |
|
|
|
pub fn to_string(data: &AppData) -> Result<String> { |
|
|
|
Ok(serde_json::to_string_pretty(data)?) |
|
|
|
Ok(serde_json::to_string_pretty(data)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to serialize a Data struct to a Vec of bytes
|
|
|
|
/// Attempts to serialize a Data struct to a Vec of bytes
|
|
|
|
pub fn to_vec(data: &ClientData) -> Result<Vec<u8>> { |
|
|
|
pub fn to_vec(data: &AppData) -> Result<Vec<u8>> { |
|
|
|
Ok(serde_json::to_vec(data)?) |
|
|
|
Ok(serde_json::to_vec(data)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to serialize a Data struct to something that implements the
|
|
|
|
/// Attempts to serialize a Data struct to something that implements the
|
|
|
|
/// std::io::Write trait
|
|
|
|
/// std::io::Write trait
|
|
|
|
pub fn to_writer<W: Write>(data: &ClientData, writer: W) -> Result<()> { |
|
|
|
pub fn to_writer<W: Write>(data: &AppData, writer: W) -> Result<()> { |
|
|
|
let mut buf_writer = BufWriter::new(writer); |
|
|
|
let mut buf_writer = BufWriter::new(writer); |
|
|
|
let vec = to_vec(data)?; |
|
|
|
let vec = to_vec(data)?; |
|
|
|
buf_writer.write_all(&vec)?; |
|
|
|
buf_writer.write_all(&vec)?; |
|
|
@ -55,7 +55,7 @@ pub fn to_writer<W: Write>(data: &ClientData, writer: W) -> Result<()> { |
|
|
|
/// When opening the file, this will set the `.write(true)` and
|
|
|
|
/// When opening the file, this will set the `.write(true)` and
|
|
|
|
/// `.truncate(true)` options, use the next method for more
|
|
|
|
/// `.truncate(true)` options, use the next method for more
|
|
|
|
/// fine-grained control
|
|
|
|
/// fine-grained control
|
|
|
|
pub fn to_file<P: AsRef<Path>>(data: &ClientData, path: P) -> Result<()> { |
|
|
|
pub fn to_file<P: AsRef<Path>>(data: &AppData, path: P) -> Result<()> { |
|
|
|
let mut options = OpenOptions::new(); |
|
|
|
let mut options = OpenOptions::new(); |
|
|
|
options.create(true).write(true).truncate(true); |
|
|
|
options.create(true).write(true).truncate(true); |
|
|
|
to_file_with_options(data, path, options)?; |
|
|
|
to_file_with_options(data, path, options)?; |
|
|
@ -63,7 +63,7 @@ pub fn to_file<P: AsRef<Path>>(data: &ClientData, path: P) -> Result<()> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Attempts to serialize a Data struct to a file
|
|
|
|
/// Attempts to serialize a Data struct to a file
|
|
|
|
pub fn to_file_with_options<P: AsRef<Path>>(data: &ClientData, path: P, options: OpenOptions) -> Result<()> { |
|
|
|
pub fn to_file_with_options<P: AsRef<Path>>(data: &AppData, path: P, options: OpenOptions) -> Result<()> { |
|
|
|
let path = path.as_ref(); |
|
|
|
let path = path.as_ref(); |
|
|
|
let file = options.open(path)?; |
|
|
|
let file = options.open(path)?; |
|
|
|
to_writer(data, file)?; |
|
|
|
to_writer(data, file)?; |
|
|
@ -93,7 +93,7 @@ mod tests { |
|
|
|
let desered = from_str(DOC).expect("Couldn't deserialize Data"); |
|
|
|
let desered = from_str(DOC).expect("Couldn't deserialize Data"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
desered, |
|
|
|
desered, |
|
|
|
ClientData { |
|
|
|
AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -108,7 +108,7 @@ mod tests { |
|
|
|
let desered = from_slice(&doc).expect("Couldn't deserialize Data"); |
|
|
|
let desered = from_slice(&doc).expect("Couldn't deserialize Data"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
desered, |
|
|
|
desered, |
|
|
|
ClientData { |
|
|
|
AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -124,7 +124,7 @@ mod tests { |
|
|
|
let desered = from_reader(doc).expect("Couldn't deserialize Data"); |
|
|
|
let desered = from_reader(doc).expect("Couldn't deserialize Data"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
desered, |
|
|
|
desered, |
|
|
|
ClientData { |
|
|
|
AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -140,7 +140,7 @@ mod tests { |
|
|
|
let desered = from_file(datafile.path()).expect("Couldn't deserialize Data"); |
|
|
|
let desered = from_file(datafile.path()).expect("Couldn't deserialize Data"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
desered, |
|
|
|
desered, |
|
|
|
ClientData { |
|
|
|
AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -151,7 +151,7 @@ mod tests { |
|
|
|
} |
|
|
|
} |
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_to_string() { |
|
|
|
fn test_to_string() { |
|
|
|
let data = ClientData { |
|
|
|
let data = AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -164,7 +164,7 @@ mod tests { |
|
|
|
} |
|
|
|
} |
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_to_vec() { |
|
|
|
fn test_to_vec() { |
|
|
|
let data = ClientData { |
|
|
|
let data = AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -177,7 +177,7 @@ mod tests { |
|
|
|
} |
|
|
|
} |
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_to_writer() { |
|
|
|
fn test_to_writer() { |
|
|
|
let data = ClientData { |
|
|
|
let data = AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -192,7 +192,7 @@ mod tests { |
|
|
|
} |
|
|
|
} |
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_to_file() { |
|
|
|
fn test_to_file() { |
|
|
|
let data = ClientData { |
|
|
|
let data = AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
@ -207,7 +207,7 @@ mod tests { |
|
|
|
} |
|
|
|
} |
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_to_file_with_options() { |
|
|
|
fn test_to_file_with_options() { |
|
|
|
let data = ClientData { |
|
|
|
let data = AppData { |
|
|
|
base: "https://example.com".into(), |
|
|
|
base: "https://example.com".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_id: "adbc01234".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|
client_secret: "0987dcba".into(), |
|
|
|