/*
 * ParseResult.vala
 * 
 * Result type for feed parsing operations
 */

public class RSSuper.ParseError : Object {
    public string message { get; private set; }
    public int code { get; private set; }

    public ParseError(string message, int code = 0) {
        this.message = message;
        this.code = code;
    }
}

public class RSSuper.ParseResult : Object {
    private Object? _value;
    private ParseError? _error;
    public bool ok { get; private set; }
    private Type _value_type;

    private ParseResult() {}

    public static ParseResult success(Object value) {
        var result = new ParseResult();
        result.ok = true;
        result._value = value;
        result._value_type = value.get_type();
        return result;
    }

    public static ParseResult error(string message, int code = 0) {
        var result = new ParseResult();
        result.ok = false;
        result._error = new ParseError(message, code);
        return result;
    }

    public Object? get_value() {
        return this._value;
    }

    public T? get_value_as<T>() {
        if (!ok) {
            return null;
        }
        if (_value is T) {
            return (T)_value;
        }
        return null;
    }

    public ParseError? get_error() {
        return this._error;
    }

    public bool is_type<T>() {
        return ok && _value_type == typeof(T);
    }
}
