using System;
using Microsoft.CodeAnalysis;
namespace Vellum.Generator
{
///
/// Describes a class that lists what it derives from and doesn't say it's [Saveable] itself, as plain data: either
/// a [Saveable] type by inheritance, or a class to check for a converter of its own.
///
public sealed class Inheritor : IEquatable
{
///
/// Holds the type's model, or null if it doesn't inherit [Saveable].
///
public readonly SaveableModel Model;
///
/// Holds what to check of the type, or null if it inherits [Saveable] or can't be saved at all.
///
public readonly UnsavedSubtype Subtype;
public Inheritor(SaveableModel model, UnsavedSubtype subtype)
{
Model = model;
Subtype = subtype;
}
///
/// Returns as plain data, or null if there's nothing to do for it: it says it's
/// [Saveable] itself, so the attribute is what finds it.
///
public static Inheritor Of(INamedTypeSymbol type)
{
if (SaveableClass.HasOwnMark(type))
{
return null;
}
if (SaveableClass.InheritsMark(type))
{
return new Inheritor(SaveableClass.Describe(type), subtype: null);
}
UnsavedSubtype subtype = UnsavedSubtype.Of(type);
if (subtype == null)
{
return null;
}
return new Inheritor(model: null, subtype);
}
public bool Equals(Inheritor other)
{
return other != null && Equals(Model, other.Model) && Equals(Subtype, other.Subtype);
}
public override bool Equals(object obj)
{
return Equals(obj as Inheritor);
}
public override int GetHashCode()
{
if (Model != null)
{
return Model.GetHashCode();
}
if (Subtype != null)
{
return Subtype.GetHashCode();
}
return 0;
}
}
}