From ebb741657bb850512dce02f0524a4a388fbe6857 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 6 Apr 2021 11:23:42 +0200 Subject: Rewrite StdinApi as BufferApi So we do not depend on stdin anymore. The user of the lib should simply provide a Read. Signed-off-by: Matthias Beyer --- librepology/src/v1/buffer.rs | 41 ++++++++++++++++++++++++++++ librepology/src/v1/mod.rs | 2 +- librepology/src/v1/stdinapi.rs | 62 ------------------------------------------ 3 files changed, 42 insertions(+), 63 deletions(-) create mode 100644 librepology/src/v1/buffer.rs delete mode 100644 librepology/src/v1/stdinapi.rs diff --git a/librepology/src/v1/buffer.rs b/librepology/src/v1/buffer.rs new file mode 100644 index 0000000..534ab4c --- /dev/null +++ b/librepology/src/v1/buffer.rs @@ -0,0 +1,41 @@ +use std::io::Read; + +use crate::v1::error::Result; +use crate::v1::error::RepologyError as Error; +use crate::v1::types::Problem; +use crate::v1::types::Package; +use crate::v1::api::Api; + +#[derive(Debug)] +pub struct BufferApi { + buf: String, +} + +impl BufferApi +{ + pub fn read_from(mut input: R) -> Result + where R: Read, + { + let mut buf = String::new(); + let read = input.read_to_string(&mut buf)?; + trace!("Read {} bytes from stdin", read); + Ok(BufferApi { buf }) + } +} + +impl Api for BufferApi { + + fn project>(&self, _name: N) -> Result> { + serde_json::de::from_str(&self.buf).map_err(Error::from) + } + + fn problems_for_repo>(&self, _repo: R) -> Result> { + serde_json::de::from_str(&self.buf).map_err(Error::from) + } + + fn problems_for_maintainer>(&self, _maintainer: M) -> Result> { + serde_json::de::from_str(&self.buf).map_err(Error::from) + } + +} + diff --git a/librepology/src/v1/mod.rs b/librepology/src/v1/mod.rs index 1d1db8d..aa1cd9b 100644 --- a/librepology/src/v1/mod.rs +++ b/librepology/src/v1/mod.rs @@ -1,5 +1,5 @@ pub mod restapi; -pub mod stdinapi; +pub mod buffer; pub mod api; pub mod types; pub mod error; diff --git a/librepology/src/v1/stdinapi.rs b/librepology/src/v1/stdinapi.rs deleted file mode 100644 index 6c0d38b..0000000 --- a/librepology/src/v1/stdinapi.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::io::{Stdin, Read}; -use std::cell::RefCell; -use std::ops::Deref; -use std::ops::DerefMut; - -use crate::v1::error::Result; -use crate::v1::error::RepologyError as Error; -use crate::v1::types::Problem; -use crate::v1::types::Package; -use crate::v1::api::Api; - -/// Wrapper for "stdin" -/// -/// This way we can implement the `Api` trait for StdIn (via a Wrapper for interior mutability) -/// This way we can read the data from stdin and process it. -pub struct StdinWrapper(RefCell); - -impl From for StdinWrapper { - fn from(inner: Stdin) -> Self { - StdinWrapper(RefCell::new(inner)) - } -} - -impl Deref for StdinWrapper { - type Target = RefCell; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for StdinWrapper { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl Api for StdinWrapper { - - fn project>(&self, _name: N) -> Result> { - let s = read_to_string(self.0.try_borrow_mut()?.deref_mut())?; - serde_json::de::from_str(&s).map_err(Error::from) - } - - fn problems_for_repo>(&self, _repo: R) -> Result> { - let s = read_to_string(self.0.try_borrow_mut()?.deref_mut())?; - serde_json::de::from_str(&s).map_err(Error::from) - } - - fn problems_for_maintainer>(&self, _maintainer: M) -> Result> { - let s = read_to_string(self.0.try_borrow_mut()?.deref_mut())?; - serde_json::de::from_str(&s).map_err(Error::from) - } - -} - -fn read_to_string(input: &mut dyn Read) -> Result { - let mut buffer = String::new(); - let read = input.read_to_string(&mut buffer)?; - trace!("Read {} bytes from stdin", read); - Ok(buffer) -} -- cgit v1.2.3