@@ -70,6 +70,8 @@ pub enum LoroValueKind {
7070 List ,
7171 Map ,
7272 ContainerType ,
73+ /// Root container type - used for mergeable containers
74+ RootContainerType ,
7375}
7476impl LoroValueKind {
7577 fn from_u8 ( kind : u8 ) -> Self {
@@ -84,6 +86,7 @@ impl LoroValueKind {
8486 7 => LoroValueKind :: List ,
8587 8 => LoroValueKind :: Map ,
8688 9 => LoroValueKind :: ContainerType ,
89+ 10 => LoroValueKind :: RootContainerType ,
8790 _ => unreachable ! ( ) ,
8891 }
8992 }
@@ -100,6 +103,7 @@ impl LoroValueKind {
100103 LoroValueKind :: List => 7 ,
101104 LoroValueKind :: Map => 8 ,
102105 LoroValueKind :: ContainerType => 9 ,
106+ LoroValueKind :: RootContainerType => 10 ,
103107 }
104108 }
105109}
@@ -665,6 +669,23 @@ impl<'a> ValueReader<'a> {
665669
666670 LoroValue :: Container ( container_id)
667671 }
672+ LoroValueKind :: RootContainerType => {
673+ // Read container type
674+ let type_u8 = self . read_u8 ( ) ?;
675+ let container_type =
676+ ContainerType :: try_from_u8 ( type_u8) . unwrap_or ( ContainerType :: Unknown ( type_u8) ) ;
677+ // Read the root container name from the keys arena
678+ let key_idx = self . read_usize ( ) ?;
679+ let name = keys
680+ . get ( key_idx)
681+ . ok_or ( LoroError :: DecodeDataCorruptionError ) ?
682+ . clone ( ) ;
683+ let container_id = ContainerID :: Root {
684+ name,
685+ container_type,
686+ } ;
687+ LoroValue :: Container ( container_id)
688+ }
668689 } )
669690 }
670691
@@ -777,6 +798,23 @@ impl<'a> ValueReader<'a> {
777798
778799 LoroValue :: Container ( container_id)
779800 }
801+ LoroValueKind :: RootContainerType => {
802+ // Read container type
803+ let type_u8 = self . read_u8 ( ) ?;
804+ let container_type = ContainerType :: try_from_u8 ( type_u8)
805+ . unwrap_or ( ContainerType :: Unknown ( type_u8) ) ;
806+ // Read the root container name from the keys arena
807+ let name_key_idx = self . read_usize ( ) ?;
808+ let name = keys
809+ . get ( name_key_idx)
810+ . ok_or ( LoroError :: DecodeDataCorruptionError ) ?
811+ . clone ( ) ;
812+ let container_id = ContainerID :: Root {
813+ name,
814+ container_type,
815+ } ;
816+ LoroValue :: Container ( container_id)
817+ }
780818 } ;
781819
782820 task = match task {
@@ -1035,10 +1073,23 @@ impl ValueWriter {
10351073 ( LoroValueKind :: Map , len)
10361074 }
10371075 LoroValue :: Binary ( value) => ( LoroValueKind :: Binary , self . write_binary ( value) ) ,
1038- LoroValue :: Container ( c) => (
1039- LoroValueKind :: ContainerType ,
1040- self . write_u8 ( c. container_type ( ) . to_u8 ( ) ) ,
1041- ) ,
1076+ LoroValue :: Container ( c) => match c {
1077+ ContainerID :: Normal { container_type, .. } => (
1078+ LoroValueKind :: ContainerType ,
1079+ self . write_u8 ( container_type. to_u8 ( ) ) ,
1080+ ) ,
1081+ ContainerID :: Root {
1082+ name,
1083+ container_type,
1084+ } => {
1085+ // For Root containers, we need to encode:
1086+ // 1. The container type
1087+ // 2. The name (via key register)
1088+ let key_idx = registers. key_mut ( ) . register ( name) ;
1089+ let len = self . write_u8 ( container_type. to_u8 ( ) ) + self . write_usize ( key_idx) ;
1090+ ( LoroValueKind :: RootContainerType , len)
1091+ }
1092+ } ,
10421093 }
10431094 }
10441095
@@ -1145,6 +1196,9 @@ fn get_loro_value_kind(value: &LoroValue) -> LoroValueKind {
11451196 LoroValue :: List ( _) => LoroValueKind :: List ,
11461197 LoroValue :: Map ( _) => LoroValueKind :: Map ,
11471198 LoroValue :: Binary ( _) => LoroValueKind :: Binary ,
1148- LoroValue :: Container ( _) => LoroValueKind :: ContainerType ,
1199+ LoroValue :: Container ( c) => match c {
1200+ ContainerID :: Normal { .. } => LoroValueKind :: ContainerType ,
1201+ ContainerID :: Root { .. } => LoroValueKind :: RootContainerType ,
1202+ } ,
11491203 }
11501204}
0 commit comments