Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/spec -- non_exhaustive enums, testStepId #6

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/output/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,37 @@ impl StdoutWriter {
}

pub struct JsonEmitter {
sequence_no: Arc<atomic::AtomicU64>,
timezone: chrono_tz::Tz,
writer: WriterType,
seqno: Arc<atomic::AtomicU64>,
}

impl JsonEmitter {
pub(crate) fn new(timezone: chrono_tz::Tz, writer: WriterType) -> Self {
JsonEmitter {
timezone,
writer,
sequence_no: Arc::new(atomic::AtomicU64::new(0)),
seqno: Arc::new(atomic::AtomicU64::new(0)),
}
}

fn serialize_artifact(&self, object: &spec::RootArtifact) -> serde_json::Value {
fn serialize_artifact(&self, object: &spec::RootImpl) -> serde_json::Value {
let now = chrono::Local::now();
let now_tz = now.with_timezone(&self.timezone);
let out_artifact = spec::Root {
let root = spec::Root {
artifact: object.clone(),
timestamp: now_tz,
seqno: self.next_sequence_no(),
};
serde_json::json!(out_artifact)
serde_json::json!(root)
}

fn next_sequence_no(&self) -> u64 {
self.sequence_no.fetch_add(1, atomic::Ordering::SeqCst);
self.sequence_no.load(atomic::Ordering::SeqCst)
self.seqno.fetch_add(1, atomic::Ordering::SeqCst);
self.seqno.load(atomic::Ordering::SeqCst)
}

pub async fn emit(&self, object: &spec::RootArtifact) -> Result<(), WriterError> {
pub async fn emit(&self, object: &spec::RootImpl) -> Result<(), WriterError> {
let serialized = self.serialize_artifact(object);
match self.writer {
WriterType::File(ref file) => file.write(&serialized.to_string()).await?,
Expand All @@ -132,8 +132,6 @@ mod tests {
use serde_json::json;

use super::*;
use crate::output as tv;
use tv::run::SchemaVersion;

#[tokio::test]
async fn test_emit_using_buffer_writer() -> Result<()> {
Expand All @@ -149,8 +147,11 @@ mod tests {
let writer = BufferWriter::new(buffer.clone());
let emitter = JsonEmitter::new(chrono_tz::UTC, WriterType::Buffer(writer));

let version = SchemaVersion::new();
emitter.emit(&version.to_artifact()).await?;
emitter
.emit(&spec::RootImpl::SchemaVersion(
spec::SchemaVersion::default(),
))
.await?;

let deserialized = serde_json::from_str::<serde_json::Value>(
buffer.lock().await.first().ok_or(anyhow!("no outputs"))?,
Expand Down Expand Up @@ -180,9 +181,10 @@ mod tests {
let buffer = Arc::new(Mutex::new(vec![]));
let writer = BufferWriter::new(buffer.clone());
let emitter = JsonEmitter::new(chrono_tz::UTC, WriterType::Buffer(writer));
let version = SchemaVersion::new();
emitter.emit(&version.to_artifact()).await?;
emitter.emit(&version.to_artifact()).await?;

let version = spec::RootImpl::SchemaVersion(spec::SchemaVersion::default());
emitter.emit(&version).await?;
emitter.emit(&version).await?;

let deserialized = serde_json::from_str::<serde_json::Value>(
buffer.lock().await.first().ok_or(anyhow!("no outputs"))?,
Expand Down
Loading
Loading