Esempio n. 1
0
        /// <summary>
        /// Deserializes a block from its Protobuf representation
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        static public Either <FormatError, Block> Deserialize(Format.Schema.Block b)
        {
            uint version = b.Version;

            if (version > SerializedBiscuit.MAX_SCHEMA_VERSION)
            {
                return(new VersionError(SerializedBiscuit.MAX_SCHEMA_VERSION, version));
            }

            SymbolTable symbols = new SymbolTable();

            foreach (string s in b.Symbols)
            {
                symbols.Add(s);
            }

            List <Fact>  facts  = new List <Fact>();
            List <Rule>  rules  = new List <Rule>();
            List <Check> checks = new List <Check>();

            if (version == 0)
            {
                foreach (Format.Schema.FactV0 fact in b.FactsV0)
                {
                    Either <FormatError, Fact> res = Fact.DeserializeV0(fact);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        facts.Add(res.Right);
                    }
                }


                foreach (Format.Schema.RuleV0 rule in b.RulesV0)
                {
                    Either <FormatError, Rule> res = Rule.DeserializeV0(rule);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        rules.Add(res.Right);
                    }
                }


                foreach (Format.Schema.CaveatV0 caveat in b.CaveatsV0)
                {
                    Either <FormatError, Check> res = Check.DeserializeV0(caveat);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        checks.Add(res.Right);
                    }
                }
            }
            else
            {
                foreach (Format.Schema.FactV1 fact in b.FactsV1)
                {
                    Either <FormatError, Fact> res = Fact.DeserializeV1(fact);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        facts.Add(res.Right);
                    }
                }


                foreach (Format.Schema.RuleV1 rule in b.RulesV1)
                {
                    Either <FormatError, Rule> res = Rule.DeserializeV1(rule);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        rules.Add(res.Right);
                    }
                }


                foreach (Format.Schema.CheckV1 check in b.ChecksV1)
                {
                    Either <FormatError, Check> res = Check.DeserializeV1(check);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        checks.Add(res.Right);
                    }
                }
            }

            return(new Right(new Block(b.Index, symbols, b.Context, facts, rules, checks)));
        }