2018-12-24 14:00:14 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
use ra_syntax::{SmolStr, ast::{self, NameOwner}};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
DefId, Cancelable,
|
|
|
|
db::{HirDatabase},
|
|
|
|
ty::{Ty},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Struct {
|
|
|
|
def_id: DefId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Struct {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Struct { def_id }
|
|
|
|
}
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<StructData>> {
|
|
|
|
Ok(db.struct_data(self.def_id)?)
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> {
|
|
|
|
Ok(db.struct_data(self.def_id)?.name.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructData {
|
|
|
|
name: SmolStr,
|
2018-12-24 14:00:14 -06:00
|
|
|
variant_data: Arc<VariantData>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StructData {
|
|
|
|
pub(crate) fn new(struct_def: ast::StructDef) -> StructData {
|
|
|
|
let name = struct_def
|
|
|
|
.name()
|
|
|
|
.map(|n| n.text())
|
|
|
|
.unwrap_or(SmolStr::new("[error]"));
|
|
|
|
let variant_data = VariantData::Unit; // TODO implement this
|
2018-12-24 14:00:14 -06:00
|
|
|
let variant_data = Arc::new(variant_data);
|
2018-12-24 12:07:48 -06:00
|
|
|
StructData { name, variant_data }
|
|
|
|
}
|
2018-12-24 14:00:14 -06:00
|
|
|
|
|
|
|
pub fn name(&self) -> &SmolStr {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variant_data(&self) -> &Arc<VariantData> {
|
|
|
|
&self.variant_data
|
|
|
|
}
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
|
|
|
def_id: DefId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Enum {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
Enum { def_id }
|
|
|
|
}
|
|
|
|
|
2018-12-24 14:00:14 -06:00
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def_id
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> {
|
|
|
|
Ok(db.enum_data(self.def_id)?.name.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct EnumData {
|
|
|
|
name: SmolStr,
|
2018-12-24 14:00:14 -06:00
|
|
|
variants: Vec<(SmolStr, Arc<VariantData>)>,
|
2018-12-24 12:07:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumData {
|
|
|
|
pub(crate) fn new(enum_def: ast::EnumDef) -> Self {
|
|
|
|
let name = enum_def
|
|
|
|
.name()
|
|
|
|
.map(|n| n.text())
|
|
|
|
.unwrap_or(SmolStr::new("[error]"));
|
|
|
|
let variants = Vec::new(); // TODO implement this
|
|
|
|
EnumData { name, variants }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct StructField {
|
|
|
|
name: SmolStr,
|
|
|
|
ty: Ty,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fields of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum VariantData {
|
|
|
|
Struct(Vec<StructField>),
|
|
|
|
Tuple(Vec<StructField>),
|
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantData {
|
|
|
|
pub fn fields(&self) -> &[StructField] {
|
|
|
|
match *self {
|
|
|
|
VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields,
|
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_struct(&self) -> bool {
|
|
|
|
if let VariantData::Struct(..) = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_tuple(&self) -> bool {
|
|
|
|
if let VariantData::Tuple(..) = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_unit(&self) -> bool {
|
|
|
|
if let VariantData::Unit = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|