openprxl 0.1.0

A Rust spreadsheet library inspired by Python's openpyxl
Documentation
//! Shared string table used by `.xlsx` files.

use indexmap::IndexMap;

/// A shared string table.
#[derive(Debug, Clone, Default)]
pub struct SharedStrings {
    strings: IndexMap<String, usize>,
    items: Vec<String>,
}

impl SharedStrings {
    /// Create an empty shared string table.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a string and return its index.
    pub fn add<S: Into<String>>(&mut self, s: S) -> usize {
        let s = s.into();
        if let Some(&idx) = self.strings.get(&s) {
            return idx;
        }
        let idx = self.items.len();
        self.strings.insert(s.clone(), idx);
        self.items.push(s);
        idx
    }

    /// Get the string at the given index.
    pub fn get(&self, idx: usize) -> Option<&str> {
        self.items.get(idx).map(|s| s.as_str())
    }

    /// Return the number of unique strings.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Iterate all unique strings in index order.
    pub fn iter(&self) -> impl Iterator<Item = (usize, &str)> {
        self.items.iter().enumerate().map(|(i, s)| (i, s.as_str()))
    }
}