changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/object/direction.rs

changeset 698: 96958d3eb5b0
parent: c7165d93a9eb
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! # Direction types
2 use crate::{Deserialize, Error, Serialize};
3 use std::str::FromStr;
4 #[derive(Debug, Default, Serialize, Deserialize)]
5 pub enum CardinalDirection {
6  #[default]
7  North,
8  South,
9  East,
10  West,
11 }
12 
13 impl FromStr for CardinalDirection {
14  type Err = Error;
15 
16  fn from_str(s: &str) -> Result<CardinalDirection, Self::Err> {
17  match s {
18  "north" => Ok(CardinalDirection::North),
19  "south" => Ok(CardinalDirection::South),
20  "east" => Ok(CardinalDirection::East),
21  "west" => Ok(CardinalDirection::West),
22  _ => Err(Error::Message("not a CardinalDirection".to_string())),
23  }
24  }
25 }
26 
27 impl From<CardinalDirection> for String {
28  fn from(d: CardinalDirection) -> Self {
29  match d {
30  CardinalDirection::North => "north".to_string(),
31  CardinalDirection::South => "south".to_string(),
32  CardinalDirection::East => "east".to_string(),
33  CardinalDirection::West => "west".to_string(),
34  }
35  }
36 }
37 
38 #[derive(Debug, Default, Serialize, Deserialize)]
39 pub enum RelativeDirection {
40  Up,
41  #[default]
42  Down,
43  Left,
44  Right,
45 }
46 
47 impl FromStr for RelativeDirection {
48  type Err = Error;
49 
50  fn from_str(s: &str) -> Result<RelativeDirection, Self::Err> {
51  match s {
52  "up" | "u" => Ok(RelativeDirection::Up),
53  "down" | "d" => Ok(RelativeDirection::Down),
54  "left" | "l" => Ok(RelativeDirection::Left),
55  "right" | "r" => Ok(RelativeDirection::Right),
56  _ => Err(Error::Message("not a RelativeDirection".to_string())),
57  }
58  }
59 }
60 
61 impl From<RelativeDirection> for String {
62  fn from(d: RelativeDirection) -> Self {
63  match d {
64  RelativeDirection::Up => "up".to_string(),
65  RelativeDirection::Down => "down".to_string(),
66  RelativeDirection::Left => "left".to_string(),
67  RelativeDirection::Right => "right".to_string(),
68  }
69  }
70 }
71 
72 #[derive(Eq, PartialEq, Clone, Debug, Hash, Copy, Serialize, Deserialize)]
73 pub enum EdgeDirection {
74  Outbound,
75  Inbound,
76 }
77 
78 impl FromStr for EdgeDirection {
79  type Err = Error;
80 
81  fn from_str(s: &str) -> Result<EdgeDirection, Self::Err> {
82  match s {
83  "outbound" => Ok(EdgeDirection::Outbound),
84  "inbound" => Ok(EdgeDirection::Inbound),
85  _ => Err(Error::Message("not an EdgeDirection".to_string())),
86  }
87  }
88 }
89 
90 impl From<EdgeDirection> for String {
91  fn from(d: EdgeDirection) -> Self {
92  match d {
93  EdgeDirection::Outbound => "outbound".to_string(),
94  EdgeDirection::Inbound => "inbound".to_string(),
95  }
96  }
97 }