Files
spacedrive/crates/sync-generator/src/attribute/mod.rs
Ericson "Fogo" Soares 2d19fad2eb [ENG-1793] Introduce error handling for sd-core-sync crate (#2556)
* Error handling for sd-core-sync crate
Also a bunch of tweaks and fixes

* Update core.ts

* Auto format

* Comment out background_processing_percentage on frontend too
 - Update rust version in contributing

* Trying to avoid data loss on actor stop

* Stronger linter on sync generator

* Stronger lint on sync subcrate

* Clean back tasks.json file

* TS typechecking

* Autoformat

* Add more verbose errors for Cloud REST API

* Removing some comments

Copilot is fun sometimes lol

* Properly stopping actors

* Fix ingest stop

* Racing on stop for actors

* Error conversion from merge with main

---------

Co-authored-by: Vítor Vasconcellos <vasconcellos.dev@gmail.com>
Co-authored-by: Arnab Chakraborty <11457760+Rocky43007@users.noreply.github.com>
2024-07-16 22:30:35 +00:00

56 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 const fn as_single(&self) -> Option<&str> {
if let AttributeFieldValue::Single(field) = self {
Some(field)
} else {
None
}
}
pub const fn as_list(&self) -> Option<&Vec<&str>> {
if let AttributeFieldValue::List(fields) = self {
Some(fields)
} else {
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_map(|(n, v)| (*n == name).then_some(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()
}