freemarker 0.1.1

Apache FreeMarker 语义兼容的 Rust 模板引擎(freemarker-core 迁移)
Documentation
//! 空缓存存储 —— 对应 Java `freemarker.cache.NullCacheStorage`
//! (不缓存任何条目;get 恒 miss、put/remove/clear 空操作——配合
//! Configuration.setCacheStorage(NullCacheStorage) 关闭模板缓存)

use crate::cache::cache_storage_with_get_size::CacheStorageWithGetSize;
use crate::cache::CacheStorage;
use crate::template::Template;
use std::rc::Rc;

/// 空缓存存储(对应 NullCacheStorage.java)
pub struct NullCacheStorage;

impl CacheStorage for NullCacheStorage {
    fn get(&mut self, _key: &str) -> Option<Rc<Template>> {
        None
    }

    fn put(&mut self, _key: &str, _value: Rc<Template>) {
        // Java NullCacheStorage.put: "do nothing" — 不缓存任何条目
    }

    fn remove(&mut self, _key: &str) {
        // Java NullCacheStorage.remove: "do nothing" — 无条目可移除
    }

    fn clear(&mut self) {
        // Java NullCacheStorage.clear: "do nothing" — 无条目可清空
    }
}

impl CacheStorageWithGetSize for NullCacheStorage {
    fn get_size(&mut self) -> usize {
        0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn null_storage_never_holds() {
        let mut s = NullCacheStorage;
        assert!(s.get("a.ftl").is_none());
        s.put(
            "a.ftl",
            Rc::new(crate::template::Template::new(
                "a.ftl".to_string(),
                Vec::new(),
                std::collections::HashMap::new(),
                std::rc::Rc::new(crate::template::Configuration::default()),
            )),
        );
        assert!(s.get("a.ftl").is_none());
        assert_eq!(s.get_size(), 0);
    }
}