File size: 4,748 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Examples

Here are some practical examples of what you might build using the Firm Rust crates.

## Custom reporting tool

Build a tool that generates weekly reports from your workspace:

```rust,no_run
use firm_lang::workspace::Workspace;
use firm_core::{
    EntityGraph, EntityType, FieldId,
    Query, EntitySelector, QueryOperation,
    FilterCondition, FilterOperator, FilterValue, FieldRef
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load workspace
    let mut workspace = Workspace::new();
    workspace.load_directory("./my_workspace")?;
    let build = workspace.build()?;

    // Build graph
    let mut graph = EntityGraph::new();
    graph.add_entities(build.entities)?;
    graph.build();

    // Find completed tasks
    let query = Query::new(EntitySelector::Type(EntityType::new("task")))
        .with_operation(QueryOperation::Where(
            FilterCondition::new(
                FieldRef::Regular(FieldId::new("is_completed")),
                FilterOperator::Equal,
                FilterValue::Boolean(true),
            )
        ));

    let completed_tasks = query.execute(&graph);

    // Generate report
    println!("Weekly Report");
    println!("Completed {} tasks", completed_tasks.len());

    Ok(())
}
```

## Custom validation

Add business-specific validation rules:

```rust,no_run
use firm_core::{EntityGraph, EntityType, FieldId};

fn validate_business_rules(graph: &EntityGraph) -> Result<(), String> {
    // Ensure every project has an owner
    let projects = graph.list_by_type(&EntityType::new("project"));

    for project in projects {
        if project.get_field(&FieldId::new("owner_ref")).is_none() {
            return Err(format!("Project {} missing owner", project.id));
        }
    }

    // Ensure all opportunities have a value
    let opportunities = graph.list_by_type(&EntityType::new("opportunity"));

    for opp in opportunities {
        if opp.get_field(&FieldId::new("value")).is_none() {
            return Err(format!("Opportunity {} missing value", opp.id));
        }
    }

    Ok(())
}
```

## Data synchronization

Sync data between Firm and external systems:

```rust,no_run
use firm_lang::workspace::Workspace;
use firm_core::{Entity, EntityType, FieldId, FieldValue};

fn sync_from_crm() -> Result<(), Box<dyn std::error::Error>> {
    // Fetch from external CRM
    let crm_contacts = fetch_from_crm()?;

    // Load existing workspace
    let mut workspace = Workspace::new();
    workspace.load_directory("./workspace")?;

    // Create or update contacts
    for crm_contact in crm_contacts {
        let entity = Entity::new(
            EntityId::new(&crm_contact.id),
            EntityType::new("contact")
        )
        .with_field(FieldId::new("name"), FieldValue::String(crm_contact.name))
        .with_field(FieldId::new("email"), FieldValue::String(crm_contact.email));

        // Generate DSL and write to file
        let dsl = generate_dsl(&entity)?;
        std::fs::write(
            format!("./workspace/contacts/{}.firm", crm_contact.id),
            dsl
        )?;
    }

    Ok(())
}
```

## Automated task creation

Automatically generate tasks based on events:

```rust,no_run
use firm_core::{Entity, EntityGraph, EntityType, EntityId, FieldId, FieldValue, ReferenceValue};
use firm_lang::generator::generate_dsl;

fn create_followup_tasks(graph: &EntityGraph) -> Result<(), Box<dyn std::error::Error>> {
    // Find interactions from this week without follow-up tasks
    let interactions = graph.list_by_type(&EntityType::new("interaction"));

    for interaction in interactions {
        // Check if follow-up task exists
        let related = graph.get_related(&interaction.id, None);
        let has_followup = related
            .map(|entities| {
                entities.iter().any(|e| e.entity_type == EntityType::new("task"))
            })
            .unwrap_or(false);

        if !has_followup {
            // Create follow-up task
            let task = Entity::new(
                EntityId::new(&format!("followup_{}", interaction.id.as_str())),
                EntityType::new("task")
            )
            .with_field(
                FieldId::new("name"),
                FieldValue::String(format!("Follow up on {}", interaction.id.as_str()))
            )
            .with_field(
                FieldId::new("source_ref"),
                FieldValue::Reference(ReferenceValue::Entity(interaction.id.clone()))
            );

            // Write to file
            let dsl = generate_dsl(&task)?;
            std::fs::write(
                format!("./workspace/tasks/followup_{}.firm", interaction.id.as_str()),
                dsl
            )?;
        }
    }

    Ok(())
}
```