diff options
author | iximeow <me@iximeow.net> | 2020-01-15 01:14:50 -0800 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2020-01-15 01:14:50 -0800 |
commit | 894cba0dde913d86472430eae8b7ba0353711b4e (patch) | |
tree | 3c0f4eea33a8c0c1db60a1d843cd72702a7be633 /src/lib.rs | |
parent | f0869a1e2fbbdc50f6b020f088da020463e177da (diff) |
no_std!!
this makes yaxpeax-arch no_std. generally nothing has changed w.r.t
downstream crates, but a lot to do with colorization has been moved
tweaked to make it no_std-friendly (specifically, allowing `termion` to
be an optional dependency)
this also makes address parsing optional, in the hopes that decode-only
use cases don't need to involve as much machinery when building.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 498 |
1 files changed, 22 insertions, 476 deletions
@@ -1,163 +1,31 @@ -extern crate num_traits; -extern crate termion; -#[cfg(feature="use-serde")] -extern crate serde; - -use std::str::FromStr; -use std::hash::Hash; +#![no_std] -use std::fmt::{Debug, Display, Formatter}; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; -use std::ops::{Add, Sub, AddAssign, SubAssign}; - -use num_traits::identities; -use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub}; - -use termion::color; +use core::fmt::{self, Debug, Display}; +use core::hash::Hash; +extern crate num_traits; #[cfg(feature="use-serde")] -use serde::{Serialize, Deserialize}; - -pub mod display; - // This is pretty wonk.. -pub trait AddressDisplay { - fn stringy(&self) -> String; -} - -/* - * TODO: this should be FromStr. - * that would require newtyping address primitives, though - * - * this is not out of the question, BUT is way more work than - * i want to put in right now - * - * this is one of those "clean it up later" situations - */ -pub trait AddrParse: Sized { - type Err; - fn parse_from(s: &str) -> Result<Self, Self::Err>; -} +extern crate serde; +#[cfg(feature="colors")] +extern crate termion; #[cfg(feature="use-serde")] -pub trait Address where Self: - Serialize + for<'de> Deserialize<'de> + - Debug + Display + AddressDisplay + - Copy + Clone + Sized + Hash + - Ord + Eq + PartialEq + Bounded + - Add<Output=Self> + Sub<Output=Self> + - AddAssign + SubAssign + - WrappingAdd + WrappingSub + - CheckedAdd + CheckedSub + - AddrParse + - Hash + - identities::One + identities::Zero { - fn to_linear(&self) -> usize; - -} -#[cfg(not(feature="use-serde"))] -pub trait Address where Self: - Debug + Display + AddressDisplay + - Copy + Clone + Sized + Hash + - Ord + Eq + PartialEq + Bounded + - Add<Output=Self> + Sub<Output=Self> + - AddAssign + SubAssign + - WrappingAdd + WrappingSub + - CheckedAdd + CheckedSub + - AddrParse + - Hash + - identities::One + identities::Zero { - fn to_linear(&self) -> usize; - -} -/* -impl <T> Address for T where T: Sized + Ord + Add<Output=Self> + From<u16> + Into<usize> { - fn to_linear(&self) -> usize { *self.into() } -} -*/ - -impl AddrParse for usize { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result<Self, Self::Err> { - if s.starts_with("0x") { - usize::from_str_radix(&s[2..], 16) - } else { - usize::from_str(s) - } - } -} - -impl AddressDisplay for usize { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u64 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result<Self, Self::Err> { - if s.starts_with("0x") { - u64::from_str_radix(&s[2..], 16) - } else { - u64::from_str(s) - } - } -} - -impl AddressDisplay for u64 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u32 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result<Self, Self::Err> { - if s.starts_with("0x") { - u32::from_str_radix(&s[2..], 16) - } else { - u32::from_str(s) - } - } -} - -impl AddressDisplay for u32 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} - -impl AddrParse for u16 { - type Err = std::num::ParseIntError; - fn parse_from(s: &str) -> Result<Self, Self::Err> { - if s.starts_with("0x") { - u16::from_str_radix(&s[2..], 16) - } else { - u16::from_str(s) - } - } -} - -impl AddressDisplay for u16 { - fn stringy(&self) -> String { - format!("{:#x}", self) - } -} +use serde::{Serialize, Deserialize}; -impl Address for u16 { - fn to_linear(&self) -> usize { *self as usize } -} +mod address; +pub use address::{Address, AddressBase}; -impl Address for u32 { - fn to_linear(&self) -> usize { *self as usize } -} +mod color; +pub use color::{Colorize, NoColors, YaxColors}; -impl Address for u64 { - fn to_linear(&self) -> usize { *self as usize } -} +#[cfg(feature="colors")] +pub use color::ColorSettings; -impl Address for usize { - fn to_linear(&self) -> usize { *self } -} +pub mod display; pub trait DecodeError { fn data_exhausted(&self) -> bool; @@ -204,335 +72,13 @@ pub trait Instruction { fn well_defined(&self) -> bool; } -#[cfg(feature="use-serde")] -impl Serialize for ColorSettings { - fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { - use serde::ser::SerializeStruct; - let s = serializer.serialize_struct("ColorSettings", 0)?; - s.end() - } -} - -#[allow(dead_code)] -pub struct ColorSettings { - arithmetic: color::Fg<&'static dyn color::Color>, - stack: color::Fg<&'static dyn color::Color>, - nop: color::Fg<&'static dyn color::Color>, - stop: color::Fg<&'static dyn color::Color>, - control: color::Fg<&'static dyn color::Color>, - data: color::Fg<&'static dyn color::Color>, - comparison: color::Fg<&'static dyn color::Color>, - invalid: color::Fg<&'static dyn color::Color>, - platform: color::Fg<&'static dyn color::Color>, - misc: color::Fg<&'static dyn color::Color>, - - register: color::Fg<&'static dyn color::Color>, - program_counter: color::Fg<&'static dyn color::Color>, - - number: color::Fg<&'static dyn color::Color>, - zero: color::Fg<&'static dyn color::Color>, - one: color::Fg<&'static dyn color::Color>, - minus_one: color::Fg<&'static dyn color::Color>, - - function: color::Fg<&'static dyn color::Color>, - symbol: color::Fg<&'static dyn color::Color>, - address: color::Fg<&'static dyn color::Color> -} - -impl Default for ColorSettings { - fn default() -> ColorSettings { - ColorSettings { - /* - arithmetic: color::Fg(&color::LightYellow), - stack: color::Fg(&color::Magenta), - nop: color::Fg(&color::Blue), - stop: color::Fg(&color::LightRed), - control: color::Fg(&color::Green), - data: color::Fg(&color::LightMagenta), - comparison: color::Fg(&color::Yellow), - invalid: color::Fg(&color::Red), - platform: color::Fg(&color::Cyan), - misc: color::Fg(&color::LightCyan), - */ - arithmetic: color::Fg(&color::Yellow), - stack: color::Fg(&color::Magenta), - nop: color::Fg(&color::Blue), - stop: color::Fg(&color::Red), - control: color::Fg(&color::Red), - data: color::Fg(&color::Yellow), - comparison: color::Fg(&color::Yellow), - invalid: color::Fg(&color::Red), - platform: color::Fg(&color::LightBlue), - misc: color::Fg(&color::LightCyan), - - register: color::Fg(&color::Cyan), - program_counter: color::Fg(&color::Red), - - number: color::Fg(&color::White), - zero: color::Fg(&color::White), - one: color::Fg(&color::White), - minus_one: color::Fg(&color::White), - - function: color::Fg(&color::LightGreen), - symbol: color::Fg(&color::LightGreen), - address: color::Fg(&color::Green) - } - } -} - -pub trait YaxColors { - fn arithmetic_op<T: Display>(&self, t: T) -> Colored<T>; - fn stack_op<T: Display>(&self, t: T) -> Colored<T>; - fn nop_op<T: Display>(&self, t: T) -> Colored<T>; - fn stop_op<T: Display>(&self, t: T) -> Colored<T>; - fn control_flow_op<T: Display>(&self, t: T) -> Colored<T>; - fn data_op<T: Display>(&self, t: T) -> Colored<T>; - fn comparison_op<T: Display>(&self, t: T) -> Colored<T>; - fn invalid_op<T: Display>(&self, t: T) -> Colored<T>; - fn platform_op<T: Display>(&self, t: T) -> Colored<T>; - fn misc_op<T: Display>(&self, t: T) -> Colored<T>; - - fn register<T: Display>(&self, t: T) -> Colored<T>; - fn program_counter<T: Display>(&self, t: T) -> Colored<T>; - fn number<T: Display>(&self, t: T) -> Colored<T>; - fn zero<T: Display>(&self, t: T) -> Colored<T>; - fn one<T: Display>(&self, t: T) -> Colored<T>; - fn minus_one<T: Display>(&self, t: T) -> Colored<T>; - fn address<T: Display>(&self, t: T) -> Colored<T>; - fn symbol<T: Display>(&self, t: T) -> Colored<T>; - fn function<T: Display>(&self, t: T) -> Colored<T>; -} - -impl YaxColors for ColorSettings { - fn arithmetic_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.arithmetic) - } - fn stack_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.stack) - } - fn nop_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.nop) - } - fn stop_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.stop) - } - fn control_flow_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.control) - } - fn data_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.data) - } - fn comparison_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.comparison) - } - fn invalid_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.invalid) - } - fn misc_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.misc) - } - fn platform_op<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.platform) - } - - fn register<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.register) - } - fn program_counter<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.program_counter) - } - fn number<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.number) - } - fn zero<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.zero) - } - fn one<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.one) - } - fn minus_one<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.minus_one) - } - fn address<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.address) - } - fn symbol<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.symbol) - } - fn function<T: Display>(&self, t: T) -> Colored<T> { - Colored::Color(t, self.function) - } -} - -impl <'a> YaxColors for Option<&'a ColorSettings> { - fn arithmetic_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.arithmetic_op(t) } - None => { Colored::Just(t) } - } - } - fn stack_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.stack_op(t) } - None => { Colored::Just(t) } - } - } - fn nop_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.nop_op(t) } - None => { Colored::Just(t) } - } - } - fn stop_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.stop_op(t) } - None => { Colored::Just(t) } - } - } - fn control_flow_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.control_flow_op(t) } - None => { Colored::Just(t) } - } - } - fn data_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.data_op(t) } - None => { Colored::Just(t) } - } - } - fn comparison_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.comparison_op(t) } - None => { Colored::Just(t) } - } - } - fn invalid_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.invalid_op(t) } - None => { Colored::Just(t) } - } - } - fn misc_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.misc_op(t) } - None => { Colored::Just(t) } - } - } - fn platform_op<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.platform_op(t) } - None => { Colored::Just(t) } - } - } - - fn register<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.register(t) } - None => { Colored::Just(t) } - } - } - fn program_counter<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.program_counter(t) } - None => { Colored::Just(t) } - } - } - fn number<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.number(t) } - None => { Colored::Just(t) } - } - } - fn zero<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.zero(t) } - None => { Colored::Just(t) } - } - } - fn one<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.one(t) } - None => { Colored::Just(t) } - } - } - fn minus_one<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.minus_one(t) } - None => { Colored::Just(t) } - } - } - fn address<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.address(t) } - None => { Colored::Just(t) } - } - } - fn symbol<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.symbol(t) } - None => { Colored::Just(t) } - } - } - fn function<T: Display>(&self, t: T) -> Colored<T> { - match self { - Some(colors) => { colors.function(t) } - None => { Colored::Just(t) } - } - } -} - -pub enum Colored<T: Display> { - Color(T, color::Fg<&'static dyn color::Color>), - Just(T) -} - -impl <T: Display> Display for Colored<T> { - fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { - match self { - Colored::Color(t, color) => { - write!(fmt, "{}{}{}", color, t, color::Fg(color::Reset)) - }, - Colored::Just(t) => { - write!(fmt, "{}", t) - } - } - } -} - -/* - * can this be a derivable trait or something? - */ -/* -impl <T: Colorize> Display for T { - fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { - self.colorize(None, fmt) - } -} -*/ - -pub trait Colorize<T: std::fmt::Write> { - fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result; -} - -/* - * and make this auto-derive from a ShowContextual impl? - */ -/* -impl <T, U> Colorize for T where T: ShowContextual<Ctx=U> { - fn colorize(&self, colors: Option<&ColorSettings>, fmt: &mut Formatter) -> std::fmt::Result { - self.contextualize(colors, None, fmt) - } -} -*/ - -pub trait ShowContextual<Addr, Ctx: ?Sized, T: std::fmt::Write> { - fn contextualize(&self, colors: Option<&ColorSettings>, address: Addr, context: Option<&Ctx>, out: &mut T) -> std::fmt::Result; +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: std::fmt::Write, U: Colorize<T>> ShowContextual<C, T> for U { - fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> std::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) } } |