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
17pub struct Context<W> {17pub struct Context<W> {
18 pub thread_limit: Option<usize>,18 pub thread_limit: Option<usize>,
19 pub format: OutputFormat,19 pub format: OutputFormat,
20 pub out: Option<W>,20 pub out: W,
21}21}
22 22
23struct CloneDelegate<W> {23struct CloneDelegate<W> {
@@ -117,13 +117,12 @@ mod blocking_io { · 93 unmodified lines
117 }117 }
118 }118 }
119 119
120 match (self.ctx.format, self.ctx.out.as_mut()) {120 match self.ctx.format {
121 (OutputFormat::Human, Some(out)) => drop(print(out, outcome, refs)),121 OutputFormat::Human => drop(print(&mut self.ctx.out, outcome, refs)),
122 #[cfg(feature = "serde1")]122 #[cfg(feature = "serde1")]
123 (OutputFormat::Json, Some(out)) => {123 OutputFormat::Json => {
124 serde_json::to_writer_pretty(out, &JsonOutcome::from_outcome_and_refs(outcome, refs))?124 serde_json::to_writer_pretty(&mut self.ctx.out, &JsonOutcome::from_outcome_and_refs(outcome, refs))?
125 }125 }
126 (_, None) => {}
127 };126 };
128 Ok(())127 Ok(())
129 }128 }
@@ -182,7 +181,6 @@ mod async_io { · 52 unmodified lines
182 index_kind: pack::index::Version::V2,181 index_kind: pack::index::Version::V2,
183 iteration_mode: pack::data::input::Mode::Verify,182 iteration_mode: pack::data::input::Mode::Verify,
184 };183 };
185 // TODO: unblock
186 let outcome = pack::bundle::Bundle::write_to_directory(184 let outcome = pack::bundle::Bundle::write_to_directory(
187 futures_lite::io::BlockOn::new(input),185 futures_lite::io::BlockOn::new(input),
188 self.directory.take(),186 self.directory.take(),
@@ -192,45 +190,32 @@ mod async_io { · 3 unmodified lines
192 .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;190 .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
193 191
194 if let Some(directory) = self.refs_directory.take() {192 if let Some(directory) = self.refs_directory.take() {
195 let refs = refs.to_owned();193 let assure_dir_exists = |path: &BString| {
196 blocking::unblock(move || -> io::Result<()> {194 assert!(!path.starts_with_str("/"), "no ref start with a /, they are relative");
197 let assure_dir_exists = |path: &BString| {195 let path = directory.join(path.to_path_lossy());
198 assert!(!path.starts_with_str("/"), "no ref start with a /, they are relative");196 std::fs::create_dir_all(path.parent().expect("multi-component path")).map(|_| path)
199 let path = directory.join(path.to_path_lossy());197 };
200 std::fs::create_dir_all(path.parent().expect("multi-component path")).map(|_| path)198 for r in refs {
201 };199 let (path, content) = match r {
202 for r in refs {200 Ref::Symbolic { path, target, .. } => (assure_dir_exists(&path)?, format!("ref: {}", target)),
203 let (path, content) = match r {201 Ref::Peeled { path, tag: object, .. } | Ref::Direct { path, object } => {
204 Ref::Symbolic { path, target, .. } => {202 (assure_dir_exists(&path)?, object.to_string())
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))?
227 }203 }
228 (_, None) => {}
229 };204 };
230 Ok(())205 std::fs::write(path, content.as_bytes())?;
231 }206 }
232 })207 }
233 .await208
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(())
234 }219 }
235 }220 }
236 221
src/plumbing/lean/main.rs+1−1
@@ -94,7 +94,7 @@ pub fn main() -> Result<()> { · 93 unmodified lines
94 core::pack::receive::Context {94 core::pack::receive::Context {
95 thread_limit,95 thread_limit,
96 format: OutputFormat::Human,96 format: OutputFormat::Human,
97 out: Some(io::stdout()),97 out: io::stdout(),
98 },98 },
99 );99 );
100 #[cfg(feature = "gitoxide-core-blocking-client")]100 #[cfg(feature = "gitoxide-core-blocking-client")]
src/plumbing/pretty/main.rs+1−1
@@ -97,7 +97,7 @@ pub fn main() -> Result<()> { · 96 unmodified lines
97 core::pack::receive::Context {97 core::pack::receive::Context {
98 thread_limit,98 thread_limit,
99 format,99 format,
100 out: Some(out),100 out,
101 },101 },
102 )102 )
103 },103 },