Improve error handling and reporting in the LED pattern parser
[led-pattern-ed] / src / led-pattern-rx44.vala
index 5b9554c..ad2d0dd 100644 (file)
@@ -21,26 +21,24 @@ class LedPatternRX44 : LedPattern {
        public List<LedCommandRX44> engine_g;
        public List<LedCommandRX44> engine_b;
 
-       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[3].length > 16*4 || p[4].length > 16*4 || p[5].length > 16*4) {
-                       print ("pattern too long!\n");
-                       return false;
-               }
+               if (p[3].length > 16*4 || p[4].length > 16*4 || p[5].length > 16*4)
+                       throw new LedPatternError.INVALID_PATTERN ("%s engine pattern too long!".printf (name));
+
+               if (p[3].length % 4 != 0 || 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 ();
@@ -49,7 +47,6 @@ class LedPatternRX44 : LedPattern {
                engine_b = parse_pattern (p[5]);
 
                on_changed ();
-               return true;
        }
 
        private List<LedCommandRX44> parse_pattern (string pattern) {