2007-02-18 21:12:02 +03:00
|
|
|
module AccountName
|
|
|
|
where
|
|
|
|
import Utils
|
2007-07-02 18:54:36 +04:00
|
|
|
import Types
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-07-03 12:46:39 +04:00
|
|
|
sepchar = ':'
|
|
|
|
|
2007-02-18 21:12:02 +03:00
|
|
|
accountNameComponents :: AccountName -> [String]
|
2007-07-03 12:46:39 +04:00
|
|
|
accountNameComponents = splitAtElement sepchar
|
2007-02-18 21:12:02 +03:00
|
|
|
|
|
|
|
accountNameFromComponents :: [String] -> AccountName
|
2007-07-03 12:46:39 +04:00
|
|
|
accountNameFromComponents = concat . intersperse [sepchar]
|
2007-02-18 21:12:02 +03:00
|
|
|
|
|
|
|
accountLeafName :: AccountName -> String
|
2007-03-11 00:24:57 +03:00
|
|
|
accountLeafName = last . accountNameComponents
|
2007-02-18 21:12:02 +03:00
|
|
|
|
|
|
|
accountNameLevel :: AccountName -> Int
|
2007-07-03 12:46:39 +04:00
|
|
|
accountNameLevel "" = 0
|
|
|
|
accountNameLevel a = (length $ filter (==sepchar) a) + 1
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | ["a:b:c","d:e"] -> ["a","a:b","a:b:c","d","d:e"]
|
2007-02-18 21:12:02 +03:00
|
|
|
expandAccountNames :: [AccountName] -> [AccountName]
|
|
|
|
expandAccountNames as = nub $ concat $ map expand as
|
|
|
|
where expand as = map accountNameFromComponents (tail $ inits $ accountNameComponents as)
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | ["a:b:c","d:e"] -> ["a","d"]
|
2007-02-18 21:12:02 +03:00
|
|
|
topAccountNames :: [AccountName] -> [AccountName]
|
|
|
|
topAccountNames as = [a | a <- expandAccountNames as, accountNameLevel a == 1]
|
|
|
|
|
2007-02-20 00:20:06 +03:00
|
|
|
parentAccountName :: AccountName -> AccountName
|
2007-03-11 00:24:57 +03:00
|
|
|
parentAccountName a = accountNameFromComponents $ init $ accountNameComponents a
|
2007-02-20 00:20:06 +03:00
|
|
|
|
|
|
|
parentAccountNames :: AccountName -> [AccountName]
|
|
|
|
parentAccountNames a = parentAccountNames' $ parentAccountName a
|
|
|
|
where
|
|
|
|
parentAccountNames' "" = []
|
|
|
|
parentAccountNames' a = [a] ++ (parentAccountNames' $ parentAccountName a)
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-07-03 12:46:39 +04:00
|
|
|
isAccountNamePrefixOf :: AccountName -> AccountName -> Bool
|
|
|
|
p `isAccountNamePrefixOf` s = ((p ++ [sepchar]) `isPrefixOf` s)
|
|
|
|
|
|
|
|
isSubAccountNameOf :: AccountName -> AccountName -> Bool
|
2007-02-18 21:12:02 +03:00
|
|
|
s `isSubAccountNameOf` p =
|
2007-07-03 03:41:07 +04:00
|
|
|
(p `isAccountNamePrefixOf` s) && (accountNameLevel s == (accountNameLevel p + 1))
|
2007-02-18 21:12:02 +03:00
|
|
|
|
|
|
|
subAccountNamesFrom :: [AccountName] -> AccountName -> [AccountName]
|
|
|
|
subAccountNamesFrom accts a = filter (`isSubAccountNameOf` a) accts
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | We could almost get by with just the above, but we need smarter
|
2007-02-18 21:12:02 +03:00
|
|
|
-- structures to eg display the account tree with boring accounts elided.
|
|
|
|
-- first, here is a tree of AccountNames; Account and Account tree are
|
|
|
|
-- defined later.
|
|
|
|
|
|
|
|
accountNameTreeFrom_props =
|
|
|
|
[
|
2007-03-10 06:16:19 +03:00
|
|
|
accountNameTreeFrom ["a"] == Node "top" [Node "a" []],
|
|
|
|
accountNameTreeFrom ["a","b"] == Node "top" [Node "a" [], Node "b" []],
|
|
|
|
accountNameTreeFrom ["a","a:b"] == Node "top" [Node "a" [Node "a:b" []]],
|
|
|
|
accountNameTreeFrom ["a:b"] == Node "top" [Node "a" [Node "a:b" []]]
|
2007-02-18 21:12:02 +03:00
|
|
|
]
|
|
|
|
accountNameTreeFrom :: [AccountName] -> Tree AccountName
|
|
|
|
accountNameTreeFrom accts =
|
2007-03-10 06:16:19 +03:00
|
|
|
Node "top" (accountsFrom (topAccountNames accts))
|
2007-02-18 21:12:02 +03:00
|
|
|
where
|
|
|
|
accountsFrom :: [AccountName] -> [Tree AccountName]
|
|
|
|
accountsFrom [] = []
|
2007-03-10 06:16:19 +03:00
|
|
|
accountsFrom as = [Node a (accountsFrom $ subs a) | a <- as]
|
2007-02-18 21:12:02 +03:00
|
|
|
subs = (subAccountNamesFrom accts)
|
|
|
|
|