hardcache

Types

pub type Cache {
  Cache(
    file_path: String,
    entries: List(#(String, String)),
    auto_update: Bool,
    decode: fn(String) -> List(#(String, String)),
    encode: fn(List(#(String, String))) -> String,
  )
}

Constructors

  • Cache(
      file_path: String,
      entries: List(#(String, String)),
      auto_update: Bool,
      decode: fn(String) -> List(#(String, String)),
      encode: fn(List(#(String, String))) -> String,
    )
pub type CacheError {
  FileError(file_stream_error.FileStreamError)
}

Constructors

  • FileError(file_stream_error.FileStreamError)

Functions

pub fn auto_update(cache: Cache) -> Cache

Sets the auto_update field of the cache to True.

Examples

let cache = hardcache.new("./example.txt", False)
let cache = case cache {
  Ok(cache) -> Ok(hardcache.auto_update(cache))
  Error(e) -> Error(e)
}
pub fn default_decoder(string: String) -> List(#(String, String))

Parses a string into key-value pairs.

Example

hardcache.default_decoder("!key=value\n")
// -> [#("key", "value")]
pub fn default_encoder(
  entries: List(#(String, String)),
) -> String

Converts key-value pairs to a string.

Example

hardcache.default_encoder([#("key", "value")])
// -> "!key=value\n"
pub fn defaults(
  in orig: Cache,
  defaults entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets a key-value pair in the list if it is not already set.

Examples

let cache = case hardcache.new("./example.txt", True) {
  Ok(cache) -> hardcache.defaults(cache, [#("msg", "Hello!")])
  err -> err
}
pub fn get(from cache: Cache, key key: String) -> Option(String)

Gets the value associated with a key in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
let a = case cache {
  Ok(cache) ->
    cache
    |> hardcache.get("a")
  Error(_) -> option.None
}
pub fn get_unwrap(
  in orig: Cache,
  key key: String,
  or when_none: String,
) -> String

Gets a string assigned to the key (2nd argument) or returns the string or (3rd argument) when there’s no value assigned to the key.

Example

let cache = hardcache.new("./example.txt", True)
let a =
  cache
  |> hardcache.get_unwrap("test", "(none)")
// -> "(none)"
pub fn new(
  path file_path: String,
  auto_update auto_update: Bool,
) -> Result(Cache, CacheError)

Creates a new cache by reading from the specified file path. Uses the default encoder and decoder.

Examples

let cache = hardcache.new("./example.txt", True)
pub fn new_custom_format(
  path file_path: String,
  auto_update auto_update: Bool,
  decoder decode: fn(String) -> List(#(String, String)),
  encoder encode: fn(List(#(String, String))) -> String,
) -> Result(Cache, CacheError)

Creates a new cache by reading from the specified file path.

Examples

let cache =
  hardcache.new_custom_format("./example.txt", True, hardcache.default_decoder, hardcache.default_encoder)
pub fn remove(
  from orig: Cache,
  key key: String,
) -> Result(Cache, CacheError)

Removes a key-value pair from the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
// -> Ok(Cache("./example.txt", "!a=A\n", True))
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.remove("a")
  Error(e) -> Error(e)
}
// -> Ok(Cache("./example.txt", "", True))
pub fn set(
  in orig: Cache,
  key key: String,
  value value: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache.

Examples

let cache = hardcache.new("./example.txt", True)
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.set("a", "A")
  Error(e) -> Error(e)
}
pub fn set_many(
  in orig: Cache,
  entries entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets multiple key-value pairs in the cache.

Examples

let cache = hardcache.new("./example.txt", True)
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.set_many([#("a", "A"), #("b", "B")])
  Error(e) -> Error(e)
}
pub fn try_defaults(
  in orig: Result(Cache, CacheError),
  defaults entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets a key-value pair in the list if it is not already set or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_defaults(cache, [#("msg", "Hello!")])
pub fn try_get(
  from orig: Result(Cache, CacheError),
  key key: String,
) -> Result(Option(String), CacheError)

Sets multiple key-value pairs in the cache or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
let a =
  cache
  |> hardcache.try_get("a")
pub fn try_get_unwrap(
  in orig: Result(Cache, CacheError),
  key key: String,
  or when_none_or_error: String,
) -> String

Gets a string assigned to the key (2nd argument) or returns the string or (3rd argument) when unsuccessful (when the 1st argument is a CacheError or when there’s no value assigned to the key).

Example

let cache =
  hardcache.new("./non_existent_directory/example.txt", True)
  |> hardcache.try_set("test", "something important") // propagates the error from `new`
// -> Error(FileError(Enoent))
let a =
  cache
  |> hardcache.get_unwrap("test", "(none)")
// -> "(none)"
pub fn try_remove(
  from orig: Result(Cache, CacheError),
  key key: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
// -> Ok(Cache("./example.txt", "!a=A\n", True))
let cache =
  cache
  |> hardcache.try_remove("a")
// -> Ok(Cache("./example.txt", "", True))
pub fn try_set(
  in orig: Result(Cache, CacheError),
  key key: String,
  value value: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
pub fn try_set_many(
  in orig: Result(Cache, CacheError),
  entries entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets multiple key-value pairs in the cache or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set_many([#("a", "A"), #("b", "B")])
pub fn try_update(
  cache: Result(Cache, CacheError),
) -> Result(Cache, CacheError)

Updates the cache file with the current cache content or propagates the error.

Examples

let cache =
  hardcache.new("./example.txt", False)
  |> hardcache.try_set("a", "A")
  |> hardcache.try_set("b", "B")
  |> hardcache.try_set_many([#("c", "C")])
  |> hardcache.try_update
pub fn update(cache: Cache) -> Result(Cache, CacheError)

Updates the cache file with the current cache content.

Examples

let cache =
  hardcache.new("./example.txt", False)
  |> hardcache.try_set("a", "A")
  |> hardcache.try_set("b", "B")
  |> hardcache.try_set_many([#("c", "C")])

let cache = case cache {
  Ok(cache) -> hardcache.update(cache)
  Error(e) -> Error(e)
}
Search Document