aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 17115aff3e46629f1cbac710fd4048d5f7ae72a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#![no_std]

#[cfg(feature = "std")]
#[macro_use]
extern crate std;

use core::fmt::{self, Debug, Display};
use core::hash::Hash;

extern crate num_traits;
#[cfg(feature="use-serde")]
extern crate serde;
#[cfg(feature="colors")]
extern crate termion;

#[cfg(feature="use-serde")]
use serde::{Serialize, Deserialize};

mod address;
pub use address::{Address, AddressBase, AddressDisplay};
pub use address::{AddressDisplayUsize, AddressDisplayU64, AddressDisplayU32, AddressDisplayU16};
#[cfg(feature="address-parse")]
pub use address::AddrParse;

mod color;
pub use color::{Colorize, NoColors, YaxColors};

#[cfg(feature="colors")]
pub use color::ColorSettings;

pub mod display;

pub trait DecodeError {
    fn data_exhausted(&self) -> bool;
    fn bad_opcode(&self) -> bool;
    fn bad_operand(&self) -> bool;
}

pub trait Decoder<Inst> where Inst: Sized + Default {
    type Error: DecodeError + Debug + Display;

    fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Inst, Self::Error> {
        let mut inst = Inst::default();
        self.decode_into(&mut inst, bytes).map(|_: ()| inst)
    }

    fn decode_into<T: IntoIterator<Item=u8>>(&self, inst: &mut Inst, bytes: T) -> Result<(), Self::Error>;
}

#[cfg(feature="use-serde")]
pub trait Arch {
    type Address: Address + Debug + Hash + PartialEq + Eq + Serialize + for<'de> Deserialize<'de>;
    type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug + Default;
    type DecodeError: DecodeError + Debug + Display;
    type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
    type Operand;
}

#[cfg(not(feature="use-serde"))]
pub trait Arch {
    type Address: Address + Debug + Hash + PartialEq + Eq;
    type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug + Default;
    type DecodeError: DecodeError + Debug + Display;
    type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
    type Operand;
}

pub trait LengthedInstruction {
    type Unit;
    fn len(&self) -> Self::Unit;
    fn min_size() -> Self::Unit;
}

pub trait Instruction {
    fn well_defined(&self) -> bool;
}

pub trait ShowContextual<Addr, Ctx: ?Sized, Color: Display, T: fmt::Write, Y: YaxColors<Color>> {
    fn contextualize(&self, colors: &Y, address: Addr, context: Option<&Ctx>, out: &mut T) -> fmt::Result;
}

/*
impl <C: ?Sized, T: fmt::Write, U: Colorize<T>> ShowContextual<C, T> for U {
    fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> fmt::Result {
        self.colorize(colors, out)
    }
}
*/