lib/fugit/nat.rb in fugit-1.11.0 vs lib/fugit/nat.rb in fugit-1.11.1
- old
+ new
@@ -5,20 +5,32 @@
# A natural language set of parsers for fugit.
# Focuses on cron expressions. The rest is better left to Chronic and friends.
#
module Nat
+ MAX_INPUT_LENGTH = 256
+
class << self
def parse(s, opts={})
return s if s.is_a?(Fugit::Cron) || s.is_a?(Fugit::Duration)
return nil unless s.is_a?(String)
s = s.strip
+ if s.length > MAX_INPUT_LENGTH
+
+ fail ArgumentError.new(
+ "input too long for a nat string, " +
+ "#{s.length} > #{MAX_INPUT_LENGTH}"
+ ) if opts[:do_parse]
+
+ return nil
+ end
+
#p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true)
#(p s; Raabro.pp(Parser.parse(s, debug: 1), colours: true)) rescue nil
if slots = Parser.parse(s)
slots.to_crons(opts.merge(_s: s))
@@ -27,10 +39,10 @@
end
end
def do_parse(s, opts={})
- parse(s, opts) ||
+ parse(s, opts.merge(do_parse: true)) ||
fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end
end
module Parser include Raabro