1use crate::{utils, EntryError, Error, PublicKey, Result};
31use hex_fmt::HexFmt;
32use serde::{Deserialize, Serialize};
33use std::{
34 collections::{btree_map::Entry, BTreeMap, BTreeSet},
35 fmt::{self, Debug, Formatter},
36 mem,
37};
38use xor_name::XorName;
39
40#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
43pub struct SeqData {
44 address: Address,
46 data: SeqEntries,
48 permissions: BTreeMap<PublicKey, PermissionSet>,
50 version: u64,
52 owner: PublicKey,
56}
57
58impl Debug for SeqData {
59 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
60 write!(formatter, "SeqMap {:?}", self.name())
61 }
62}
63
64#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
67pub struct UnseqData {
68 address: Address,
70 data: UnseqEntries,
72 permissions: BTreeMap<PublicKey, PermissionSet>,
74 version: u64,
76 owner: PublicKey,
80}
81
82impl Debug for UnseqData {
83 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
84 write!(formatter, "UnseqMap {:?}", self.name())
85 }
86}
87
88#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
90pub struct SeqValue {
91 pub data: Vec<u8>,
93 pub version: u64,
95}
96
97impl Debug for SeqValue {
98 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
99 write!(f, "{:<8} :: {}", HexFmt(&self.data), self.version)
100 }
101}
102
103#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
105pub enum Value {
106 Seq(SeqValue),
108 Unseq(Vec<u8>),
110}
111
112impl From<SeqValue> for Value {
113 fn from(value: SeqValue) -> Self {
114 Value::Seq(value)
115 }
116}
117
118impl From<Vec<u8>> for Value {
119 fn from(value: Vec<u8>) -> Self {
120 Value::Unseq(value)
121 }
122}
123
124#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
126pub enum Values {
127 Seq(Vec<SeqValue>),
129 Unseq(Vec<Vec<u8>>),
131}
132
133impl From<Vec<SeqValue>> for Values {
134 fn from(values: Vec<SeqValue>) -> Self {
135 Values::Seq(values)
136 }
137}
138
139impl From<Vec<Vec<u8>>> for Values {
140 fn from(values: Vec<Vec<u8>>) -> Self {
141 Values::Unseq(values)
142 }
143}
144
145#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize, Default)]
147pub struct PermissionSet {
148 permissions: BTreeSet<Action>,
149}
150
151impl PermissionSet {
152 pub fn new() -> PermissionSet {
154 PermissionSet {
155 permissions: Default::default(),
156 }
157 }
158
159 pub fn allow(mut self, action: Action) -> Self {
161 let _ = self.permissions.insert(action);
162 self
163 }
164
165 pub fn deny(mut self, action: Action) -> Self {
167 let _ = self.permissions.remove(&action);
168 self
169 }
170
171 pub fn is_allowed(&self, action: Action) -> bool {
173 self.permissions.contains(&action)
174 }
175}
176
177#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
179pub enum Action {
180 Read,
182 Insert,
184 Update,
186 Delete,
188 ManagePermissions,
190}
191
192macro_rules! impl_map {
193 ($flavour:ident) => {
194 impl $flavour {
195 pub fn address(&self) -> &Address {
197 &self.address
198 }
199
200 pub fn name(&self) -> &XorName {
202 self.address.name()
203 }
204
205 pub fn tag(&self) -> u64 {
207 self.address.tag()
208 }
209
210 pub fn kind(&self) -> Kind {
212 self.address.kind()
213 }
214
215 pub fn version(&self) -> u64 {
217 self.version
218 }
219
220 pub fn owner(&self) -> &PublicKey {
222 &self.owner
223 }
224
225 pub fn keys(&self) -> BTreeSet<Vec<u8>> {
227 self.data.keys().cloned().collect()
228 }
229
230 pub fn shell(&self) -> Self {
232 Self {
233 address: self.address.clone(),
234 data: BTreeMap::new(),
235 permissions: self.permissions.clone(),
236 version: self.version,
237 owner: self.owner,
238 }
239 }
240
241 pub fn permissions(&self) -> BTreeMap<PublicKey, PermissionSet> {
243 self.permissions.clone()
244 }
245
246 pub fn user_permissions(&self, user: PublicKey) -> Result<&PermissionSet> {
248 self.permissions.get(&user).ok_or(Error::NoSuchKey)
249 }
250
251 pub fn check_is_owner(&self, requester: PublicKey) -> Result<()> {
256 if self.owner == requester {
257 Ok(())
258 } else {
259 Err(Error::AccessDenied)
260 }
261 }
262
263 pub fn check_permissions(&self, action: Action, requester: PublicKey) -> Result<()> {
267 if self.owner == requester {
268 Ok(())
269 } else {
270 let permissions = self
271 .user_permissions(requester)
272 .map_err(|_| Error::AccessDenied)?;
273 if permissions.is_allowed(action) {
274 Ok(())
275 } else {
276 Err(Error::AccessDenied)
277 }
278 }
279 }
280
281 pub fn set_user_permissions(
286 &mut self,
287 user: PublicKey,
288 permissions: PermissionSet,
289 version: u64,
290 ) -> Result<()> {
291 if version != self.version + 1 {
292 return Err(Error::InvalidSuccessor(self.version));
293 }
294
295 let _prev = self.permissions.insert(user, permissions);
296 self.version = version;
297
298 Ok(())
299 }
300
301 pub fn del_user_permissions(&mut self, user: PublicKey, version: u64) -> Result<()> {
306 if version != self.version + 1 {
307 return Err(Error::InvalidSuccessor(self.version));
308 }
309 if !self.permissions.contains_key(&user) {
310 return Err(Error::NoSuchKey);
311 }
312
313 let _ = self.permissions.remove(&user);
314 self.version = version;
315
316 Ok(())
317 }
318
319 pub fn del_user_permissions_without_validation(
324 &mut self,
325 user: PublicKey,
326 version: u64,
327 ) -> bool {
328 if version <= self.version {
329 return false;
330 }
331
332 let _ = self.permissions.remove(&user);
333 self.version = version;
334
335 true
336 }
337
338 pub fn change_owner(&mut self, new_owner: PublicKey, version: u64) -> Result<()> {
343 if version != self.version + 1 {
344 return Err(Error::InvalidSuccessor(self.version));
345 }
346
347 self.owner = new_owner;
348 self.version = version;
349
350 Ok(())
351 }
352
353 pub fn change_owner_without_validation(
358 &mut self,
359 new_owner: PublicKey,
360 version: u64,
361 ) -> bool {
362 if version <= self.version {
363 return false;
364 }
365
366 self.owner = new_owner;
367 self.version = version;
368
369 true
370 }
371
372 pub fn is_action_allowed(&self, requester: &PublicKey, action: Action) -> bool {
374 match self.permissions.get(requester) {
375 Some(perms) => perms.is_allowed(action),
376 None => false,
377 }
378 }
379 }
380 };
381}
382
383impl_map!(SeqData);
384impl_map!(UnseqData);
385
386impl UnseqData {
387 pub fn new(name: XorName, tag: u64, owner: PublicKey) -> Self {
389 Self {
390 address: Address::Unseq { name, tag },
391 data: Default::default(),
392 permissions: Default::default(),
393 version: 0,
394 owner,
395 }
396 }
397
398 pub fn new_with_data(
400 name: XorName,
401 tag: u64,
402 data: UnseqEntries,
403 permissions: BTreeMap<PublicKey, PermissionSet>,
404 owner: PublicKey,
405 ) -> Self {
406 Self {
407 address: Address::Unseq { name, tag },
408 data,
409 permissions,
410 version: 0,
411 owner,
412 }
413 }
414
415 pub fn get(&self, key: &[u8]) -> Option<&Vec<u8>> {
417 self.data.get(key)
418 }
419
420 pub fn values(&self) -> Vec<Vec<u8>> {
422 self.data.values().cloned().collect()
423 }
424
425 pub fn entries(&self) -> &UnseqEntries {
427 &self.data
428 }
429
430 pub fn take_entries(&mut self) -> UnseqEntries {
432 mem::replace(&mut self.data, BTreeMap::new())
433 }
434
435 pub fn mutate_entries(
439 &mut self,
440 actions: UnseqEntryActions,
441 requester: PublicKey,
442 ) -> Result<()> {
443 let (insert, update, delete) = actions.actions.into_iter().fold(
444 (
445 BTreeMap::<Vec<u8>, Vec<u8>>::new(),
446 BTreeMap::<Vec<u8>, Vec<u8>>::new(),
447 BTreeSet::<Vec<u8>>::new(),
448 ),
449 |(mut insert, mut update, mut delete), (key, item)| {
450 match item {
451 UnseqEntryAction::Ins(value) => {
452 let _ = insert.insert(key, value);
453 }
454 UnseqEntryAction::Update(value) => {
455 let _ = update.insert(key, value);
456 }
457 UnseqEntryAction::Del => {
458 let _ = delete.insert(key);
459 }
460 };
461 (insert, update, delete)
462 },
463 );
464
465 if *self.owner() != requester
466 && ((!insert.is_empty() && !self.is_action_allowed(&requester, Action::Insert))
467 || (!update.is_empty() && !self.is_action_allowed(&requester, Action::Update))
468 || (!delete.is_empty() && !self.is_action_allowed(&requester, Action::Delete)))
469 {
470 return Err(Error::AccessDenied);
471 }
472
473 let mut new_data = self.data.clone();
474 let mut errors = BTreeMap::new();
475
476 for (key, val) in insert {
477 match new_data.entry(key) {
478 Entry::Occupied(entry) => {
479 let _ = errors.insert(entry.key().clone(), EntryError::EntryExists(0));
480 }
481 Entry::Vacant(entry) => {
482 let _ = entry.insert(val);
483 }
484 }
485 }
486
487 for (key, val) in update {
488 match new_data.entry(key) {
489 Entry::Occupied(mut entry) => {
490 let _ = entry.insert(val);
491 }
492 Entry::Vacant(entry) => {
493 let _ = errors.insert(entry.key().clone(), EntryError::NoSuchEntry);
494 }
495 }
496 }
497
498 for key in delete {
499 match new_data.entry(key.clone()) {
500 Entry::Occupied(_) => {
501 let _ = new_data.remove(&key);
502 }
503 Entry::Vacant(entry) => {
504 let _ = errors.insert(entry.key().clone(), EntryError::NoSuchEntry);
505 }
506 }
507 }
508
509 if !errors.is_empty() {
510 return Err(Error::InvalidEntryActions(errors));
511 }
512
513 let _old_data = mem::replace(&mut self.data, new_data);
514
515 Ok(())
516 }
517}
518
519impl SeqData {
521 pub fn new(name: XorName, tag: u64, owner: PublicKey) -> Self {
523 Self {
524 address: Address::Seq { name, tag },
525 data: Default::default(),
526 permissions: Default::default(),
527 version: 0,
528 owner,
529 }
530 }
531
532 pub fn new_with_data(
534 name: XorName,
535 tag: u64,
536 data: SeqEntries,
537 permissions: BTreeMap<PublicKey, PermissionSet>,
538 owner: PublicKey,
539 ) -> Self {
540 Self {
541 address: Address::Seq { name, tag },
542 data,
543 permissions,
544 version: 0,
545 owner,
546 }
547 }
548
549 pub fn get(&self, key: &[u8]) -> Option<&SeqValue> {
551 self.data.get(key)
552 }
553
554 pub fn values(&self) -> Vec<SeqValue> {
556 self.data.values().cloned().collect()
557 }
558
559 pub fn entries(&self) -> &SeqEntries {
561 &self.data
562 }
563
564 pub fn take_entries(&mut self) -> SeqEntries {
566 mem::replace(&mut self.data, BTreeMap::new())
567 }
568
569 pub fn mutate_entries(&mut self, actions: SeqEntryActions, requester: PublicKey) -> Result<()> {
573 let (insert, update, delete) = actions.actions.into_iter().fold(
575 (BTreeMap::new(), BTreeMap::new(), BTreeMap::new()),
576 |(mut insert, mut update, mut delete), (key, item)| {
577 match item {
578 SeqEntryAction::Ins(value) => {
579 let _ = insert.insert(key, value);
580 }
581 SeqEntryAction::Update(value) => {
582 let _ = update.insert(key, value);
583 }
584 SeqEntryAction::Del(version) => {
585 let _ = delete.insert(key, version);
586 }
587 };
588 (insert, update, delete)
589 },
590 );
591
592 if *self.owner() != requester
593 && ((!insert.is_empty() && !self.is_action_allowed(&requester, Action::Insert))
594 || (!update.is_empty() && !self.is_action_allowed(&requester, Action::Update))
595 || (!delete.is_empty() && !self.is_action_allowed(&requester, Action::Delete)))
596 {
597 return Err(Error::AccessDenied);
598 }
599
600 let mut new_data = self.data.clone();
601 let mut errors = BTreeMap::new();
602
603 for (key, val) in insert {
604 match new_data.entry(key) {
605 Entry::Occupied(entry) => {
606 let _ = errors.insert(
607 entry.key().clone(),
608 EntryError::EntryExists(entry.get().version as u8),
609 );
610 }
611 Entry::Vacant(entry) => {
612 let _ = entry.insert(val);
613 }
614 }
615 }
616
617 for (key, val) in update {
618 match new_data.entry(key) {
619 Entry::Occupied(mut entry) => {
620 let current_version = entry.get().version;
621 if val.version == current_version + 1 {
622 let _ = entry.insert(val);
623 } else {
624 let _ = errors.insert(
625 entry.key().clone(),
626 EntryError::InvalidSuccessor(current_version as u8),
627 );
628 }
629 }
630 Entry::Vacant(entry) => {
631 let _ = errors.insert(entry.key().clone(), EntryError::NoSuchEntry);
632 }
633 }
634 }
635
636 for (key, version) in delete {
637 match new_data.entry(key.clone()) {
638 Entry::Occupied(entry) => {
639 let current_version = entry.get().version;
640 if version == current_version + 1 {
641 let _ = new_data.remove(&key);
642 } else {
643 let _ = errors.insert(
644 entry.key().clone(),
645 EntryError::InvalidSuccessor(current_version as u8),
646 );
647 }
648 }
649 Entry::Vacant(entry) => {
650 let _ = errors.insert(entry.key().clone(), EntryError::NoSuchEntry);
651 }
652 }
653 }
654
655 if !errors.is_empty() {
656 return Err(Error::InvalidEntryActions(errors));
657 }
658
659 let _old_data = mem::replace(&mut self.data, new_data);
660
661 Ok(())
662 }
663}
664
665#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
667pub enum Kind {
668 Unseq,
670 Seq,
672}
673
674impl Kind {
675 pub fn from_flag(sequenced: bool) -> Self {
677 if sequenced {
678 Kind::Seq
679 } else {
680 Kind::Unseq
681 }
682 }
683
684 pub fn is_seq(self) -> bool {
686 self == Kind::Seq
687 }
688
689 pub fn is_unseq(self) -> bool {
691 !self.is_seq()
692 }
693}
694
695#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
697pub enum Address {
698 Unseq {
700 name: XorName,
702 tag: u64,
704 },
705 Seq {
707 name: XorName,
709 tag: u64,
711 },
712}
713
714impl Address {
715 pub fn from_kind(kind: Kind, name: XorName, tag: u64) -> Self {
717 match kind {
718 Kind::Seq => Address::Seq { name, tag },
719 Kind::Unseq => Address::Unseq { name, tag },
720 }
721 }
722
723 pub fn kind(&self) -> Kind {
725 match self {
726 Address::Seq { .. } => Kind::Seq,
727 Address::Unseq { .. } => Kind::Unseq,
728 }
729 }
730
731 pub fn name(&self) -> &XorName {
733 match self {
734 Address::Unseq { ref name, .. } | Address::Seq { ref name, .. } => name,
735 }
736 }
737
738 pub fn tag(&self) -> u64 {
740 match self {
741 Address::Unseq { tag, .. } | Address::Seq { tag, .. } => *tag,
742 }
743 }
744
745 pub fn is_seq(&self) -> bool {
747 self.kind().is_seq()
748 }
749
750 pub fn is_unseq(&self) -> bool {
752 self.kind().is_unseq()
753 }
754
755 pub fn encode_to_zbase32(&self) -> String {
757 utils::encode(&self)
758 }
759
760 pub fn decode_from_zbase32<T: AsRef<str>>(encoded: T) -> Result<Self> {
762 utils::decode(encoded)
763 }
764}
765
766#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
768pub enum Data {
769 Seq(SeqData),
771 Unseq(UnseqData),
773}
774
775impl Data {
776 pub fn address(&self) -> &Address {
778 match self {
779 Data::Seq(data) => data.address(),
780 Data::Unseq(data) => data.address(),
781 }
782 }
783
784 pub fn kind(&self) -> Kind {
786 self.address().kind()
787 }
788
789 pub fn name(&self) -> &XorName {
791 self.address().name()
792 }
793
794 pub fn tag(&self) -> u64 {
796 self.address().tag()
797 }
798
799 pub fn is_seq(&self) -> bool {
801 self.kind().is_seq()
802 }
803
804 pub fn is_unseq(&self) -> bool {
806 self.kind().is_unseq()
807 }
808
809 pub fn version(&self) -> u64 {
811 match self {
812 Data::Seq(data) => data.version(),
813 Data::Unseq(data) => data.version(),
814 }
815 }
816
817 pub fn keys(&self) -> BTreeSet<Vec<u8>> {
819 match self {
820 Data::Seq(data) => data.keys(),
821 Data::Unseq(data) => data.keys(),
822 }
823 }
824
825 pub fn shell(&self) -> Self {
827 match self {
828 Data::Seq(data) => Data::Seq(data.shell()),
829 Data::Unseq(data) => Data::Unseq(data.shell()),
830 }
831 }
832
833 pub fn permissions(&self) -> BTreeMap<PublicKey, PermissionSet> {
835 match self {
836 Data::Seq(data) => data.permissions(),
837 Data::Unseq(data) => data.permissions(),
838 }
839 }
840
841 pub fn user_permissions(&self, user: PublicKey) -> Result<&PermissionSet> {
843 match self {
844 Data::Seq(data) => data.user_permissions(user),
845 Data::Unseq(data) => data.user_permissions(user),
846 }
847 }
848
849 pub fn set_user_permissions(
851 &mut self,
852 user: PublicKey,
853 permissions: PermissionSet,
854 version: u64,
855 ) -> Result<()> {
856 match self {
857 Data::Seq(data) => data.set_user_permissions(user, permissions, version),
858 Data::Unseq(data) => data.set_user_permissions(user, permissions, version),
859 }
860 }
861
862 pub fn del_user_permissions(&mut self, user: PublicKey, version: u64) -> Result<()> {
864 match self {
865 Data::Seq(data) => data.del_user_permissions(user, version),
866 Data::Unseq(data) => data.del_user_permissions(user, version),
867 }
868 }
869
870 pub fn check_permissions(&self, action: Action, requester: PublicKey) -> Result<()> {
872 match self {
873 Data::Seq(data) => data.check_permissions(action, requester),
874 Data::Unseq(data) => data.check_permissions(action, requester),
875 }
876 }
877
878 pub fn check_is_owner(&self, requester: PublicKey) -> Result<()> {
880 match self {
881 Data::Seq(data) => data.check_is_owner(requester),
882 Data::Unseq(data) => data.check_is_owner(requester),
883 }
884 }
885
886 pub fn owner(&self) -> PublicKey {
888 match self {
889 Data::Seq(data) => data.owner,
890 Data::Unseq(data) => data.owner,
891 }
892 }
893
894 pub fn mutate_entries(&mut self, actions: EntryActions, requester: PublicKey) -> Result<()> {
896 match self {
897 Data::Seq(data) => {
898 if let EntryActions::Seq(actions) = actions {
899 return data.mutate_entries(actions, requester);
900 }
901 }
902 Data::Unseq(data) => {
903 if let EntryActions::Unseq(actions) = actions {
904 return data.mutate_entries(actions, requester);
905 }
906 }
907 }
908
909 Err(Error::InvalidOperation)
910 }
911}
912
913impl From<SeqData> for Data {
914 fn from(data: SeqData) -> Self {
915 Data::Seq(data)
916 }
917}
918
919impl From<UnseqData> for Data {
920 fn from(data: UnseqData) -> Self {
921 Data::Unseq(data)
922 }
923}
924
925#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug)]
927pub enum SeqEntryAction {
928 Ins(SeqValue),
930 Update(SeqValue),
932 Del(u64),
934}
935
936impl SeqEntryAction {
937 pub fn version(&self) -> u64 {
939 match *self {
940 Self::Ins(ref value) => value.version,
941 Self::Update(ref value) => value.version,
942 Self::Del(v) => v,
943 }
944 }
945
946 pub fn set_version(&mut self, version: u64) {
948 match *self {
949 Self::Ins(ref mut value) => value.version = version,
950 Self::Update(ref mut value) => value.version = version,
951 Self::Del(ref mut v) => *v = version,
952 }
953 }
954}
955
956#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug)]
958pub enum UnseqEntryAction {
959 Ins(Vec<u8>),
961 Update(Vec<u8>),
963 Del,
965}
966
967#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug, Default)]
969pub struct SeqEntryActions {
970 actions: BTreeMap<Vec<u8>, SeqEntryAction>,
972}
973
974impl SeqEntryActions {
975 pub fn new() -> Self {
977 Default::default()
978 }
979
980 pub fn actions(&self) -> &BTreeMap<Vec<u8>, SeqEntryAction> {
982 &self.actions
983 }
984
985 pub fn into_actions(self) -> BTreeMap<Vec<u8>, SeqEntryAction> {
987 self.actions
988 }
989
990 pub fn ins(mut self, key: Vec<u8>, content: Vec<u8>, version: u64) -> Self {
995 let _ = self.actions.insert(
996 key,
997 SeqEntryAction::Ins(SeqValue {
998 data: content,
999 version,
1000 }),
1001 );
1002 self
1003 }
1004
1005 pub fn update(mut self, key: Vec<u8>, content: Vec<u8>, version: u64) -> Self {
1010 let _ = self.actions.insert(
1011 key,
1012 SeqEntryAction::Update(SeqValue {
1013 data: content,
1014 version,
1015 }),
1016 );
1017 self
1018 }
1019
1020 pub fn del(mut self, key: Vec<u8>, version: u64) -> Self {
1025 let _ = self.actions.insert(key, SeqEntryAction::Del(version));
1026 self
1027 }
1028
1029 pub fn add_action(&mut self, key: Vec<u8>, action: SeqEntryAction) {
1031 let _ = self.actions.insert(key, action);
1032 }
1033}
1034
1035impl From<SeqEntryActions> for BTreeMap<Vec<u8>, SeqEntryAction> {
1036 fn from(actions: SeqEntryActions) -> Self {
1037 actions.actions
1038 }
1039}
1040
1041impl From<BTreeMap<Vec<u8>, SeqEntryAction>> for SeqEntryActions {
1042 fn from(actions: BTreeMap<Vec<u8>, SeqEntryAction>) -> Self {
1043 SeqEntryActions { actions }
1044 }
1045}
1046
1047#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug, Default)]
1049pub struct UnseqEntryActions {
1050 actions: BTreeMap<Vec<u8>, UnseqEntryAction>,
1053}
1054
1055impl UnseqEntryActions {
1056 pub fn new() -> Self {
1058 Default::default()
1059 }
1060
1061 pub fn actions(&self) -> &BTreeMap<Vec<u8>, UnseqEntryAction> {
1063 &self.actions
1064 }
1065
1066 pub fn into_actions(self) -> BTreeMap<Vec<u8>, UnseqEntryAction> {
1068 self.actions
1069 }
1070
1071 pub fn ins(mut self, key: Vec<u8>, content: Vec<u8>) -> Self {
1073 let _ = self.actions.insert(key, UnseqEntryAction::Ins(content));
1074 self
1075 }
1076
1077 pub fn update(mut self, key: Vec<u8>, content: Vec<u8>) -> Self {
1079 let _ = self.actions.insert(key, UnseqEntryAction::Update(content));
1080 self
1081 }
1082
1083 pub fn del(mut self, key: Vec<u8>) -> Self {
1085 let _ = self.actions.insert(key, UnseqEntryAction::Del);
1086 self
1087 }
1088
1089 pub fn add_action(&mut self, key: Vec<u8>, action: UnseqEntryAction) {
1091 let _ = self.actions.insert(key, action);
1092 }
1093}
1094
1095impl From<UnseqEntryActions> for BTreeMap<Vec<u8>, UnseqEntryAction> {
1096 fn from(actions: UnseqEntryActions) -> Self {
1097 actions.actions
1098 }
1099}
1100
1101impl From<BTreeMap<Vec<u8>, UnseqEntryAction>> for UnseqEntryActions {
1102 fn from(actions: BTreeMap<Vec<u8>, UnseqEntryAction>) -> Self {
1103 UnseqEntryActions { actions }
1104 }
1105}
1106
1107#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug)]
1109pub enum EntryActions {
1110 Seq(SeqEntryActions),
1112 Unseq(UnseqEntryActions),
1114}
1115
1116impl EntryActions {
1117 pub fn kind(&self) -> Kind {
1119 match self {
1120 EntryActions::Seq(_) => Kind::Seq,
1121 EntryActions::Unseq(_) => Kind::Unseq,
1122 }
1123 }
1124}
1125
1126impl From<SeqEntryActions> for EntryActions {
1127 fn from(entry_actions: SeqEntryActions) -> Self {
1128 EntryActions::Seq(entry_actions)
1129 }
1130}
1131
1132impl From<UnseqEntryActions> for EntryActions {
1133 fn from(entry_actions: UnseqEntryActions) -> Self {
1134 EntryActions::Unseq(entry_actions)
1135 }
1136}
1137
1138pub type SeqEntries = BTreeMap<Vec<u8>, SeqValue>;
1140pub type UnseqEntries = BTreeMap<Vec<u8>, Vec<u8>>;
1142
1143#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug)]
1145pub enum Entries {
1146 Seq(SeqEntries),
1148 Unseq(UnseqEntries),
1150}
1151
1152impl From<SeqEntries> for Entries {
1153 fn from(entries: SeqEntries) -> Self {
1154 Entries::Seq(entries)
1155 }
1156}
1157
1158impl From<UnseqEntries> for Entries {
1159 fn from(entries: UnseqEntries) -> Self {
1160 Entries::Unseq(entries)
1161 }
1162}
1163
1164#[cfg(test)]
1165mod tests {
1166 use super::{Address, XorName};
1167 use unwrap::unwrap;
1168
1169 #[test]
1170 fn zbase32_encode_decode_map_address() {
1171 let name = XorName(rand::random());
1172 let address = Address::Seq { name, tag: 15000 };
1173 let encoded = address.encode_to_zbase32();
1174 let decoded = unwrap!(self::Address::decode_from_zbase32(&encoded));
1175 assert_eq!(address, decoded);
1176 }
1177}