Example #1
0
        public void AppendKVP <T>(string key, T value, TypeMarshaller <T> marshaller)
        {
            checkArgs();

            _stringBuilder.AppendFormatLine(KVP_PATTERN, key, pack());

            return;

            void checkArgs()
            {
                ThrowUtils.ThrowIf_NullArgument(key, marshaller);
                throwIfKeyInvalid();

                return;

                void throwIfKeyInvalid()
                {
                    bool haveInvalidChars = key.Any(c => !char.IsLetterOrDigit(c) && c != '_');

                    if (haveInvalidChars)
                    {
                        throw new INIBuilderException();
                    }
                }
            }

            string pack()
            {
                var success = marshaller.TryPack(value, out string packed);

                if (!success)
                {
                    throw new INIBuilderException();
                }
                else if (_invalidCharsReplacer.GetType() != marshaller.GetType())
                {
                    success = _invalidCharsReplacer.TryPack(packed, out packed);
                    if (!success)
                    {
                        throw new INIBuilderException();
                    }
                }

                return(packed);
            }
        }
Example #2
0
        public void TryPack_StringArray()
        {
            var m = new StringMarshaller();

            Assert.Throws <ArrayPackingNotSupportedException>(() => m.TryPack(new string[] { "one", "two!" }, out ConfigValue result));
        }