// Copyright (c) 2022-2023 Niko Bonnieure, Par le Peuple, NextGraph.org developers // All rights reserved. // This code is partly derived from work written by TG x Thoth from P2Pcollab. // Copyright 2022 TG x Thoth // Licensed under the Apache License, Version 2.0 // // or the MIT license , // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. //! Immutable Block use crate::types::*; impl BlockV0 { pub fn new( children: Vec, deps: ObjectDeps, expiry: Option, content: Vec, key: Option, ) -> BlockV0 { let mut b = BlockV0 { id: None, key, children, deps, expiry, content, }; let block = Block::V0(b.clone()); b.id = Some(block.get_id()); b } } impl Block { pub fn new( children: Vec, deps: ObjectDeps, expiry: Option, content: Vec, key: Option, ) -> Block { Block::V0(BlockV0::new(children, deps, expiry, content, key)) } /// Compute the ID pub fn get_id(&self) -> BlockId { let ser = serde_bare::to_vec(self).unwrap(); let hash = blake3::hash(ser.as_slice()); Digest::Blake3Digest32(hash.as_bytes().clone()) } /// Get the already computed ID pub fn id(&self) -> BlockId { match self { Block::V0(b) => match b.id { Some(id) => id, None => self.get_id(), }, } } /// Get the content pub fn content(&self) -> &Vec { match self { Block::V0(b) => &b.content, } } /// Get the children pub fn children(&self) -> &Vec { match self { Block::V0(b) => &b.children, } } /// Get the dependencies pub fn deps(&self) -> &ObjectDeps { match self { Block::V0(b) => &b.deps, } } /// Get the expiry pub fn expiry(&self) -> Option { match self { Block::V0(b) => b.expiry, } } pub fn set_expiry(&mut self, expiry: Option) { match self { Block::V0(b) => { b.id = None; b.expiry = expiry } } } /// Get the key pub fn key(&self) -> Option { match self { Block::V0(b) => b.key, } } /// Set the key pub fn set_key(&mut self, key: Option) { match self { Block::V0(b) => b.key = key, } } }