Init RaspberryIO

This commit is contained in:
2019-02-17 14:08:57 +01:00
commit e7c3f7af91
193 changed files with 36743 additions and 0 deletions
@@ -0,0 +1,102 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// Models an option specification.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArgumentOptionAttribute
: Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// The default long name will be inferred from target property.
/// </summary>
public ArgumentOptionAttribute()
: this(string.Empty, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="longName">The long name of the option.</param>
public ArgumentOptionAttribute(string longName)
: this(string.Empty, longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option or null if not used.</param>
public ArgumentOptionAttribute(char shortName, string longName)
: this(new string(shortName, 1), longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option..</param>
public ArgumentOptionAttribute(char shortName)
: this(new string(shortName, 1), string.Empty)
{
}
private ArgumentOptionAttribute(string shortName, string longName)
{
ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName));
LongName = longName ?? throw new ArgumentNullException(nameof(longName));
}
/// <summary>
/// Gets long name of this command line option. This name is usually a single English word.
/// </summary>
/// <value>
/// The long name.
/// </value>
public string LongName { get; }
/// <summary>
/// Gets a short name of this command line option, made of one character.
/// </summary>
/// <value>
/// The short name.
/// </value>
public string ShortName { get; }
/// <summary>
/// When applying attribute to <see cref="System.Collections.Generic.IEnumerable{T}"/> target properties,
/// it allows you to split an argument and consume its content as a sequence.
/// </summary>
public char Separator { get; set; } = '\0';
/// <summary>
/// Gets or sets mapped property default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a command line option is required.
/// </summary>
/// <value>
/// <c>true</c> if required; otherwise, <c>false</c>.
/// </value>
public bool Required { get; set; }
/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
}
}
@@ -0,0 +1,13 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// Represents an attribute to select which properties are copyable between objects.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public class CopyableAttribute : Attribute
{
}
}
@@ -0,0 +1,39 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// An attribute used to help setup a property behavior when serialize/deserialize JSON.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class JsonPropertyAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyAttribute" /> class.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="ignored">if set to <c>true</c> [ignored].</param>
public JsonPropertyAttribute(string propertyName, bool ignored = false)
{
PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
Ignored = ignored;
}
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public string PropertyName { get; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="JsonPropertyAttribute" /> is ignored.
/// </summary>
/// <value>
/// <c>true</c> if ignored; otherwise, <c>false</c>.
/// </value>
public bool Ignored { get; }
}
}
@@ -0,0 +1,54 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// An attribute used to include additional information to a Property for serialization.
///
/// Previously we used DisplayAttribute from DataAnnotation.
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyDisplayAttribute : Attribute
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; set; }
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public string GroupName { get; set; }
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets the format string to call with method <c>ToString</c>.
/// </summary>
/// <value>
/// The format.
/// </value>
public string Format { get; set; }
}
}
@@ -0,0 +1,30 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// An attribute used to help conversion structs back and forth into arrays of bytes via
/// extension methods included in this library ToStruct and ToBytes.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
public class StructEndiannessAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="StructEndiannessAttribute"/> class.
/// </summary>
/// <param name="endianness">The endianness.</param>
public StructEndiannessAttribute(Endianness endianness)
{
Endianness = endianness;
}
/// <summary>
/// Gets the endianness.
/// </summary>
/// <value>
/// The endianness.
/// </value>
public Endianness Endianness { get; }
}
}
@@ -0,0 +1,133 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System.Text.RegularExpressions;
using Abstractions;
/// <summary>
/// Regex validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class MatchAttribute : Attribute, IValidator
{
/// <summary>
/// Initializes a new instance of the <see cref="MatchAttribute" /> class.
/// </summary>
/// <param name="regex">A regex string.</param>
/// <param name="errorMessage">The error message.</param>
/// <exception cref="ArgumentNullException">Expression.</exception>
public MatchAttribute(string regex, string errorMessage = null)
{
Expression = regex ?? throw new ArgumentNullException(nameof(Expression));
ErrorMessage = errorMessage ?? "String does not match the specified regular expression";
}
/// <summary>
/// The string regex used to find a match.
/// </summary>
public string Expression { get; }
/// <inheritdoc/>
public string ErrorMessage { get; internal set; }
/// <inheritdoc/>
public bool IsValid<T>(T value)
{
if (Equals(value, default(T)))
return false;
return !(value is string)
? throw new ArgumentException("Property is not a string")
: Regex.IsMatch(value.ToString(), Expression);
}
}
/// <summary>
/// Email validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class EmailAttribute : MatchAttribute
{
private const string EmailRegExp =
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
/// <summary>
/// Initializes a new instance of the <see cref="EmailAttribute"/> class.
/// </summary>
/// <param name="errorMessage">The error message.</param>
public EmailAttribute(string errorMessage = null)
: base(EmailRegExp, errorMessage ?? "String is not an email")
{
}
}
/// <summary>
/// A not null validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute, IValidator
{
/// <inheritdoc/>
public string ErrorMessage => "Value is null";
/// <inheritdoc/>
public bool IsValid<T>(T value) => !Equals(default(T), value);
}
/// <summary>
/// A range constraint validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : Attribute, IValidator
{
/// <summary>
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes integer minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(int min, int max)
{
if (min >= max)
throw new InvalidOperationException("Maximum value must be greater than minimum");
Maximum = max;
Minimum = min;
}
/// <summary>
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes double minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(double min, double max)
{
if (min >= max)
throw new InvalidOperationException("Maximum value must be greater than minimum");
Maximum = max;
Minimum = min;
}
/// <inheritdoc/>
public string ErrorMessage => "Value is not within the specified range";
/// <summary>
/// Maximum value for the range.
/// </summary>
public IComparable Maximum { get; }
/// <summary>
/// Minimum value for the range.
/// </summary>
public IComparable Minimum { get; }
/// <inheritdoc/>
public bool IsValid<T>(T value)
=> value is IComparable comparable
? comparable.CompareTo(Minimum) >= 0 && comparable.CompareTo(Maximum) <= 0
: throw new ArgumentException(nameof(value));
}
}
@@ -0,0 +1,40 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// Models a verb option.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class VerbOptionAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="VerbOptionAttribute" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name.</exception>
public VerbOptionAttribute(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
/// <summary>
/// Gets the name of the verb option.
/// </summary>
/// <value>
/// Name.
/// </value>
public string Name { get; }
/// <summary>
/// Gets or sets a short description of this command line verb. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
/// <inheritdoc />
public override string ToString() => $" {Name}\t\t{HelpText}";
}
}