Exemple #1
0
        static Parsed <Thickness <Points> > TryParseThickness(string str)
        {
            var parts = str.Split(",");

            switch (parts.Length)
            {
            case 1:
                return(parts[0].TryParsePoints()
                       .Select(all => new Thickness <Points>(all)));

            case 2:
                return(Parsed.Create(
                           stringValue: str,
                           value: Optional.Combine(
                               parts[0].TryParsePoints().Value,
                               parts[1].TryParsePoints().Value,
                               (h, v) => new Thickness <Points>(h, v))));

            case 4:
                return(Parsed.Create(
                           stringValue: str,
                           value: Optional.Combine(
                               parts[0].TryParsePoints().Value,
                               parts[1].TryParsePoints().Value,
                               parts[2].TryParsePoints().Value,
                               parts[3].TryParsePoints().Value,
                               (l, t, r, b) => new Thickness <Points>(l, t, r, b))));

            default:
                return(Parsed.Failure <Thickness <Points> >(str));
            }
        }
Exemple #2
0
        // Pixels

        public static Parsed <Pixels> TryParsePixels(this string str)
        {
            double value;

            if (Double.TryParse(str.StripSuffix("px"), out value))
            {
                return(Parsed.Success(new Pixels(value), str));
            }

            return(Parsed.Failure <Pixels>(str));
        }
Exemple #3
0
        public static Parsed <int> TryParseInt(string str)
        {
            int value;

            if (int.TryParse(str, out value))
            {
                return(Parsed.Success(value, str));
            }

            return(Parsed.Failure <int>(str));
        }
Exemple #4
0
        public static Parsed <double> TryParseDouble(string str)
        {
            double value;

            if (double.TryParse(str, out value))
            {
                return(Parsed.Success(value, str));
            }

            return(Parsed.Failure <double>(str));
        }
Exemple #5
0
        public static Parsed <float> TryParseFloat(string str)
        {
            float value;

            if (float.TryParse(str, out value))
            {
                return(Parsed.Success(value, str));
            }

            return(Parsed.Failure <float>(str));
        }
Exemple #6
0
        static Parsed <bool> TryParseBoolean(string str)
        {
            bool value;

            if (Boolean.TryParse(str, out value))
            {
                return(Parsed.Success(value, str));
            }

            return(Parsed.Failure <bool>(str));
        }
Exemple #7
0
        static Parsed <TimeSpan> TryParseTimeSpan(string str)
        {
            double value;

            if (double.TryParse(str.StripSuffix("s"), out value))
            {
                return(Parsed.Success(TimeSpan.FromSeconds(value), str));
            }

            return(Parsed.Failure <TimeSpan>(str));
        }
Exemple #8
0
        static Parsed <T> TryParseEnum <T>(string str)
            where T : struct
        {
            T value;

            if (Enum.TryParse(str, out value) && Enum.IsDefined(typeof(T), value))
            {
                return(Parsed.Success(value, str));
            }

            return(Parsed.Failure <T>(str));
        }
Exemple #9
0
        // Percentages

        public static Parsed <Percentages> TryParsePercentages(this string str)
        {
            if (!str.EndsWith("%"))
            {
                return(ScalarParser.TryParseDouble(str).Select(d => new Percentages(d)));
            }

            double value;

            if (double.TryParse(str.StripSuffix("%"), out value))
            {
                return(Parsed.Success(new Percentages(value), str));
            }

            return(Parsed.Failure <Percentages>(str));
        }
Exemple #10
0
        public static Parsed <Color> TryParseColor(string str)
        {
            if (str.StartsWith("#"))
            {
                return new Parsed <Color>
                       {
                           String = str,
                           Value  = TryParseColorHex(str.AfterFirst("#")),
                       }
            }
            ;

            var vector = str.Split(",").Select(v => v.Trim()).ToArray();

            switch (vector.Length)
            {
            case 3:
                return(new Parsed <Color>
                {
                    String = str,
                    Value =
                        ScalarParser.TryParseFloat(vector[0]).Value.SelectMany(r =>
                                                                               ScalarParser.TryParseFloat(vector[1]).Value.SelectMany(g =>
                                                                                                                                      ScalarParser.TryParseFloat(vector[2]).Value.Select(b =>
                                                                                                                                                                                         new Color(r, g, b, 1))))
                });

            case 4:
                return(new Parsed <Color>
                {
                    String = str,
                    Value =
                        ScalarParser.TryParseFloat(vector[0]).Value.SelectMany(r =>
                                                                               ScalarParser.TryParseFloat(vector[1]).Value.SelectMany(g =>
                                                                                                                                      ScalarParser.TryParseFloat(vector[2]).Value.SelectMany(b =>
                                                                                                                                                                                             ScalarParser.TryParseFloat(vector[3]).Value.Select(a =>
                                                                                                                                                                                                                                                new Color(r, g, b, a)))))
                });
            }
            return(Parsed.Failure <Color>(str));
        }
Exemple #11
0
        public static Func <string, Parsed <Size <T> > > CreateSizeParser <T>(Func <string, Parsed <T> > tryParseLength)
        {
            return(value =>
            {
                var parts = value.Split(",");
                if (parts.Length == 1)
                {
                    return tryParseLength(parts[0]).Select(Size.Create);
                }

                if (parts.Length == 2)
                {
                    return Parsed.Create(value,
                                         tryParseLength(parts[0]).Value.SelectMany(x =>
                                                                                   tryParseLength(parts[1]).Value.Select(y =>
                                                                                                                         Size.Create(x, y))));
                }

                return Parsed.Failure <Size <T> >(value);
            });
        }