Skip to content

Commit 88da779

Browse files
committed
Introduce NoOpHandler to simplify LocalHandler
1 parent 23fdcc5 commit 88da779

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

lib/src/internal/local_handler.rs

+17-32
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,40 @@ use std::env;
22
use std::fs::File;
33
use std::io::{Write, Error};
44

5+
// use crate::internal::noop_handler::NoOpHandler;
56
use crate::internal::{LibHandler};
67

78
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
89

910
pub struct LocalHandler {
10-
maybe_writer: Option<File>
11+
writer: File
1112
}
1213

1314
impl LocalHandler {
14-
pub fn new() -> Self {
15-
let filename = match env::var(LOCAL_OUTPUT) {
16-
Err(_) => return LocalHandler{ maybe_writer: None },
17-
Ok(s) => s
18-
};
15+
pub fn new() -> Option<Self> {
16+
let filename = env::var(LOCAL_OUTPUT).ok()?;
1917

2018
let create_result = File::create(&filename);
21-
if let Ok(f) = create_result {
22-
// Disabling buffering by setting capacity to 0 for now
23-
// Inefficient, but ensures that no buffered bytes are abandoned
24-
// for a LocalHandler instance that does not get Drop'ed
25-
// Seems like LocalHandler gets bound to a reference with
26-
// a 'static lifetime.
27-
LocalHandler{
28-
maybe_writer: Some(f)
29-
}
19+
if let Ok(writer) = create_result {
20+
Some(LocalHandler{ writer })
3021
} else {
31-
eprintln!("Unable to write to '{}' - {}", filename.as_str(), create_result.unwrap_err());
32-
LocalHandler {
33-
maybe_writer: None
34-
}
22+
eprintln!("Unable to write to '{}' - {}", filename.as_str(), create_result.unwrap_err());
23+
None
3524
}
3625
}
3726
}
3827

3928
impl LibHandler for LocalHandler {
29+
4030
fn output(&self, value: &str) -> Result<(), Error> {
41-
match &self.maybe_writer {
42-
Some(writer_ref) => {
43-
let mut writer_mut = writer_ref;
44-
// The compact Display impl (selected using `{}`) of `serde_json::Value` contains no newlines,
45-
// hence we are outputing valid JSONL format here.
46-
// Using the `{:#}` format specifier may results in extra newlines and indentation.
47-
// See https://docs.rs/serde_json/latest/serde_json/enum.Value.html#impl-Display-for-Value.
48-
writeln!(writer_mut, "{}", value)?;
49-
writer_mut.flush()?;
50-
Ok(())
51-
},
52-
None => Ok(())
53-
}
31+
// The compact Display impl (selected using `{}`) of `serde_json::Value` contains no newlines,
32+
// hence we are outputing valid JSONL format here.
33+
// Using the `{:#}` format specifier may results in extra newlines and indentation.
34+
// See https://docs.rs/serde_json/latest/serde_json/enum.Value.html#impl-Display-for-Value.
35+
let mut writer_mut = & self.writer;
36+
writeln!(writer_mut, "{}", value)?;
37+
writer_mut.flush()?;
38+
Ok(())
5439
}
5540

5641
fn random(&self) -> u64 {

lib/src/internal/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use rustc_version_runtime::version;
33
use serde::Serialize;
44
use std::io::{Error};
55
use local_handler::LocalHandler;
6-
use voidstar_handler::{VoidstarHandler};
6+
use voidstar_handler::VoidstarHandler;
7+
use noop_handler::NoOpHandler;
78

89
mod local_handler;
10+
mod noop_handler;
911
mod voidstar_handler;
1012

1113
#[derive(Serialize, Debug)]
@@ -35,7 +37,11 @@ const SDK_VERSION: &str = "0.1.2";
3537
pub(crate) static LIB_HANDLER: Lazy<Box<dyn LibHandler + Sync + Send>> = Lazy::new(|| {
3638
let handler: Box<dyn LibHandler + Sync + Send> = match VoidstarHandler::try_load() {
3739
Ok(handler) => Box::new(handler),
38-
Err(_) => Box::new(LocalHandler::new()),
40+
Err(_) => match LocalHandler::new() {
41+
Some(h) => Box::new(h),
42+
None => Box::new(NoOpHandler::new())
43+
}
44+
3945
};
4046
let s = serde_json::to_string(&sdk_info()).unwrap_or("{}".to_owned());
4147
let _ = handler.output(s.as_str());

lib/src/internal/noop_handler.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::internal::{LibHandler};
2+
use std::io::Error;
3+
4+
pub struct NoOpHandler {}
5+
6+
impl NoOpHandler {
7+
pub fn new() -> Self {
8+
NoOpHandler{}
9+
}
10+
}
11+
12+
impl LibHandler for NoOpHandler {
13+
fn output(&self, _value: &str) -> Result<(), Error> {
14+
Ok(())
15+
}
16+
17+
fn random(&self) -> u64 {
18+
rand::random::<u64>()
19+
}
20+
}

0 commit comments

Comments
 (0)