History

[async-client] refactor

Sebastian Thiel committed 5 years ago

Commit 89e6f661 parent 7f29adc3 files · +30 −45Browse files

Unified · Split

3 files
  1. gitoxide-core/src/pack/receive.rs +28 −43
  2. src/plumbing/lean/main.rs +1 −1
  3. src/plumbing/pretty/main.rs +1 −1
gitoxide-core/src/pack/receive.rs+28−43
@@ -17,7 +17,7 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3; · 16 unmodified lines
1717 pub struct Context<W> {
1818 pub thread_limit: Option<usize>,
1919 pub format: OutputFormat,
20− pub out: Option<W>,
20+ pub out: W,
2121 }
2222
2323 struct CloneDelegate<W> {
@@ -117,13 +117,12 @@ mod blocking_io { · 93 unmodified lines
117117 }
118118 }
119119
120− match (self.ctx.format, self.ctx.out.as_mut()) {
121− (OutputFormat::Human, Some(out)) => drop(print(out, outcome, refs)),
120+ match self.ctx.format {
121+ OutputFormat::Human => drop(print(&mut self.ctx.out, outcome, refs)),
122122 #[cfg(feature = "serde1")]
123− (OutputFormat::Json, Some(out)) => {
124− serde_json::to_writer_pretty(out, &JsonOutcome::from_outcome_and_refs(outcome, refs))?
123+ OutputFormat::Json => {
124+ serde_json::to_writer_pretty(&mut self.ctx.out, &JsonOutcome::from_outcome_and_refs(outcome, refs))?
125125 }
126− (_, None) => {}
127126 };
128127 Ok(())
129128 }
@@ -182,7 +181,6 @@ mod async_io { · 52 unmodified lines
182181 index_kind: pack::index::Version::V2,
183182 iteration_mode: pack::data::input::Mode::Verify,
184183 };
185− // TODO: unblock
186184 let outcome = pack::bundle::Bundle::write_to_directory(
187185 futures_lite::io::BlockOn::new(input),
188186 self.directory.take(),
@@ -192,45 +190,32 @@ mod async_io { · 3 unmodified lines
192190 .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
193191
194192 if let Some(directory) = self.refs_directory.take() {
195− let refs = refs.to_owned();
196− blocking::unblock(move || -> io::Result<()> {
197− let assure_dir_exists = |path: &BString| {
198− assert!(!path.starts_with_str("/"), "no ref start with a /, they are relative");
199− let path = directory.join(path.to_path_lossy());
200− std::fs::create_dir_all(path.parent().expect("multi-component path")).map(|_| path)
201− };
202− for r in refs {
203− let (path, content) = match r {
204− Ref::Symbolic { path, target, .. } => {
205− (assure_dir_exists(&path)?, format!("ref: {}", target))
206− }
207− Ref::Peeled { path, tag: object, .. } | Ref::Direct { path, object } => {
208− (assure_dir_exists(&path)?, object.to_string())
209− }
210− };
211− std::fs::write(path, content.as_bytes())?;
212− }
213− Ok(())
214− })
215− .await?;
216− }
217−
218− blocking::unblock({
219− let format_and_output = (self.ctx.format, self.ctx.out.take());
220− let refs = refs.to_owned();
221− move || -> io::Result<()> {
222− match format_and_output {
223− (OutputFormat::Human, Some(mut out)) => drop(print(&mut out, outcome, &refs)),
224− #[cfg(feature = "serde1")]
225− (OutputFormat::Json, Some(mut out)) => {
226− serde_json::to_writer_pretty(&mut out, &JsonOutcome::from_outcome_and_refs(outcome, &refs))?
193+ let assure_dir_exists = |path: &BString| {
194+ assert!(!path.starts_with_str("/"), "no ref start with a /, they are relative");
195+ let path = directory.join(path.to_path_lossy());
196+ std::fs::create_dir_all(path.parent().expect("multi-component path")).map(|_| path)
197+ };
198+ for r in refs {
199+ let (path, content) = match r {
200+ Ref::Symbolic { path, target, .. } => (assure_dir_exists(&path)?, format!("ref: {}", target)),
201+ Ref::Peeled { path, tag: object, .. } | Ref::Direct { path, object } => {
202+ (assure_dir_exists(&path)?, object.to_string())
227203 }
228− (_, None) => {}
229204 };
230− Ok(())
205+ std::fs::write(path, content.as_bytes())?;
231206 }
232− })
233− .await
207+ }
208+
209+ let refs = refs.to_owned();
210+ match self.ctx.format {
211+ OutputFormat::Human => drop(print(&mut self.ctx.out, outcome, &refs)),
212+ #[cfg(feature = "serde1")]
213+ OutputFormat::Json => serde_json::to_writer_pretty(
214+ &mut self.ctx.out,
215+ &JsonOutcome::from_outcome_and_refs(outcome, &refs),
216+ )?,
217+ };
218+ Ok(())
234219 }
235220 }
236221
src/plumbing/lean/main.rs+1−1
@@ -94,7 +94,7 @@ pub fn main() -> Result<()> { · 93 unmodified lines
9494 core::pack::receive::Context {
9595 thread_limit,
9696 format: OutputFormat::Human,
97− out: Some(io::stdout()),
97+ out: io::stdout(),
9898 },
9999 );
100100 #[cfg(feature = "gitoxide-core-blocking-client")]
src/plumbing/pretty/main.rs+1−1
@@ -97,7 +97,7 @@ pub fn main() -> Result<()> { · 96 unmodified lines
9797 core::pack::receive::Context {
9898 thread_limit,
9999 format,
100− out: Some(out),
100+ out,
101101 },
102102 )
103103 },