From 79d82cf3d349d7914de5e198d1e191171a8fcea6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 25 Apr 2019 09:36:21 +0200 Subject: Refactor: Move StdinWrapper to own module --- librepology/src/v1/api.rs | 57 -------------------------------------- librepology/src/v1/mod.rs | 1 + librepology/src/v1/stdinapi.rs | 63 ++++++++++++++++++++++++++++++++++++++++++ src/backend.rs | 2 +- 4 files changed, 65 insertions(+), 58 deletions(-) create mode 100644 librepology/src/v1/stdinapi.rs diff --git a/librepology/src/v1/api.rs b/librepology/src/v1/api.rs index b493209..e192022 100644 --- a/librepology/src/v1/api.rs +++ b/librepology/src/v1/api.rs @@ -1,10 +1,4 @@ -use std::io::{Stdin, Read}; -use std::cell::RefCell; -use std::ops::Deref; -use std::ops::DerefMut; - use failure::Fallible as Result; -use failure::Error; use crate::v1::types::Problem; use crate::v1::types::Package; @@ -26,54 +20,3 @@ pub trait 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 Read) -> Result { - let mut buffer = String::new(); - let read = input.read_to_string(&mut buffer)?; - trace!("Read {} bytes from stdin", read); - Ok(buffer) -} diff --git a/librepology/src/v1/mod.rs b/librepology/src/v1/mod.rs index cd4d2fe..1d5eed6 100644 --- a/librepology/src/v1/mod.rs +++ b/librepology/src/v1/mod.rs @@ -1,3 +1,4 @@ pub mod restapi; +pub mod stdinapi; pub mod api; pub mod types; diff --git a/librepology/src/v1/stdinapi.rs b/librepology/src/v1/stdinapi.rs new file mode 100644 index 0000000..8ee26a4 --- /dev/null +++ b/librepology/src/v1/stdinapi.rs @@ -0,0 +1,63 @@ +use std::io::{Stdin, Read}; +use std::cell::RefCell; +use std::ops::Deref; +use std::ops::DerefMut; + +use failure::Fallible as Result; +use failure::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 Read) -> Result { + let mut buffer = String::new(); + let read = input.read_to_string(&mut buffer)?; + trace!("Read {} bytes from stdin", read); + Ok(buffer) +} diff --git a/src/backend.rs b/src/backend.rs index 23a09d4..fe1fa7e 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -3,8 +3,8 @@ use failure::Fallible as Result; use librepology::v1::api::Api; use librepology::v1::restapi::RestApi; +use librepology::v1::stdinapi::StdinWrapper; use librepology::v1::types::*; -use librepology::v1::api::StdinWrapper; use crate::config::Configuration; -- cgit v1.2.3