oxedyne/fe2o3/fe2o3_jdat/src/note.rs
1.6 KiB, 3 runs
created by r1870400018:479, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A `Dat::ABox` is an Annotated Box encapsulating a `Daticle` but extending a `Dat::Box` by |
| 2 | //! including a `String` annotation, which can be displayed natively in daticle string formatted |
| 3 | //! output. |
| 4 | //! |
| 5 | use oxedyne_fe2o3_core::{ |
| 6 | prelude::*, |
| 7 | byte::{ |
| 8 | ToBytes, |
| 9 | FromBytes, |
| 10 | }, |
| 11 | }; |
| 12 | |
| 13 | |
| 14 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 15 | pub struct NoteConfig { |
| 16 | pub adjacent: bool, |
| 17 | pub type1: bool, |
| 18 | } |
| 19 | |
| 20 | impl Default for NoteConfig { |
| 21 | fn default() -> Self { |
| 22 | Self { |
| 23 | adjacent: true, |
| 24 | type1: true, |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl NoteConfig { |
| 30 | pub const ADJACENT_BIT: u8 = 0b0000_0001; |
| 31 | pub const TYPE1_BIT: u8 = 0b0000_0010; |
| 32 | |
| 33 | pub fn is_adjacent(&self) -> bool { self.adjacent } |
| 34 | pub fn is_type1(&self) -> bool { self.type1 } |
| 35 | |
| 36 | pub fn set_adjacent(mut self, b: bool) -> Self { |
| 37 | self.adjacent = b; |
| 38 | self |
| 39 | } |
| 40 | pub fn set_type1(mut self, b: bool) -> Self { |
| 41 | self.type1 = b; |
| 42 | self |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl ToBytes for NoteConfig { |
| 47 | fn to_bytes(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> { |
| 48 | let mut b = 0; |
| 49 | if self.adjacent { |
| 50 | b |= Self::ADJACENT_BIT; |
| 51 | } |
| 52 | if self.type1 { |
| 53 | b |= Self::TYPE1_BIT; |
| 54 | } |
| 55 | buf.push(b); |
| 56 | Ok(buf) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl FromBytes for NoteConfig { |
| 61 | fn from_bytes(buf: &[u8]) -> Outcome<(Self, usize)> { |
| 62 | Ok(( |
| 63 | Self { |
| 64 | adjacent: (buf[0] & Self::ADJACENT_BIT) != 0, |
| 65 | type1: (buf[0] & Self::TYPE1_BIT) != 0, |
| 66 | }, |
| 67 | 1, |
| 68 | )) |
| 69 | } |
| 70 | } |