File size: 2,149 Bytes
f14b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::{fmt, io, path::PathBuf};

use firm_core::EntityType;

use crate::defaults;

/// Defines the errors you might encounter using a workspace.
#[derive(Debug)]
pub enum WorkspaceError {
    IoError(io::Error),
    ParseError(PathBuf, String),
    ValidationError(PathBuf, String),
    MissingSchemaError(PathBuf, EntityType),
}

impl fmt::Display for WorkspaceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WorkspaceError::IoError(error) => {
                write!(f, "There was a problem reading workspace files: {}", error)
            }
            WorkspaceError::ParseError(path_buf, error) => write!(
                f,
                "Workspace file at {} could not be parsed: {}",
                path_buf.display(),
                error
            ),
            WorkspaceError::ValidationError(path_buf, error) => write!(
                f,
                "Workspace file at {} was invalid: {}",
                path_buf.display(),
                error
            ),
            WorkspaceError::MissingSchemaError(path_buf, entity_type) => {
                let is_default_schema = is_default_schema_type(entity_type);

                if is_default_schema {
                    write!(
                        f,
                        "No schema found for entity type '{}' in {}\n\nRun 'firm init' to create default schemas, or define your own schema in your workspace.",
                        entity_type,
                        path_buf.display()
                    )
                } else {
                    write!(
                        f,
                        "No schema found for entity type '{}' in {}\n\nDefine a schema for this type in your workspace.",
                        entity_type,
                        path_buf.display()
                    )
                }
            }
        }
    }
}

/// Check if an entity type matches one of the default schemas.
fn is_default_schema_type(entity_type: &EntityType) -> bool {
    defaults::all_default_schemas()
        .iter()
        .any(|schema| &schema.entity_type == entity_type)
}