use core::fmt;
use primitives::script::Builder as PrimitiveBuilder;
use super::{Error, Script, ScriptBuf};
use crate::locktime::absolute;
use crate::relative;
#[derive(PartialEq, Eq, Clone)]
pub struct Builder<T>(PrimitiveBuilder<T>);
impl<T> Builder<T> {
#[inline]
pub const fn new() -> Self {
Self(PrimitiveBuilder::new())
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self(PrimitiveBuilder::with_capacity(capacity))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn push_int(self, n: i32) -> Result<Self, Error> {
self.0.push_int(n).map(Self).map_err(Into::into)
}
pub fn push_int_unchecked(self, n: i64) -> Self {
Self(self.0.push_int_unchecked(n))
}
pub(in crate::blockdata) fn push_int_non_minimal(self, data: i64) -> Self {
Self(self.0.push_int_non_minimal(data))
}
pub fn push_slice<D: AsRef<[u8]>>(self, data: D) -> Self {
Self(self.0.push_slice(data))
}
pub fn push_slice_non_minimal<D: AsRef<[u8]>>(self, data: D) -> Self {
Self(self.0.push_slice_non_minimal(data))
}
pub fn push_opcode(self, data: crate::opcodes::Opcode) -> Self {
Self(self.0.push_opcode(data))
}
pub fn push_verify(self) -> Self {
Self(self.0.push_verify())
}
pub fn push_lock_time(self, lock_time: absolute::LockTime) -> Self {
self.push_int_unchecked(lock_time.to_consensus_u32().into())
}
pub fn push_relative_lock_time(self, lock_time: relative::LockTime) -> Self {
self.push_int_unchecked(lock_time.to_consensus_u32().into())
}
pub fn into_script(self) -> ScriptBuf<T> {
self.0.into_script()
}
pub fn into_bytes(self) -> crate::prelude::Vec<u8> {
self.0.into_bytes()
}
pub fn as_script(&self) -> &Script<T> {
self.0.as_script()
}
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}
impl<T> Default for Builder<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> From<crate::prelude::Vec<u8>> for Builder<T> {
fn from(v: crate::prelude::Vec<u8>) -> Self {
Self(PrimitiveBuilder::from(v))
}
}
impl<T> fmt::Display for Builder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<T> fmt::Debug for Builder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}