Improve error handling and reporting in the LED pattern parser
[led-pattern-ed] / src / led-pattern-rx51.vala
index 1518c4b..b4163e9 100644 (file)
@@ -22,26 +22,24 @@ class LedPatternRX51 : LedPattern {
        public List<LedCommandRX51> engine1;
        public List<LedCommandRX51> engine2;
 
-       public override bool parse (string line) {
+       public override void parse (string line) throws LedPatternError {
                string[] key_value = line.split ("=");
 
-               if (key_value.length != 2) {
-                       print ("pattern line does not contain '=': %s\n", line);
-                       return false;
-               }
+               if (key_value.length != 2)
+                       throw new LedPatternError.INVALID_PATTERN ("pattern line does not contain '=': " + line);
+
+               name = key_value[0];
 
                string[] p = key_value[1].split (";");
-               if (p.length != 6) {
-                       print ("pattern does not contain 6 components: %d\n", p.length);
-                       return false;
-               }
+               if (p.length != 6)
+                       throw new LedPatternError.INVALID_PATTERN ("%s does not contain 6 components: %d".printf (name, p.length));
 
-               if (p[4].length > 16*4 || p[5].length > 16*4) {
-                       print ("pattern too long!\n");
-                       return false;
-               }
+               if (p[4].length > 16*4 || p[5].length > 16*4)
+                       throw new LedPatternError.INVALID_PATTERN ("%s engine pattern too long!".printf (name));
+
+               if (p[4].length % 4 != 0 || p[5].length % 4 != 0)
+                       throw new LedPatternError.INVALID_PATTERN ("%s engine pattern not an even number of bytes!".printf (name));
 
-               name = key_value[0];
                priority = p[0].to_int ();
                screen_on = p[1].to_int ();
                timeout = p[2].to_int ();
@@ -57,7 +55,6 @@ class LedPatternRX51 : LedPattern {
                }
 
                on_changed ();
-               return true;
        }
 
        private void parse_led_map (string led_map, out LedColor color1, out LedColor color2) {
@@ -174,10 +171,12 @@ class LedPatternRX51 : LedPattern {
 
        public void on_changed () {
                bool unresolved = calculate_timing ();
-               if (unresolved)
+               if (unresolved) {
                        unresolved = calculate_timing ();
-               if (unresolved)
+               }
+               if (unresolved) {
                        Hildon.Banner.show_information (null, null, "Timing unresolved");
+               }
                changed ();
        }