Trait codes_common::Data

source ·
pub trait Data {
Show 15 methods fn new(type_name: &'static str) -> Self
    where
        Self: Sized
; fn new_with_inner(
        type_name: &'static str,
        inner_type_name: &'static str
    ) -> Self
    where
        Self: Sized
; fn type_name(&self) -> &'static str; fn inner_type_name(&self) -> Option<&'static str>; fn rows(&self) -> &DataMap; fn rows_mut(&mut self) -> &mut DataMap; fn into_rows(self) -> DataMap; fn has_inner_type(&self) -> bool { ... } fn all_ids(&self) -> Vec<&String> { ... } fn all_ids_sorted(&self) -> Value { ... } fn contains(&self, id: &str) -> bool { ... } fn get(&self, id: &str) -> Option<&DataRow> { ... } fn get_mut(&mut self, id: &str) -> Option<&mut DataRow> { ... } fn insert_row(&mut self, id: &str, row: DataRow) { ... } fn insert_row_value(&mut self, id: &str, key: &str, value: Value) { ... }
}

Required Methods§

Provided Methods§

Examples found in repository?
src/lib.rs (line 110)
109
110
111
112
113
114
115
116
117
118
119
    fn all_ids_sorted(&self) -> Value {
        let mut all_ids = self.all_ids();
        all_ids.sort();
        all_ids.dedup();
        Value::Array(
            all_ids
                .into_iter()
                .map(|s| Value::String(s.into()))
                .collect(),
        )
    }
Examples found in repository?
src/lib.rs (line 331)
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
pub fn default_finalize_for<T>(data: T) -> Result<tera::Context, Box<dyn std::error::Error>>
where
    T: Data,
{
    let mut ctx = tera::Context::new();

    ctx.insert("type_name", &Value::String(data.type_name().into()));

    if let Some(inner_type_name) = data.inner_type_name() {
        ctx.insert("inner_type_name", &Value::String(inner_type_name.into()));
    }

    ctx.insert("all_ids", &data.all_ids_sorted());

    ctx.insert(
        "codes",
        &Value::Object(
            data.into_rows()
                .into_iter()
                .map(|(key, value)| (key, Value::Object(value)))
                .collect(),
        ),
    );

    Ok(ctx)
}
Examples found in repository?
src/lib.rs (line 136)
135
136
137
138
    fn insert_row_value(&mut self, id: &str, key: &str, value: Value) {
        let row = self.get_mut(id).unwrap();
        row.insert(key.to_string(), value);
    }
Examples found in repository?
src/csv.rs (line 99)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
fn process_vsv_input<D, F>(
    mut data: D,
    file_name: &str,
    delimiter: u8,
    process_row: F,
) -> Result<D, Box<dyn std::error::Error>>
where
    D: Data,
    F: Fn(StringRecord, &mut DataRow) -> Result<String, Box<dyn std::error::Error>>,
{
    let mut rdr = open_csv_file(file_name, Some(delimiter))?;

    for result in rdr.records() {
        let record = result?;

        let mut row: Map<String, Value> = Default::default();

        let id = process_row(record, &mut row)?;

        data.insert_row(&id, row);
    }

    Ok(data)
}

Implementors§