mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-04-29 02:42:47 -04:00
52 lines
1.1 KiB
Rust
52 lines
1.1 KiB
Rust
use prisma_client_rust_sdk::prisma::prisma_models::{ast::WithDocumentation, walkers::ModelWalker};
|
|
|
|
mod parser;
|
|
|
|
#[derive(Debug)]
|
|
pub enum AttributeFieldValue<'a> {
|
|
Single(&'a str),
|
|
List(Vec<&'a str>),
|
|
}
|
|
|
|
#[allow(unused)]
|
|
impl AttributeFieldValue<'_> {
|
|
pub fn as_single(&self) -> Option<&str> {
|
|
match self {
|
|
AttributeFieldValue::Single(field) => Some(field),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn as_list(&self) -> Option<&Vec<&str>> {
|
|
match self {
|
|
AttributeFieldValue::List(fields) => Some(fields),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Attribute<'a> {
|
|
pub name: &'a str,
|
|
pub fields: Vec<(&'a str, AttributeFieldValue<'a>)>,
|
|
}
|
|
|
|
impl<'a> Attribute<'a> {
|
|
pub fn parse(input: &'a str) -> Result<Self, ()> {
|
|
parser::parse(input).map(|(_, a)| a).map_err(|_| ())
|
|
}
|
|
|
|
pub fn field(&self, name: &str) -> Option<&AttributeFieldValue> {
|
|
self.fields.iter().find(|(n, _)| *n == name).map(|(_, v)| v)
|
|
}
|
|
}
|
|
|
|
pub fn model_attributes(model: ModelWalker) -> Vec<Attribute> {
|
|
model
|
|
.ast_model()
|
|
.documentation()
|
|
.as_ref()
|
|
.map(|docs| docs.lines().flat_map(Attribute::parse).collect())
|
|
.unwrap_or_default()
|
|
}
|