diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-03 09:24:42 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-03 09:24:42 -0500 |
commit | 39f1b8bec3891b3fcc7f60aea86e2ab2f8c3ea8a (patch) | |
tree | 876a55e466ff9a276833016093a74e9b9dea1edc /src/env.rs | |
parent | 3f98fd297b52fa2211363204283360d52a618e45 (diff) | |
download | nbsh-39f1b8bec3891b3fcc7f60aea86e2ab2f8c3ea8a.tar.gz nbsh-39f1b8bec3891b3fcc7f60aea86e2ab2f8c3ea8a.zip |
use real serialization
Diffstat (limited to 'src/env.rs')
-rw-r--r-- | src/env.rs | 78 |
1 files changed, 70 insertions, 8 deletions
@@ -1,23 +1,85 @@ +use serde::Deserialize as _; use std::os::unix::process::ExitStatusExt as _; -pub struct Env { +#[derive(serde::Serialize, serde::Deserialize)] +pub enum Env { + V0(V0), +} + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct V0 { + pipeline: Option<String>, + #[serde( + serialize_with = "serialize_status", + deserialize_with = "deserialize_status" + )] latest_status: async_std::process::ExitStatus, } impl Env { - pub fn new(code: i32) -> Self { - Self { - latest_status: async_std::process::ExitStatus::from_raw( - code << 8, - ), + pub fn new() -> Self { + Self::V0(V0 { + pipeline: None, + latest_status: std::process::ExitStatus::from_raw(0), + }) + } + + pub fn set_pipeline(&mut self, pipeline: String) { + match self { + Self::V0(env) => { + env.pipeline = Some(pipeline); + } } } pub fn set_status(&mut self, status: async_std::process::ExitStatus) { - self.latest_status = status; + match self { + Self::V0(env) => { + env.latest_status = status; + } + } + } + + pub fn pipeline(&self) -> Option<&str> { + match self { + Self::V0(env) => env.pipeline.as_deref(), + } } pub fn latest_status(&self) -> &async_std::process::ExitStatus { - &self.latest_status + match self { + Self::V0(env) => &env.latest_status, + } } + + pub fn as_bytes(&self) -> Vec<u8> { + bincode::serialize(self).unwrap() + } + + pub fn from_bytes(bytes: &[u8]) -> Self { + bincode::deserialize(bytes).unwrap() + } +} + +#[allow(clippy::trivially_copy_pass_by_ref)] +fn serialize_status<S>( + status: &std::process::ExitStatus, + s: S, +) -> Result<S::Ok, S::Error> +where + S: serde::Serializer, +{ + let code: u16 = status.code().unwrap_or(0).try_into().unwrap(); + let signal: u16 = status.signal().unwrap_or(0).try_into().unwrap(); + s.serialize_u16((code << 8) | signal) +} + +fn deserialize_status<'de, D>( + d: D, +) -> Result<std::process::ExitStatus, D::Error> +where + D: serde::Deserializer<'de>, +{ + let status = u16::deserialize(d)?; + Ok(std::process::ExitStatus::from_raw(i32::from(status))) } |