Skip to content

Commit 8a4a0af

Browse files
committed
refactor
- split corpus modules out into files
1 parent 83895f9 commit 8a4a0af

File tree

3 files changed

+95
-97
lines changed

3 files changed

+95
-97
lines changed

gitoxide-core/src/corpus/mod.rs

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -38,100 +38,5 @@ pub(crate) struct Run {
3838
error: Option<String>,
3939
}
4040

41-
pub(crate) mod trace {
42-
use rusqlite::params;
43-
use std::path::Path;
44-
use std::sync::atomic::{AtomicU32, Ordering};
45-
use std::sync::{Arc, Mutex};
46-
use tracing_forest::tree::Tree;
47-
use tracing_subscriber::layer::SubscriberExt;
48-
49-
pub fn override_thread_subscriber(
50-
db_path: impl AsRef<Path>,
51-
) -> anyhow::Result<(tracing::subscriber::DefaultGuard, Arc<AtomicU32>)> {
52-
let current_id = Arc::new(AtomicU32::default());
53-
let processor = tracing_forest::Printer::new().formatter(StoreTreeToDb {
54-
con: Arc::new(Mutex::new(rusqlite::Connection::open(&db_path)?)),
55-
run_id: current_id.clone(),
56-
});
57-
let subscriber = tracing_subscriber::Registry::default().with(tracing_forest::ForestLayer::from(processor));
58-
let guard = tracing::subscriber::set_default(subscriber);
59-
Ok((guard, current_id))
60-
}
61-
62-
pub struct StoreTreeToDb {
63-
pub con: Arc<Mutex<rusqlite::Connection>>,
64-
pub run_id: Arc<AtomicU32>,
65-
}
66-
impl tracing_forest::printer::Formatter for StoreTreeToDb {
67-
type Error = rusqlite::Error;
68-
69-
fn fmt(&self, tree: &Tree) -> Result<String, Self::Error> {
70-
let json = serde_json::to_string_pretty(&tree).expect("serialization to string always works");
71-
let run_id = self.run_id.load(Ordering::SeqCst);
72-
self.con
73-
.lock()
74-
.unwrap()
75-
.execute("UPDATE run SET spans_json = ?1 WHERE id = ?2", params![json, run_id])?;
76-
Ok(String::new())
77-
}
78-
}
79-
}
80-
81-
pub(crate) mod run {
82-
use crate::corpus;
83-
use crate::corpus::{Run, Task};
84-
use std::path::Path;
85-
use std::sync::atomic::AtomicBool;
86-
87-
impl Task {
88-
pub fn perform(
89-
&self,
90-
run: &mut Run,
91-
repo: &Path,
92-
progress: &mut corpus::Progress,
93-
threads: Option<usize>,
94-
should_interrupt: &AtomicBool,
95-
) {
96-
let start = std::time::Instant::now();
97-
if let Err(err) = self.execute.execute(repo, progress, threads, should_interrupt) {
98-
run.error = Some(format!("{err:#?}"))
99-
}
100-
run.duration = start.elapsed();
101-
}
102-
}
103-
104-
/// Note that once runs have been recorded, the implementation must not change anymore to keep it comparable.
105-
/// If changes have be done, rather change the name of the owning task to start a new kind of task.
106-
pub(crate) trait Execute {
107-
fn execute(
108-
&self,
109-
repo: &Path,
110-
progress: &mut corpus::Progress,
111-
threads: Option<usize>,
112-
should_interrupt: &AtomicBool,
113-
) -> anyhow::Result<()>;
114-
}
115-
116-
pub(crate) static ALL: &[Task] = &[Task {
117-
short_name: "OPNR",
118-
description: "open repository (isolated)",
119-
execute_exclusive: false,
120-
execute: &OpenRepo,
121-
}];
122-
123-
struct OpenRepo;
124-
125-
impl Execute for OpenRepo {
126-
fn execute(
127-
&self,
128-
repo: &Path,
129-
_progress: &mut corpus::Progress,
130-
_threads: Option<usize>,
131-
_should_interrupt: &AtomicBool,
132-
) -> anyhow::Result<()> {
133-
gix::open_opts(repo, gix::open::Options::isolated())?;
134-
Ok(())
135-
}
136-
}
137-
}
41+
pub(crate) mod run;
42+
pub(crate) mod trace;

gitoxide-core/src/corpus/run.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::corpus;
2+
use crate::corpus::{Run, Task};
3+
use std::path::Path;
4+
use std::sync::atomic::AtomicBool;
5+
6+
impl Task {
7+
pub fn perform(
8+
&self,
9+
run: &mut Run,
10+
repo: &Path,
11+
progress: &mut corpus::Progress,
12+
threads: Option<usize>,
13+
should_interrupt: &AtomicBool,
14+
) {
15+
let start = std::time::Instant::now();
16+
if let Err(err) = self.execute.execute(repo, progress, threads, should_interrupt) {
17+
run.error = Some(format!("{err:#?}"))
18+
}
19+
run.duration = start.elapsed();
20+
}
21+
}
22+
23+
/// Note that once runs have been recorded, the implementation must not change anymore to keep it comparable.
24+
/// If changes have be done, rather change the name of the owning task to start a new kind of task.
25+
pub(crate) trait Execute {
26+
fn execute(
27+
&self,
28+
repo: &Path,
29+
progress: &mut corpus::Progress,
30+
threads: Option<usize>,
31+
should_interrupt: &AtomicBool,
32+
) -> anyhow::Result<()>;
33+
}
34+
35+
pub(crate) static ALL: &[Task] = &[Task {
36+
short_name: "OPNR",
37+
description: "open repository (isolated)",
38+
execute_exclusive: false,
39+
execute: &OpenRepo,
40+
}];
41+
42+
struct OpenRepo;
43+
44+
impl Execute for OpenRepo {
45+
fn execute(
46+
&self,
47+
repo: &Path,
48+
_progress: &mut corpus::Progress,
49+
_threads: Option<usize>,
50+
_should_interrupt: &AtomicBool,
51+
) -> anyhow::Result<()> {
52+
gix::open_opts(repo, gix::open::Options::isolated())?;
53+
Ok(())
54+
}
55+
}

gitoxide-core/src/corpus/trace.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use rusqlite::params;
2+
use std::path::Path;
3+
use std::sync::atomic::{AtomicU32, Ordering};
4+
use std::sync::{Arc, Mutex};
5+
use tracing_forest::tree::Tree;
6+
use tracing_subscriber::layer::SubscriberExt;
7+
8+
pub fn override_thread_subscriber(
9+
db_path: impl AsRef<Path>,
10+
) -> anyhow::Result<(tracing::subscriber::DefaultGuard, Arc<AtomicU32>)> {
11+
let current_id = Arc::new(AtomicU32::default());
12+
let processor = tracing_forest::Printer::new().formatter(StoreTreeToDb {
13+
con: Arc::new(Mutex::new(rusqlite::Connection::open(&db_path)?)),
14+
run_id: current_id.clone(),
15+
});
16+
let subscriber = tracing_subscriber::Registry::default().with(tracing_forest::ForestLayer::from(processor));
17+
let guard = tracing::subscriber::set_default(subscriber);
18+
Ok((guard, current_id))
19+
}
20+
21+
pub struct StoreTreeToDb {
22+
pub con: Arc<Mutex<rusqlite::Connection>>,
23+
pub run_id: Arc<AtomicU32>,
24+
}
25+
26+
impl tracing_forest::printer::Formatter for StoreTreeToDb {
27+
type Error = rusqlite::Error;
28+
29+
fn fmt(&self, tree: &Tree) -> Result<String, Self::Error> {
30+
let json = serde_json::to_string_pretty(&tree).expect("serialization to string always works");
31+
let run_id = self.run_id.load(Ordering::SeqCst);
32+
self.con
33+
.lock()
34+
.unwrap()
35+
.execute("UPDATE run SET spans_json = ?1 WHERE id = ?2", params![json, run_id])?;
36+
Ok(String::new())
37+
}
38+
}

0 commit comments

Comments
 (0)