Skip to content

Commit 0682302

Browse files
committed
Merge remote-tracking branch 'github/pr/210'
2 parents b55465e + 229fdb3 commit 0682302

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

System/OsPath/Internal.hs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE RankNTypes #-}
33
{-# LANGUAGE UnliftedFFITypes #-}
4+
{-# LANGUAGE TemplateHaskellQuotes #-}
5+
{-# LANGUAGE ViewPatterns #-} -- needed to quote a view pattern
46

57
module System.OsPath.Internal where
68

@@ -111,32 +113,37 @@ fromBytes = OS.fromBytes
111113

112114
-- | QuasiQuote an 'OsPath'. This accepts Unicode characters
113115
-- and encodes as UTF-8 on unix and UTF-16LE on windows. Runs 'isValid'
114-
-- on the input.
116+
-- on the input. If used as a pattern, requires turning on the @ViewPatterns@
117+
-- extension.
115118
osp :: QuasiQuoter
116119
osp = QuasiQuoter
117120
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
118121
{ quoteExp = \s -> do
119122
osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s
120123
when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
121124
lift osp'
122-
, quotePat = \_ ->
123-
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
125+
, quotePat = \s -> do
126+
osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s
127+
when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
128+
[p|((==) osp' -> True)|]
124129
, quoteType = \_ ->
125-
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
130+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)"
126131
, quoteDec = \_ ->
127-
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
132+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)"
128133
}
129134
#else
130135
{ quoteExp = \s -> do
131136
osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s
132137
when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
133138
lift osp'
134-
, quotePat = \_ ->
135-
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
139+
, quotePat = \s -> do
140+
osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF8 ErrorOnCodingFailure) $ s
141+
when (not $ isValid osp') $ fail ("filepath not valid: " ++ show osp')
142+
[p|((==) osp' -> True)|]
136143
, quoteType = \_ ->
137-
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
144+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a type)"
138145
, quoteDec = \_ ->
139-
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
146+
fail "illegal QuasiQuote (allowed as expression or pattern only, used as a declaration)"
140147
}
141148
#endif
142149

0 commit comments

Comments
 (0)