lib.sbn 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. pub mod prelude{
  2. pub use crate::*;
  3. {{~ for ns in __ns ~}}
  4. pub use {{ns}}::*;
  5. {{~end~}}
  6. }
  7. {{~ if __polymorphic_beans.count != 0 ~}}
  8. type AbstractBase = dyn std::any::Any + Sync + Send;
  9. pub trait GetBase<'a, T> {
  10. fn get_base(&'a self) -> Result<T, LubanError>;
  11. }
  12. {{~end~}}
  13. #[derive(Debug)]
  14. pub enum LubanError {
  15. Loader(String),
  16. Table(String),
  17. Bean(String),
  18. Polymorphic(String),
  19. Unknown(String),
  20. }
  21. impl std::fmt::Display for LubanError {
  22. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  23. f.write_str(match self {
  24. LubanError::Loader(msg) |
  25. LubanError::Table(msg) |
  26. LubanError::Bean(msg) |
  27. LubanError::Polymorphic(msg) |
  28. LubanError::Unknown(msg) => msg,
  29. })
  30. }
  31. }
  32. pub struct Tables{
  33. {{~ for table in __tables ~}}
  34. pub {{table.name}}: std::sync::Arc<{{full_name table}}>,
  35. {{~end~}}
  36. }
  37. impl Tables {
  38. pub fn new<T: Fn(&str) -> Result<ByteBuf, LubanError>>(loader: T) -> Result<Tables, LubanError> {
  39. Ok(Tables {
  40. {{~ for table in __tables ~}}
  41. {{table.name}}: {{full_name table}}::new(loader("{{table.output_data_file}}")?)?,
  42. {{~end~}}
  43. })
  44. }
  45. }