mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 04:45:05 +00:00
move variants to API
This commit is contained in:
parent
71c7936932
commit
2d0ab52212
3 changed files with 34 additions and 29 deletions
|
@ -3,7 +3,7 @@ use std::sync::Arc;
|
||||||
use ra_syntax::ast::{self, NameOwner, StructFlavor};
|
use ra_syntax::ast::{self, NameOwner, StructFlavor};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefId, Name, AsName, Struct, Enum,
|
DefId, Name, AsName, Struct, Enum, VariantData, StructField,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -67,30 +67,6 @@ impl EnumData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A single field of an enum variant or struct
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub struct StructField {
|
|
||||||
name: Name,
|
|
||||||
type_ref: TypeRef,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StructField {
|
|
||||||
pub fn name(&self) -> Name {
|
|
||||||
self.name.clone()
|
|
||||||
}
|
|
||||||
pub fn type_ref(&self) -> &TypeRef {
|
|
||||||
&self.type_ref
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fields of an enum variant or struct
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub enum VariantData {
|
|
||||||
Struct(Vec<StructField>),
|
|
||||||
Tuple(Vec<StructField>),
|
|
||||||
Unit,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VariantData {
|
impl VariantData {
|
||||||
pub fn new(flavor: StructFlavor) -> Self {
|
pub fn new(flavor: StructFlavor) -> Self {
|
||||||
match flavor {
|
match flavor {
|
||||||
|
@ -122,8 +98,8 @@ impl VariantData {
|
||||||
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
|
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
|
||||||
self.fields()
|
self.fields()
|
||||||
.iter()
|
.iter()
|
||||||
.find(|f| f.name == *field_name)
|
.find(|f| f.name() == field_name)
|
||||||
.map(|f| &f.type_ref)
|
.map(|f| f.type_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fields(&self) -> &[StructField] {
|
pub fn fields(&self) -> &[StructField] {
|
||||||
|
|
|
@ -4,7 +4,12 @@ use relative_path::RelativePathBuf;
|
||||||
use ra_db::{CrateId, Cancelable, FileId};
|
use ra_db::{CrateId, Cancelable, FileId};
|
||||||
use ra_syntax::{ast, TreePtr, SyntaxNode};
|
use ra_syntax::{ast, TreePtr, SyntaxNode};
|
||||||
|
|
||||||
use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope, adt::VariantData};
|
use crate::{
|
||||||
|
Name, DefId, Path, PerNs,
|
||||||
|
type_ref::TypeRef,
|
||||||
|
nameres::ModuleScope,
|
||||||
|
db::HirDatabase,
|
||||||
|
};
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main inteface with which
|
/// hir::Crate describes a single crate. It's the main inteface with which
|
||||||
/// crate's dependencies interact. Mostly, it should be just a proxy for the
|
/// crate's dependencies interact. Mostly, it should be just a proxy for the
|
||||||
|
@ -114,6 +119,30 @@ impl Module {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A single field of an enum variant or struct
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct StructField {
|
||||||
|
pub(crate) name: Name,
|
||||||
|
pub(crate) type_ref: TypeRef,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StructField {
|
||||||
|
pub fn name(&self) -> &Name {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
pub fn type_ref(&self) -> &TypeRef {
|
||||||
|
&self.type_ref
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fields of an enum variant or struct
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum VariantData {
|
||||||
|
Struct(Vec<StructField>),
|
||||||
|
Tuple(Vec<StructField>),
|
||||||
|
Unit,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Struct {
|
pub struct Struct {
|
||||||
pub(crate) def_id: DefId,
|
pub(crate) def_id: DefId,
|
||||||
|
|
|
@ -59,7 +59,7 @@ pub use self::function::FnSignatureInfo;
|
||||||
pub use self::code_model_api::{
|
pub use self::code_model_api::{
|
||||||
Crate, CrateDependency,
|
Crate, CrateDependency,
|
||||||
Module, ModuleSource, Problem,
|
Module, ModuleSource, Problem,
|
||||||
Struct, Enum,
|
Struct, Enum, VariantData, StructField,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum Def {
|
pub enum Def {
|
||||||
|
|
Loading…
Reference in a new issue