options.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. use std::str::FromStr;
  2. pub enum CellLayout {
  3. Trusting,
  4. Wrapping,
  5. Unbounded
  6. }
  7. pub enum CellSize {
  8. Bits(usize),
  9. Modular(usize),
  10. Int
  11. }
  12. pub struct Options {
  13. pub cell_layout: CellLayout,
  14. pub memory_size: i64,
  15. pub cell_size: CellSize,
  16. }
  17. impl Default for Options {
  18. fn default() -> Self {
  19. Options {
  20. cell_layout: CellLayout::Trusting,
  21. memory_size: 0x10000,
  22. cell_size: CellSize::Bits(8),
  23. }
  24. }
  25. }
  26. impl FromStr for CellLayout {
  27. type Err = &'static str;
  28. fn from_str(s: &str) -> Result<Self, Self::Err> {
  29. match s {
  30. "trusting" => Ok(CellLayout::Trusting),
  31. "wrapping" => Ok(CellLayout::Wrapping),
  32. _ => Err("invalid cell layout"),
  33. }
  34. }
  35. }
  36. impl FromStr for CellSize {
  37. type Err = &'static str;
  38. fn from_str(s: &str) -> Result<Self, Self::Err> {
  39. let integer = s.parse::<usize>();
  40. match integer {
  41. Ok(i) => Ok(CellSize::Bits(i)),
  42. Err(e) => match s {
  43. "8" => Ok(CellSize::Bits(8)),
  44. "16" => Ok(CellSize::Bits(16)),
  45. "32" => Ok(CellSize::Bits(16)),
  46. "int" => Ok(CellSize::Int),
  47. _ => Err("invalid cell size"),
  48. }
  49. }
  50. }
  51. }