Пример #1
0
        public void ISBN13FromISBN10Examples()
        {
            var loadResult   = ISBNRangeMessage.LoadFromEmbeddedResource();
            var isbn13Parser = new ISBN13Parser(loadResult.Unwrap());

            var isbn10Str = "0306406152";

            Console.WriteLine(isbn13Parser.Parse(isbn10Str).Unwrap());
            //: 978-0-306-40615-7

            //Invalid ISBN10
            isbn13Parser.Parse("0306406199").MapErr(err =>
            {
                Console.WriteLine($"{err.Message}, {err.ErrorKind}");
                //: Input 0306406199 is not a valid ISBN10 number: InvalidChecksum, InvalidISBN10
                return(false);
            });
        }
Пример #2
0
        public void ISBN13Examples()
        {
            //Validate and get metadata from an ISBN-13
            var loadResult   = ISBNRangeMessage.LoadFromEmbeddedResource();
            var isbn13Parser = new ISBN13Parser(loadResult.Unwrap());

            var parseResult = isbn13Parser.Parse("9780671704278");

            //Unwrap (throws exception when result is an error)
            ISBN13.Metadata isbn13Metadata = parseResult.Unwrap();
            Console.WriteLine(isbn13Metadata.ToString());
            //: 978-0-671-70427-8

            //match: handle Err or Ok case
            parseResult.Match(
                err =>
            {
                //This is executed when the result is an error
                switch (err.ErrorKind)
                {
                case ISBNParseErrorKind.InvalidEan:
                    break;

                case ISBNParseErrorKind.NoMetadataFound:
                    break;

                case ISBNParseErrorKind.InvalidISBN13:
                    break;

                case ISBNParseErrorKind.InvalidISBN10:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                return(err.Message);
            },
                metadata =>
            {
                //This is executed when the result is Ok
                Console.WriteLine($"ISBN13: {metadata.ToString()}, Country: {metadata.CountryAgency}");
                //: ISBN13: 978-0-671-70427-8, Country: English language

                //An isbn13 is a valid EAN13
                var ean13 = metadata.Ean;
                Console.WriteLine($"Code: {ean13.Ean13Code} Checksum: {ean13.Checksum}");
                //: Code: 9780671704278 Checksum: 8

                return(metadata.ToString());
            });

            //See: ISBN13MetaDataInfoTests for more examples
        }