| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use crate::cbor::{self, Value}; |
| 11 | | use crate::hash::{hash_object, ChangeHash, ObjectRef, ObjectType}; |
| 12 | | use crate::ids::WorkspaceId; |
| 13 | | use crate::objects::{ |
| 14 | | array, bytes, check_version, field, oref, text, Hlc, ObjectError, FORMAT_VERSION, |
| 15 | | }; |
| 16 | | use std::collections::BTreeMap; |
| 17 | |
|
| 18 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 19 | | pub struct View { |
| 20 | | |
| 21 | | pub bookmarks: BTreeMap<String, ObjectRef>, |
| 22 | | pub workspaces: BTreeMap<WorkspaceId, ChangeHash>, |
| 23 | | |
| 24 | | pub remotes: BTreeMap<String, BTreeMap<String, ObjectRef>>, |
| 25 | | |
| 26 | | pub policy: Option<ObjectRef>, |
| 27 | | |
| 28 | | pub tags: BTreeMap<String, ObjectRef>, |
| 29 | | } |
| 30 | |
|
| 31 | |
|
| 32 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 33 | | pub struct Format1View { |
| 34 | | pub bookmarks: BTreeMap<String, Vec<ChangeHash>>, |
| 35 | | pub workspaces: BTreeMap<WorkspaceId, ChangeHash>, |
| 36 | | } |
| 37 | |
|
| 38 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 39 | | pub struct Op { |
| 40 | | pub parents: Vec<ObjectRef>, |
| 41 | | pub hlc: Hlc, |
| 42 | | pub command: String, |
| 43 | | pub view: View, |
| 44 | | |
| 45 | | |
| 46 | | pub workspace: Option<WorkspaceId>, |
| 47 | | |
| 48 | | |
| 49 | | pub undoes: Option<ObjectRef>, |
| 50 | | } |
| 51 | |
|
| 52 | | impl View { |
| 53 | 3378× | fn to_value(&self) -> Value { |
| 54 | 3378× | Value::map([ |
| 55 | | ( |
| 56 | | 0, |
| 57 | | Value::Array( |
| 58 | 3378× | self.bookmarks |
| 59 | 3378× | .iter() |
| 60 | 3593× | .map(|(n, st)| { |
| 61 | 3593× | Value::Array(vec![Value::Text(n.clone()), Value::Bytes(st.0.to_vec())]) |
| 62 | 3593× | }) |
| 63 | 3378× | .collect(), |
| 64 | | ), |
| 65 | | ), |
| 66 | | ( |
| 67 | | 1, |
| 68 | | Value::Array( |
| 69 | 3378× | self.workspaces |
| 70 | 3378× | .iter() |
| 71 | 3378× | .map(|(w, h)| { |
| 72 | 3329× | Value::Array(vec![ |
| 73 | 3329× | Value::Bytes(w.0.to_vec()), |
| 74 | 3329× | Value::Bytes(h.0.to_vec()), |
| 75 | 3329× | ]) |
| 76 | 3329× | }) |
| 77 | 3378× | .collect(), |
| 78 | | ), |
| 79 | | ), |
| 80 | | ( |
| 81 | | 2, |
| 82 | | Value::Array( |
| 83 | 3378× | self.remotes |
| 84 | 3378× | .iter() |
| 85 | 3378× | .map(|(r, bms)| { |
| 86 | 1872× | Value::Array(vec![ |
| 87 | 1872× | Value::Text(r.clone()), |
| 88 | | Value::Array( |
| 89 | 1872× | bms.iter() |
| 90 | 1919× | .map(|(n, st)| { |
| 91 | 1919× | Value::Array(vec![ |
| 92 | 1919× | Value::Text(n.clone()), |
| 93 | 1919× | Value::Bytes(st.0.to_vec()), |
| 94 | 1919× | ]) |
| 95 | 1919× | }) |
| 96 | 1872× | .collect(), |
| 97 | | ), |
| 98 | | ]) |
| 99 | 1872× | }) |
| 100 | 3378× | .collect(), |
| 101 | | ), |
| 102 | | ), |
| 103 | | ( |
| 104 | | 3, |
| 105 | 3378× | self.policy |
| 106 | 3378× | .map(|p| Value::Bytes(p.0.to_vec())) |
| 107 | 3378× | .unwrap_or(Value::Null), |
| 108 | | ), |
| 109 | | ( |
| 110 | | 4, |
| 111 | | Value::Array( |
| 112 | 3378× | self.tags |
| 113 | 3378× | .iter() |
| 114 | 3378× | .map(|(n, st)| { |
| 115 | 118× | Value::Array(vec![Value::Text(n.clone()), Value::Bytes(st.0.to_vec())]) |
| 116 | 118× | }) |
| 117 | 3378× | .collect(), |
| 118 | | ), |
| 119 | | ), |
| 120 | | ]) |
| 121 | 3378× | } |
| 122 | |
|
| 123 | 17807× | fn from_value(v: &Value) -> Result<View, ObjectError> { |
| 124 | 17807× | let m = v |
| 125 | 17807× | .as_map() |
| 126 | 17807× | .ok_or_else(|| ObjectError::Schema("view: expected map".into()))?; |
| 127 | 17807× | let mut view = View::default(); |
| 128 | 18858× | for b in array(field(m, 0, "bookmarks")?, "bookmarks")? { |
| 129 | 18858× | let pair = array(b, "bookmark")?; |
| 130 | 18858× | if pair.len() != 2 { |
| 131 | 0× | return Err(ObjectError::Schema( |
| 132 | 0× | "bookmark: expected [name, heads]".into(), |
| 133 | 0× | )); |
| 134 | 18858× | } |
| 135 | 18858× | let name = text(&pair[0], "bookmark name")?.to_owned(); |
| 136 | 18858× | view.bookmarks |
| 137 | 18858× | .insert(name, oref(&pair[1], "bookmark state")?); |
| 138 | | } |
| 139 | 17806× | view.workspaces = workspaces_from_value(field(m, 1, "workspaces")?)?; |
| 140 | 17806× | if let Some(rs) = m.get(&2) { |
| 141 | 17806× | for r in array(rs, "remotes")? { |
| 142 | 7548× | let pair = array(r, "remote")?; |
| 143 | 7548× | if pair.len() != 2 { |
| 144 | 0× | return Err(ObjectError::Schema( |
| 145 | 0× | "remote: expected [name, bookmarks]".into(), |
| 146 | 0× | )); |
| 147 | 7548× | } |
| 148 | 7548× | let name = text(&pair[0], "remote name")?.to_owned(); |
| 149 | 7548× | let mut bms = BTreeMap::new(); |
| 150 | 7710× | for b in array(&pair[1], "remote bookmarks")? { |
| 151 | 7710× | let bp = array(b, "remote bookmark")?; |
| 152 | 7710× | if bp.len() != 2 { |
| 153 | 0× | return Err(ObjectError::Schema( |
| 154 | 0× | "remote bookmark: expected [name, state]".into(), |
| 155 | 0× | )); |
| 156 | 7710× | } |
| 157 | 7710× | bms.insert( |
| 158 | 7710× | text(&bp[0], "bookmark name")?.to_owned(), |
| 159 | 7710× | oref(&bp[1], "bookmark state")?, |
| 160 | | ); |
| 161 | | } |
| 162 | 7548× | view.remotes.insert(name, bms); |
| 163 | | } |
| 164 | 0× | } |
| 165 | 17806× | if let Some(p) = m.get(&3) { |
| 166 | 17806× | view.policy = if p.is_null() { |
| 167 | 16693× | None |
| 168 | | } else { |
| 169 | 1113× | Some(oref(p, "policy")?) |
| 170 | | }; |
| 171 | 0× | } |
| 172 | 17806× | if let Some(ts) = m.get(&4) { |
| 173 | 17806× | for t in array(ts, "tags")? { |
| 174 | 677× | let pair = array(t, "tag")?; |
| 175 | 677× | if pair.len() != 2 { |
| 176 | 0× | return Err(ObjectError::Schema("tag: expected [name, state]".into())); |
| 177 | 677× | } |
| 178 | 677× | view.tags.insert( |
| 179 | 677× | text(&pair[0], "tag name")?.to_owned(), |
| 180 | 677× | oref(&pair[1], "tag state")?, |
| 181 | | ); |
| 182 | | } |
| 183 | 0× | } |
| 184 | 17806× | Ok(view) |
| 185 | 17807× | } |
| 186 | | } |
| 187 | |
|
| 188 | 17809× | fn workspaces_from_value(v: &Value) -> Result<BTreeMap<WorkspaceId, ChangeHash>, ObjectError> { |
| 189 | 17809× | let mut out = BTreeMap::new(); |
| 190 | 17809× | for w in array(v, "workspaces")? { |
| 191 | 17196× | let pair = array(w, "workspace")?; |
| 192 | 17196× | if pair.len() != 2 { |
| 193 | 0× | return Err(ObjectError::Schema( |
| 194 | 0× | "workspace: expected [id, change]".into(), |
| 195 | 0× | )); |
| 196 | 17196× | } |
| 197 | 17196× | let id = WorkspaceId::from_slice(bytes(&pair[0], "workspace id")?) |
| 198 | 17196× | .ok_or_else(|| ObjectError::Schema("workspace id: expected 16 bytes".into()))?; |
| 199 | 17196× | out.insert(id, oref(&pair[1], "workspace change")?); |
| 200 | | } |
| 201 | 17809× | Ok(out) |
| 202 | 17809× | } |
| 203 | |
|
| 204 | | impl Format1View { |
| 205 | 3× | fn from_value(v: &Value) -> Result<Format1View, ObjectError> { |
| 206 | 3× | let m = v |
| 207 | 3× | .as_map() |
| 208 | 3× | .ok_or_else(|| ObjectError::Schema("view: expected map".into()))?; |
| 209 | 3× | let mut view = Format1View::default(); |
| 210 | 3× | for b in array(field(m, 0, "bookmarks")?, "bookmarks")? { |
| 211 | 3× | let pair = array(b, "bookmark")?; |
| 212 | 3× | if pair.len() != 2 { |
| 213 | 0× | return Err(ObjectError::Schema( |
| 214 | 0× | "bookmark: expected [name, heads]".into(), |
| 215 | 0× | )); |
| 216 | 3× | } |
| 217 | 3× | let name = text(&pair[0], "bookmark name")?.to_owned(); |
| 218 | 3× | let mut heads = array(&pair[1], "heads")? |
| 219 | 3× | .iter() |
| 220 | 3× | .map(|h| oref(h, "head")) |
| 221 | 3× | .collect::<Result<Vec<_>, _>>()?; |
| 222 | 3× | heads.sort(); |
| 223 | 3× | heads.dedup(); |
| 224 | 3× | view.bookmarks.insert(name, heads); |
| 225 | | } |
| 226 | 3× | view.workspaces = workspaces_from_value(field(m, 1, "workspaces")?)?; |
| 227 | 3× | Ok(view) |
| 228 | 3× | } |
| 229 | |
|
| 230 | | |
| 231 | | #[cfg(test)] |
| 232 | 4× | pub(crate) fn to_value(&self) -> Value { |
| 233 | 4× | Value::map([ |
| 234 | | ( |
| 235 | | 0, |
| 236 | | Value::Array( |
| 237 | 4× | self.bookmarks |
| 238 | 4× | .iter() |
| 239 | 4× | .map(|(n, hs)| { |
| 240 | 4× | Value::Array(vec![ |
| 241 | 4× | Value::Text(n.clone()), |
| 242 | | Value::Array( |
| 243 | 5× | hs.iter().map(|h| Value::Bytes(h.0.to_vec())).collect(), |
| 244 | | ), |
| 245 | | ]) |
| 246 | 4× | }) |
| 247 | 4× | .collect(), |
| 248 | | ), |
| 249 | | ), |
| 250 | | ( |
| 251 | | 1, |
| 252 | | Value::Array( |
| 253 | 4× | self.workspaces |
| 254 | 4× | .iter() |
| 255 | 4× | .map(|(w, h)| { |
| 256 | 4× | Value::Array(vec![ |
| 257 | 4× | Value::Bytes(w.0.to_vec()), |
| 258 | 4× | Value::Bytes(h.0.to_vec()), |
| 259 | 4× | ]) |
| 260 | 4× | }) |
| 261 | 4× | .collect(), |
| 262 | | ), |
| 263 | | ), |
| 264 | | ]) |
| 265 | 4× | } |
| 266 | | } |
| 267 | |
|
| 268 | |
|
| 269 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 270 | | pub struct Format1Op { |
| 271 | | pub parents: Vec<ObjectRef>, |
| 272 | | pub hlc: Hlc, |
| 273 | | pub command: String, |
| 274 | | pub view: Format1View, |
| 275 | | } |
| 276 | |
|
| 277 | | impl Format1Op { |
| 278 | | #[cfg(test)] |
| 279 | 4× | pub(crate) fn encode(&self) -> Vec<u8> { |
| 280 | 4× | cbor::encode(&Value::map([ |
| 281 | 4× | (0, Value::Uint(FORMAT_VERSION)), |
| 282 | | ( |
| 283 | | 1, |
| 284 | | Value::Array( |
| 285 | 4× | self.parents |
| 286 | 4× | .iter() |
| 287 | 4× | .map(|p| Value::Bytes(p.0.to_vec())) |
| 288 | 4× | .collect(), |
| 289 | | ), |
| 290 | | ), |
| 291 | 4× | (2, self.hlc.to_value()), |
| 292 | 4× | (3, Value::Text(self.command.clone())), |
| 293 | 4× | (4, self.view.to_value()), |
| 294 | | ])) |
| 295 | 4× | } |
| 296 | | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | | pub const SNAPSHOT_SUFFIX: &str = " (snapshot)"; |
| 301 | |
|
| 302 | | impl Op { |
| 303 | | |
| 304 | 58× | pub fn is_snapshot(&self) -> bool { |
| 305 | 58× | self.command.ends_with(SNAPSHOT_SUFFIX) |
| 306 | 58× | } |
| 307 | |
|
| 308 | 3378× | pub fn to_value(&self) -> Value { |
| 309 | 3378× | let mut fields = vec![ |
| 310 | 3378× | (0, Value::Uint(FORMAT_VERSION)), |
| 311 | | ( |
| 312 | | 1, |
| 313 | | Value::Array( |
| 314 | 3378× | self.parents |
| 315 | 3378× | .iter() |
| 316 | 3378× | .map(|p| Value::Bytes(p.0.to_vec())) |
| 317 | 3378× | .collect(), |
| 318 | | ), |
| 319 | | ), |
| 320 | 3378× | (2, self.hlc.to_value()), |
| 321 | 3378× | (3, Value::Text(self.command.clone())), |
| 322 | 3378× | (4, self.view.to_value()), |
| 323 | | ]; |
| 324 | | |
| 325 | 3378× | if let Some(w) = &self.workspace { |
| 326 | 3371× | fields.push((5, Value::Bytes(w.0.to_vec()))); |
| 327 | 3371× | } |
| 328 | 3378× | if let Some(u) = &self.undoes { |
| 329 | 5× | fields.push((6, Value::Bytes(u.0.to_vec()))); |
| 330 | 3373× | } |
| 331 | 3378× | Value::map(fields) |
| 332 | 3378× | } |
| 333 | 3378× | pub fn encode(&self) -> Vec<u8> { |
| 334 | 3378× | cbor::encode(&self.to_value()) |
| 335 | 3378× | } |
| 336 | 4× | pub fn hash(&self) -> ObjectRef { |
| 337 | 4× | hash_object(ObjectType::Op, &self.encode()) |
| 338 | 4× | } |
| 339 | 17807× | pub fn decode(b: &[u8]) -> Result<Op, ObjectError> { |
| 340 | 17807× | let v = cbor::decode(b)?; |
| 341 | 17807× | let m = v |
| 342 | 17807× | .as_map() |
| 343 | 17807× | .ok_or_else(|| ObjectError::Schema("op: expected map".into()))?; |
| 344 | 17807× | check_version(m)?; |
| 345 | | Ok(Op { |
| 346 | 17807× | parents: array(field(m, 1, "parents")?, "parents")? |
| 347 | 17807× | .iter() |
| 348 | 17807× | .map(|p| oref(p, "parent")) |
| 349 | 17807× | .collect::<Result<Vec<_>, _>>()?, |
| 350 | 17807× | hlc: Hlc::from_value(field(m, 2, "hlc")?)?, |
| 351 | 17807× | command: text(field(m, 3, "command")?, "command")?.to_owned(), |
| 352 | 17807× | view: View::from_value(field(m, 4, "view")?)?, |
| 353 | 17806× | workspace: match m.get(&5) { |
| 354 | 17797× | Some(v) => Some( |
| 355 | 17797× | WorkspaceId::from_slice(bytes(v, "workspace")?).ok_or_else(|| { |
| 356 | 0× | ObjectError::Schema("workspace: expected 16 bytes".into()) |
| 357 | 0× | })?, |
| 358 | | ), |
| 359 | 9× | None => None, |
| 360 | | }, |
| 361 | 17806× | undoes: m.get(&6).map(|v| oref(v, "undoes")).transpose()?, |
| 362 | | }) |
| 363 | 17807× | } |
| 364 | |
|
| 365 | | |
| 366 | 3× | pub fn decode_format1(b: &[u8]) -> Result<Format1Op, ObjectError> { |
| 367 | 3× | let v = cbor::decode(b)?; |
| 368 | 3× | let m = v |
| 369 | 3× | .as_map() |
| 370 | 3× | .ok_or_else(|| ObjectError::Schema("op: expected map".into()))?; |
| 371 | 3× | check_version(m)?; |
| 372 | | Ok(Format1Op { |
| 373 | 3× | parents: array(field(m, 1, "parents")?, "parents")? |
| 374 | 3× | .iter() |
| 375 | 3× | .map(|p| oref(p, "parent")) |
| 376 | 3× | .collect::<Result<Vec<_>, _>>()?, |
| 377 | 3× | hlc: Hlc::from_value(field(m, 2, "hlc")?)?, |
| 378 | 3× | command: text(field(m, 3, "command")?, "command")?.to_owned(), |
| 379 | 3× | view: Format1View::from_value(field(m, 4, "view")?)?, |
| 380 | | }) |
| 381 | 3× | } |
| 382 | | } |
| 383 | |
|
| 384 | | #[cfg(test)] |
| 385 | | mod tests { |
| 386 | | use super::*; |
| 387 | |
|
| 388 | | #[test] |
| 389 | 1× | fn op_round_trip() { |
| 390 | 1× | let mut view = View::default(); |
| 391 | 1× | view.bookmarks.insert("main".into(), ObjectRef([2; 32])); |
| 392 | 1× | view.bookmarks.insert("dev".into(), ObjectRef([3; 32])); |
| 393 | 1× | view.workspaces |
| 394 | 1× | .insert(WorkspaceId([5; 16]), ObjectRef([9; 32])); |
| 395 | 1× | view.remotes |
| 396 | 1× | .entry("origin".into()) |
| 397 | 1× | .or_default() |
| 398 | 1× | .insert("main".into(), ObjectRef([4; 32])); |
| 399 | 1× | view.policy = Some(ObjectRef([6; 32])); |
| 400 | 1× | view.tags.insert("v1".into(), ObjectRef([8; 32])); |
| 401 | 1× | let op = Op { |
| 402 | 1× | parents: vec![ObjectRef([7; 32])], |
| 403 | 1× | hlc: Hlc { |
| 404 | 1× | wall_ms: 5, |
| 405 | 1× | logical: 1, |
| 406 | 1× | }, |
| 407 | 1× | command: "oxy commit".into(), |
| 408 | 1× | view, |
| 409 | 1× | workspace: None, |
| 410 | 1× | undoes: None, |
| 411 | 1× | }; |
| 412 | 1× | let d = Op::decode(&op.encode()).unwrap(); |
| 413 | 1× | assert_eq!(d, op); |
| 414 | | |
| 415 | 1× | let mut known = op.clone(); |
| 416 | 1× | known.workspace = Some(WorkspaceId([3; 16])); |
| 417 | 1× | known.undoes = Some(ObjectRef([1; 32])); |
| 418 | 1× | assert_eq!(Op::decode(&known.encode()).unwrap(), known); |
| 419 | 1× | assert_ne!(known.hash(), op.hash()); |
| 420 | 1× | assert_eq!(d.hash(), Op::decode(&d.encode()).unwrap().hash()); |
| 421 | | |
| 422 | 1× | let mut v1 = Format1View::default(); |
| 423 | 1× | v1.bookmarks |
| 424 | 1× | .insert("main".into(), vec![ObjectRef([2; 32]), ObjectRef([1; 32])]); |
| 425 | 1× | v1.workspaces |
| 426 | 1× | .insert(WorkspaceId([5; 16]), ObjectRef([9; 32])); |
| 427 | 1× | let old = Format1Op { |
| 428 | 1× | parents: vec![], |
| 429 | 1× | hlc: Hlc::default(), |
| 430 | 1× | command: "oxy init".into(), |
| 431 | 1× | view: v1, |
| 432 | 1× | }; |
| 433 | 1× | assert!(Op::decode(&old.encode()).is_err()); |
| 434 | 1× | let back = Op::decode_format1(&old.encode()).unwrap(); |
| 435 | 1× | assert_eq!( |
| 436 | 1× | back.view.bookmarks["main"], |
| 437 | 1× | vec![ObjectRef([1; 32]), ObjectRef([2; 32])] |
| 438 | | ); |
| 439 | 1× | } |
| 440 | | } |