add integer based on plutus core manual information
This commit is contained in:
parent
b345afd12f
commit
2e130ac5f0
|
@ -20,6 +20,14 @@ impl Encode for u8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encode for isize {
|
||||||
|
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||||
|
e.integer(*self)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Encode for char {
|
impl Encode for char {
|
||||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||||
e.char(*self)?;
|
e.char(*self)?;
|
||||||
|
|
|
@ -51,7 +51,13 @@ impl Encoder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bytes(&mut self, arr: &[u8]) -> Result<&mut Self, String> {
|
pub fn bytes(&mut self, x: &[u8]) -> Result<&mut Self, String> {
|
||||||
|
// use filler to write current buffer so bits used gets reset
|
||||||
|
self.filler();
|
||||||
|
self.byte_array(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn byte_array(&mut self, arr: &[u8]) -> Result<&mut Self, String> {
|
||||||
if self.used_bits != 0 {
|
if self.used_bits != 0 {
|
||||||
return Err("Buffer is not byte aligned".to_string());
|
return Err("Buffer is not byte aligned".to_string());
|
||||||
}
|
}
|
||||||
|
@ -59,8 +65,13 @@ impl Encoder {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn integer(&mut self, i: isize) -> Result<&mut Self, String> {
|
||||||
|
self.word(i);
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn char(&mut self, c: char) -> Result<&mut Self, String> {
|
pub fn char(&mut self, c: char) -> Result<&mut Self, String> {
|
||||||
self.word(c as u32);
|
self.word(c as isize);
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +132,7 @@ impl Encoder {
|
||||||
self.write_blk(arr, src_ptr);
|
self.write_blk(arr, src_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn word(&mut self, c: u32) {
|
fn word(&mut self, c: isize) {
|
||||||
loop {
|
loop {
|
||||||
let mut w = (c & 127) as u8;
|
let mut w = (c & 127) as u8;
|
||||||
let c = c >> 7;
|
let c = c >> 7;
|
||||||
|
|
|
@ -74,7 +74,7 @@ pub fn safe_encode_bits(num_bits: u32, byte: u8, e: &mut Encoder) -> Result<(),
|
||||||
pub enum Constant {
|
pub enum Constant {
|
||||||
// TODO: figure out the right size for this
|
// TODO: figure out the right size for this
|
||||||
// tag: 0
|
// tag: 0
|
||||||
Integer(i64),
|
Integer(isize),
|
||||||
// tag: 1
|
// tag: 1
|
||||||
ByteString(Vec<u8>),
|
ByteString(Vec<u8>),
|
||||||
// tag: 2
|
// tag: 2
|
||||||
|
@ -159,7 +159,10 @@ impl Encode for Term {
|
||||||
impl Encode for &Constant {
|
impl Encode for &Constant {
|
||||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||||
match self {
|
match self {
|
||||||
Constant::Integer(_) => todo!(),
|
Constant::Integer(i) => {
|
||||||
|
encode_constant(0, e)?;
|
||||||
|
i.encode(e)?;
|
||||||
|
}
|
||||||
Constant::ByteString(bytes) => {
|
Constant::ByteString(bytes) => {
|
||||||
encode_constant(1, e)?;
|
encode_constant(1, e)?;
|
||||||
bytes.encode(e)?;
|
bytes.encode(e)?;
|
||||||
|
|
Loading…
Reference in New Issue