add derive builder for header
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Michael Zhang 2023-02-10 22:56:46 -06:00
parent db6a185ebb
commit 99f5f8d9b2
7 changed files with 115 additions and 40 deletions

79
Cargo.lock generated
View file

@ -217,6 +217,72 @@ dependencies = [
"syn",
]
[[package]]
name = "darling"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0808e1bd8671fb44a113a14e13497557533369847788fa2ae912b6ebfce9fa8"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "001d80444f28e193f30c2f293455da62dcf9a6b29918a4253152ae2b1de592cb"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b36230598a2d5de7ec1c6f51f72d8a99a9208daff41de2084d06e3fd3ea56685"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "derive_builder"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8"
dependencies = [
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "derive_builder_macro"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
dependencies = [
"derive_builder_core",
"syn",
]
[[package]]
name = "errno"
version = "0.2.8"
@ -238,6 +304,12 @@ dependencies = [
"libc",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "futures"
version = "0.3.25"
@ -372,6 +444,12 @@ dependencies = [
"cxx-build",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "io-lifetimes"
version = "1.0.3"
@ -443,6 +521,7 @@ dependencies = [
"anyhow",
"chrono",
"clap",
"derive_builder",
"futures",
"libc",
"log",

View file

@ -8,6 +8,7 @@ edition = "2021"
anyhow = { version = "1.0.68", features = ["backtrace"] }
chrono = "0.4.23"
clap = { version = "4.0.32", features = ["derive"] }
derive_builder = "0.12.0"
futures = "0.3.25"
libc = "0.2.139"
log = "0.4.17"

View file

@ -9,7 +9,7 @@
overlays = [ fenix.overlays.default ];
};
toolchain = pkgs.fenix.stable;
toolchain = pkgs.fenix.default;
flakePkgs = {
asciinema = pkgs.callPackage ./. { inherit toolchain; };
@ -26,10 +26,11 @@
cargo-watch
cargo-deny
cargo-edit
cargo-expand
# Get the nightly version of rustfmt so we can wrap comments
pkgs.fenix.default.rustfmt
]) ++ (with toolchain; [ cargo rustc rustfmt ]);
]) ++ (with toolchain; [ cargo clippy rustc rustfmt ]);
};
});
}

View file

@ -6,22 +6,39 @@ use std::collections::HashMap;
use serde::ser::{Serialize, SerializeSeq, Serializer};
#[derive(Serialize)]
#[derive(Debug, Builder, Serialize)]
pub struct Header {
pub version: u32,
#[builder(setter(skip))]
pub version: Version,
pub width: u32,
pub height: u32,
#[builder(setter(strip_option), default)]
pub timestamp: Option<u32>,
#[builder(setter(strip_option), default)]
pub duration: Option<f64>,
#[builder(setter(strip_option), default)]
pub idle_time_limit: Option<f64>,
#[builder(setter(into, strip_option), default)]
pub command: Option<String>,
#[builder(setter(into, strip_option), default)]
pub title: Option<String>,
#[builder(setter(into, strip_option), default)]
pub env: Option<HashMap<String, String>>,
#[builder(setter(strip_option), default)]
pub theme: Option<Theme>,
}
#[derive(Serialize)]
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Version(u32);
impl Default for Version {
fn default() -> Self {
Version(2)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Theme {
fg: String,
bg: String,
@ -55,19 +72,3 @@ impl Serialize for Event {
seq.end()
}
}
fn build_header(width: u32, height: u32) -> Header {
Header {
version: 2,
width,
height,
timestamp: None,
duration: None,
idle_time_limit: None,
command: None,
title: None,
env: None,
theme: None,
}
}

View file

@ -1,8 +1,8 @@
//! Setting up a recorder
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
@ -10,7 +10,7 @@ use std::thread;
use anyhow::Result;
use crate::asciicast::Header;
use crate::asciicast::HeaderBuilder;
use super::terminal::Terminal;
@ -29,7 +29,7 @@ pub fn record(opts: RecordOpts) -> Result<()> {
// Write header
// TODO: Clean this up
let header = {
let command_str = format!("{:?}", command);
let command_str = format!("{command:?}");
let env = command
.get_envs()
.into_iter()
@ -40,20 +40,14 @@ pub fn record(opts: RecordOpts) -> Result<()> {
b.to_string_lossy().to_string(),
)
})
.collect();
Header {
version: 2,
width: 30,
height: 30,
.collect::<HashMap<_, _>>();
timestamp: None,
duration: None,
idle_time_limit: None,
command: Some(command_str),
title: None,
env: Some(env),
theme: None,
}
HeaderBuilder::default()
.width(30)
.height(30)
.command(command_str)
.env(env)
.build()?
};
serde_json::to_writer(&file, &header)?;

View file

@ -3,6 +3,8 @@ extern crate anyhow;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate derive_builder;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate tracing;

View file

@ -1,6 +1,3 @@
#[macro_use]
extern crate tracing;
use anyhow::Result;
use clap::{ArgAction, Parser};
use liveterm::client::recorder::{record, RecordOpts};